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_RowUp dating(object
sender,GridView UpdateEventArgs e)
{
String S1;
SqlConnection MyCon = new
SqlConnection(" Server=HEMALATH A\\SQLEXPRESS;I ntegrated
Security=SSPI;d atabase=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("I mageButton1");
t1 = (TextBox)gv.Fin dControl("TextB ox1");
t2 = (TextBox)gv.Fin dControl("TextB ox2");
if (gv.RowState == DataControlRowS tate.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.Execu teNonQuery();
}
}
MyCon.Close();
}
this is the code for asp
<asp:GridView DataKeyNames="I d" ID="GridView2" runat="server"
AutoGenerateCol umns="False" Style="z-index: 104;
left: 66px; position: absolute; top: 405px" Width="581px"
OnRowEditing="G ridView2_RowEdi ting"
OnRowUpdating=" GridView2_RowUp dating"
OnRowCancelingE dit="GridView2_ RowCancelingEdi t"
OnRowDeleting=" GridView2_RowDe leting"
OnRowDataBound= "GridView2_RowD ataBound"
But if i put the command buttons i can easily update
placing the code below.
protected void GridView2_RowUp dating(object
sender,GridView UpdateEventArgs e)
{
String S1;
SqlConnection MyCon = new
SqlConnection(" Server=HEMALATH A\\SQLEXPRESS;I ntegrated
Security=SSPI;d atabase=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("I mageButton1");
t1 = (TextBox)gv.Fin dControl("TextB ox1");
t2 = (TextBox)gv.Fin dControl("TextB ox2");
if (gv.RowState == DataControlRowS tate.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.Execu teNonQuery();
}
}
MyCon.Close();
}
OnSelectedIndex Changed="GridVi ew2_SelectedInd exChanged"
ShowFooter="Tru e">
*** Sent via Developersdex http://www.developersdex.com ***