473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1803

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
3
by: adam222 | last post by:
hello, i have a web-form with a GridView control, i wanted to update & delete, using the AutoGenerateEditButton. when i used it with sqlDataSource (executing SP in the DB) it works like a...
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
3
by: pblack9455 | last post by:
I have a simple requirement to bind a small ArrayList of (ItemLine) Objects to a GridView control. The Gridview renders on the page and allows me to click update/edit buttons...however the data...
0
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
I'm using a GridView to display large search results, and in each row I display a "Select" image button to choose the record. What I've been noticing is that when users search and display the...
2
by: fanoftvb | last post by:
Hi, I'm having problem with my rows on gridview. When i click on the approve button, only the first row of the grid will update. Does anyone know how to make it that when i click on the different...
1
by: Evan M. | last post by:
Here's my GridView and my SqlDataSource <asp:GridView ID="ContactHistoryGrid" runat="server" AutoGenerateColumns="False" DataSourceID="ContactHistoryDS" DataKeyNames="JobHistoryID"...
3
by: dhaneshrs | last post by:
I have a gridview i managed to update using the edit option in the smartmenu. The problem comes when i delete a value or make a value read only. Say i have a table Employee with the following...
4
by: Peter | last post by:
I want to call a JavaScript on PageIndexChanged event, how do I do that? Thank You Peter
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.