Here, I'm Inserting values through Table textboxes..... where the inserted values or data should be displayed in GridVIew.
What i want is to Insert data through TABLE textboxes only and For EDITING and UPDATING should be performed in GRIDVIEW only....
plz suggest me how to UPDATE???
<table align="center" cellpadding="2" cellspacing="1"
style="background-color: #FF0066">
<tr>
<td>
NAME:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
DESIGNATION:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
SALARY:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
CITY:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
E-MAIL:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td colspan="2">
<asp:Button ID="Button1" runat="server" BackColor="#3333FF"
BorderColor="#000099" ForeColor="White" onclick="Button1_Click" Text="SUBMIT" />
</td>
</tr>
</table>
1)You should know that GridView doesn't support Inserting,however you can do insert things through the FooterRow。Now please have a look at this sample code(please download and use it carefully,it will satisfy your need……):http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0
Hello Decker Dong:)) Yes, but a part of it..... Now, when i click on EDIT button,,,,,that selected row details in gridview must be populated to the Table's textboxes so that i can edit at table and update there itself....!!
Now, when i click on EDIT button,,,,,that selected row details in gridview must be populated to the Table's textboxes so that i can edit at table and update there itself....!!
Please handle the GridView_RowEdit:
protected void GridView1_RowEdit(……)
{
GridView1.RowEdit = e.NewEditIndex;
//Here you should rebind the GridView by settings DataSource and call DataBind()
}
I'm Done with Editing but at updating i'm getting an error Error:Cannot implicitly convert type 'string' to 'int'
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();
//lblHdnempId.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[Index where it is displaying in the gridview].Text;
GridView1.DataBind();
con.Close();
Error in updating.....
ErroR:Input string was not in a correct format.
int id = Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[0].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;InitialCatalog=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();
}
rookie tiro
Member
86 Points
130 Posts
Gridview Update Problem!!!!
Apr 22, 2012 07:29 AM|LINK
Here, I'm Inserting values through Table textboxes..... where the inserted values or data should be displayed in GridVIew. What i want is to Insert data through TABLE textboxes only and For EDITING and UPDATING should be performed in GRIDVIEW only.... plz suggest me how to UPDATE??? <table align="center" cellpadding="2" cellspacing="1" style="background-color: #FF0066"> <tr> <td> NAME:</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td> DESIGNATION:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td> SALARY:</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td> CITY:</td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td> E-MAIL:</td> <td> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </td> </tr> <tr align="center"> <td colspan="2"> <asp:Button ID="Button1" runat="server" BackColor="#3333FF" BorderColor="#000099" ForeColor="White" onclick="Button1_Click" Text="SUBMIT" /> </td> </tr> </table><asp:GridView ID="GridView1" runat="server" align="center" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="false" onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" DataKeyNames="Name" onrowupdating="GridView1_RowUpdating"> <FooterStyle BackColor="#CCCCCC" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="#CCCCCC" /> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:Button ID="LinkButton1" runat="server" Text="Update" CommandName="Update"></asp:Button> <asp:Button ID="LinkButton2" runat="server" Text="Cancel" CommandName="Cancel"></asp:Button> </EditItemTemplate> <ItemTemplate> <asp:Button ID="LinkButton4" runat="server" Text="Select" CommandName="Select"></asp:Button> <asp:Button ID="LinkButton3" runat="server" Text="Edit" CommandName="Edit"></asp:Button> <asp:Button ID="LinkButton5" runat="server" Text="Delete" CommandName="Delete"></asp:Button> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="TextBox7" runat="server" Text='<%#Eval("name") %>'> </asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("name") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Designation"> <EditItemTemplate> <asp:TextBox ID="TextBox8" runat="server" Text='<%#Eval("desg") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("desg") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Salary"> <EditItemTemplate> <asp:TextBox ID="TextBox9" runat="server" Text='<%#Eval("sal") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("sal") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:TextBox ID="TextBox10" runat="server" Text='<%#Eval("city") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("city") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <EditItemTemplate> <asp:TextBox ID="TextBox11" runat="server" Text='<%#Eval("email") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("email") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>amit.jain
Star
11225 Points
1815 Posts
Re: Gridview Update Problem!!!!
Apr 22, 2012 12:33 PM|LINK
refer http://www.csharpaspnetarticles.com/2009/05/gridview-sqldatasource-insert-edit.html
http://www.csharpaspnetarticles.com/2009/06/gridview-objectdatasource-insert-update.html
amiT jaiN
ASP.NET C# VB Articles And Code Examples
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 24, 2012 01:40 AM|LINK
Hello rookie tiro:)
1)You should know that GridView doesn't support Inserting,however you can do insert things through the FooterRow。Now please have a look at this sample code(please download and use it carefully,it will satisfy your need……):http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0
2)I found your problem was solved here:http://forums.asp.net/p/1795472/4946759.aspx/1?Re+How+to+update+GridView+
rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 25, 2012 07:45 AM|LINK
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 25, 2012 07:52 AM|LINK
Please handle the GridView_RowEdit:
protected void GridView1_RowEdit(……) { GridView1.RowEdit = e.NewEditIndex; //Here you should rebind the GridView by settings DataSource and call DataBind() }suresh dasar...
Contributor
3606 Points
743 Posts
Re: Gridview Update Problem!!!!
Apr 25, 2012 12:29 PM|LINK
hi rookie,
check below posts i hope it helps you
http://www.aspdotnet-suresh.com/2011/02/how-to-highlight-gridview-rows-on.html
http://www.aspdotnet-suresh.com/2011/02/normal-0-false-false-false-en-us-x-none.html
Please "Mark as Answer" If post helps you
bhaskar.mule
Contributor
2264 Points
656 Posts
Re: Gridview Update Problem!!!!
Apr 25, 2012 12:36 PM|LINK
hi
refer this link
http://csharpektroncmssql.blogspot.com/2011/11/row-editdeleteupdate-in-grid-view-using.html
Site:Rare technical solutions
rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 26, 2012 03:12 AM|LINK
Guys u didnt get me.... I wanted grid selected row's data to be populated into the textboxes of my table...when i click the button EDIT That means this..... <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"> SALARY</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> D.O.B</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> ZIP</td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> PLACE</td> <td> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </td> </tr> </table>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(); //lblHdnempId.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[Index where it is displaying in the gridview].Text; GridView1.DataBind(); con.Close();Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Gridview Update Problem!!!!
Apr 26, 2012 03:16 AM|LINK
Sorry and now you can try to put the codes into SelecteIndexChanged event——
protected void GridView_SelecteIndexChanged(object sender, EventArgs e) { if (GridView1.SelectedIndex > -1) { 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; } }rookie tiro
Member
86 Points
130 Posts
Re: Gridview Update Problem!!!!
Apr 26, 2012 05:24 PM|LINK
Error in updating..... ErroR:Input string was not in a correct format. int id = Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[0].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;InitialCatalog=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(); }