Hello,
I am having a problem with GRIDVIEW IN C#.
The following code updates only the alternate rows.
When i use the edit button all the cells does get converted to textboxes
thereafter when i click on the updated button the alternate rows do not
enter the if loop mentioned in the code. So only the rows 1,3,5,7...
gets updated whereas the rows which bear the even no.s 2,4,6,8.. does
not get updated. And the smart thing is neither does it show any error
i am producing the code below
protected void GridView2_RowUpdating(object
sender,GridViewUpdateEventArgs e)
{
String S1;
SqlConnection MyCon = new
SqlConnection("Server=HEMALATHA\\SQLEXPRESS;Integr ated
Security=SSPI;database=demo");
MyCon.Open();
ImageButton b1 = new ImageButton();
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
foreach (GridViewRow gv in GridView2.Rows)
{
int k = 1;
Response.Write (k);
b1 = (ImageButton)gv.FindControl("ImageButton1");
t1 = (TextBox)gv.FindControl("TextBox1");
t2 = (TextBox)gv.FindControl("TextBox2");
if (gv.RowState == DataControlRowState.Edit)
{
int l = 2;
Response.Write (l);
S1 = "update t1 set name='" + t2.Text + "' where id=" +
t1.Text + "";
SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
UpdateCmd.ExecuteNonQuery();
}
}
MyCon.Close();
}
this is the code for asp
<asp:GridView DataKeyNames="Id" ID="GridView2" runat="server"
AutoGenerateColumns="False" Style="z-index: 104;
left: 66px; position: absolute; top: 405px" Width="581px"
OnRowEditing="GridView2_RowEditing"
OnRowUpdating="GridView2_RowUpdating"
OnRowCancelingEdit="GridView2_RowCancelingEdit"
OnRowDeleting="GridView2_RowDeleting"
OnRowDataBound="GridView2_RowDataBound"
But if i put the command buttons i can easily update
placing the code below.
protected void GridView2_RowUpdating(object
sender,GridViewUpdateEventArgs e)
{
String S1;
SqlConnection MyCon = new
SqlConnection("Server=HEMALATHA\\SQLEXPRESS;Integr ated
Security=SSPI;database=demo");
MyCon.Open();
ImageButton b1 = new ImageButton();
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
foreach (GridViewRow gv in GridView2.Rows)
{
int k = 1;
Response.Write (k);
b1 = (ImageButton)gv.FindControl("ImageButton1");
t1 = (TextBox)gv.FindControl("TextBox1");
t2 = (TextBox)gv.FindControl("TextBox2");
if (gv.RowState == DataControlRowState.Edit)
{
int l = 2;
Response.Write (l);
S1 = "update t1 set name='" + t2.Text + "' where id=" +
t1.Text + "";
SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
UpdateCmd.ExecuteNonQuery();
}
}
MyCon.Close();
}
OnSelectedIndexChanged="GridView2_SelectedIndexCha nged"
ShowFooter="True">
*** Sent via Developersdex http://www.developersdex.com ***