471,337 Members | 1,129 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

GridView does not update when used image buttons

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 ***
Aug 4 '07 #1
0 1713

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by NateDawg | last post: by
3 posts views Thread by pblack9455 | last post: by
reply views Thread by =?Utf-8?B?QnJ5YW4=?= | last post: by
1 post views Thread by Evan M. | last post: by
4 posts views Thread by Peter | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.