data grid out of range of valid values

Posted by Kishore under ASP.NET on 3/21/2010 | Views : 3902 | Status : [Member] | Replies : 3
hi,
i created one aspx page that contains datagrid(properties: showfooter-true) and button(name: add record) controls, when i click the button it shows specified argument was out of range of values and my code is...plz clarify my doubt
here am using stored procedure for inseting values into database


protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow r = GridView1.FooterRow;

int newempid, newesalary;
string newename, neweprofession, newedept;
DateTime newdateofjoining;

TextBox t1 = (TextBox)r.Cells[0].Controls[0];

newempid = Convert.ToInt32(t1.Text);

t1 = (TextBox)r.Cells[1].Controls[0];
newename = t1.Text;

t1 = (TextBox)r.Cells[2].Controls[0];
neweprofession = t1.Text;

t1 = (TextBox)r.Cells[3].Controls[0];
newdateofjoining = Convert.ToDateTime(t1.Text);

t1 = (TextBox)r.Cells[4].Controls[0];
newesalary = Convert.ToInt32(t1.Text);

t1 = (TextBox)r.Cells[5].Controls[0];
newedept = t1.Text;

cmd = new SqlCommand("insrtrow", con);
cmd.CommandType = CommandType.StoredProcedure;


p = new SqlParameter("@empid", SqlDbType.Int);
p.Value = newempid;
cmd.Parameters.Add(p);

p = new SqlParameter("@ename", SqlDbType.VarChar);
p.Value = newename;
cmd.Parameters.Add(p);

p = new SqlParameter("@eprofession", SqlDbType.VarChar);
p.Value = neweprofession;
cmd.Parameters.Add(p);

p = new SqlParameter("@dateofjoining", SqlDbType.DateTime);
p.Value = newdateofjoining;
cmd.Parameters.Add(p);

p = new SqlParameter("@esalary", SqlDbType.Int);
p.Value = newesalary;
cmd.Parameters.Add(p);

p = new SqlParameter("@edept", SqlDbType.VarChar);
p.Value = newedept;
cmd.Parameters.Add(p);


con.Open();
cmd.ExecuteNonQuery();
con.Close();


}




Responses

Posted by: Abhijit Jana on: 3/21/2010 [Member] [MVP] Bronze

Up
0
Down
Just put a breakpoint on the button click event. as execute line by line. and check which line throwing the exception.

Cheers !
Abhijit

Kishore, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore on: 3/21/2010 [Member] Starter

Up
0
Down
here the statement throws the error: Specified argument was out of the range of valid values.
Parameter name: index

TextBox t1 = (TextBox)r.Cells[0].Controls[0];


Kishore, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakhangarg on: 3/22/2010 [Member] [Moderator] Silver

Up
0
Down
Hi Kishore-

You can Add the button in the footer and then on ItemCommand event
write Command name for button as insert or any name that you want.

on command event check like this:
if(e.Commandname=="Insert")

{
TextBox t1 = (TextBox)GridView1.FooterRow.Cells[0].FindControl["ControlID"];
newempid = Convert.ToInt32(t1.Text);
}


Thanks
Lakhan Pal Garg

Kishore, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response