protected void Edit_Click(object sender, EventArgs e)
{
Button2.Visible = true;
if (GridView1.SelectedIndex > -1) //then we identity that the user has selected a row
{
TextBox1.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
TextBox2.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;
TextBox3.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text;
TextBox4.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text;
TextBox5.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text;
}
}
protected void Update_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text);
SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=employee;Integrated Security=True");
SqlCommand cmd = new SqlCommand("update emp set name='" + TextBox1.Text + "',desg='" + TextBox2.Text + "',sal='" + TextBox3.Text + "',city='" + TextBox4.Text + "',email='" + TextBox5.Text + "'where id = "+id,con);
con.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
}
I have ID in my Database as id, name, designation, salary, city and email...
so i need to end my update command with where....n...id
wat to do....
The value of id is getting "0"
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 12:57 AM|LINK
Check whether the value can be converted to numeric……
rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 06:49 AM|LINK
No..it couldn't be converted to numeric.....
any other alternatives..??
tusharrs
Contributor
3230 Points
668 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 06:59 AM|LINK
hi,
you have no id in first column of the grid
it simply contains the buttons
( Mark as Answer if it helps you out )
View my Blog
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 07:10 AM|LINK
Please make sure that the value of the textbox must be type of numeric……
What's the value there?plz do debugging……
rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 08:35 AM|LINK
<table class="style1" bgcolor="#FF0066"> <tr> <td class="style2"> NAME</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> DESIGNATION</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> SALARY</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> CITY </td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> E-MAIL</td> <td> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </td> </tr> </table><asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True" onselectedindexchanged="GridView1_SelectedIndexChanged" DataKeyNames="id"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:employeeConnectionString %>" DeleteCommand="DELETE FROM [emp] WHERE [id] = @id" InsertCommand="INSERT INTO [emp] ([name], [desg], [sal], [city], [email]) VALUES (@name, @desg, @sal, @city, @email)" SelectCommand="SELECT * FROM [emp]" UpdateCommand="UPDATE [emp] SET [name] = @name, [desg] = @desg, [sal] = @sal, [city] = @city, [email] = @email WHERE [id] = @id"> <DeleteParameters> <asp:Parameter Name="id" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="desg" Type="String" /> <asp:Parameter Name="sal" Type="Double" /> <asp:Parameter Name="city" Type="String" /> <asp:Parameter Name="email" Type="String" /> <asp:Parameter Name="id" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="desg" Type="String" /> <asp:Parameter Name="sal" Type="Double" /> <asp:Parameter Name="city" Type="String" /> <asp:Parameter Name="email" Type="String" /> </InsertParameters> </asp:SqlDataSource>protected void Edit_Click(object sender, EventArgs e) { Button2.Visible = true; if (GridView1.SelectedIndex > -1) //then we identity that the user has selected a row { TextBox1.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text; TextBox2.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text; TextBox3.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text; TextBox4.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[5].Text; TextBox5.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text; } } protected void Update_Click(object sender, EventArgs e) { int id = Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text); SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("update emp set name='" + TextBox1.Text + "',desg='" + TextBox2.Text + "',sal='" + TextBox3.Text + "',city='" + TextBox4.Text + "',email='" + TextBox5.Text + "'where id = "+id,con); con.Open(); cmd.ExecuteNonQuery(); GridView1.DataBind(); con.Close(); } I have ID in my Database as id, name, designation, salary, city and email... so i need to end my update command with where....n...id wat to do.... The value of id is getting "0"Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 27, 2012 08:47 AM|LINK
Hi rookie:)
What's the value of "
If 0, such an error exception cannot be thrown out,because "0" is a number……rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 28, 2012 04:48 PM|LINK
Hello Decker,
It worked...its my mistake..... Cells[1] solved my problem...
Thank u:-)