472,984 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 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 1780

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.