First create a table with the following two fields:

  1. id int (auto increment)
  2. name nvarchar(100)

Save as Test

table design

Insert some values into this new table.

The table will looks as in this:

table

Then go to Visual Studio then go to New > Website then choose Empty Website.

Provide your website a name, if you want to change the location of your website then you can change your location.

The next step is to add a webform with the following code.

Defult.aspx

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <title></title>
  5. </head>
  6. <body>
  7. <form id="form1" runat="server">
  8. <div>
  9. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
  10. <Columns>
  11. <asp:TemplateField>
  12. <HeaderTemplate>
  13. ID
  14. </HeaderTemplate>
  15. <ItemTemplate>
  16. <%# Eval("id") %>
  17. </ItemTemplate>
  18. <EditItemTemplate>
  19. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("id") %>' ReadOnly="true"></asp:TextBox>
  20. </EditItemTemplate>
  21. </asp:TemplateField>
  22. <asp:TemplateField>
  23. <HeaderTemplate>
  24. Name
  25. </HeaderTemplate>
  26. <ItemTemplate>
  27. <%# Eval("name") %>
  28. </ItemTemplate>
  29. <EditItemTemplate>
  30. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("name") %>' ></asp:TextBox>
  31. </EditItemTemplate>
  32. </asp:TemplateField>
  33. <asp:TemplateField>
  34. <HeaderTemplate>
  35. Edit
  36. </HeaderTemplate>
  37. <ItemTemplate>
  38. <asp:Button ID="edit" runat="server" CommandName="Edit" formnovalidate Height="28" Text="Edit" />
  39. </ItemTemplate>
  40. <EditItemTemplate>
  41. <asp:Button ID="update" runat="server" CommandName="Update" formnovalidate Text="Update" />
  42. <asp:Button ID="cancel" runat="server" CommandName="Cancel" formnovalidate Text="Cancel"/>
  43. </EditItemTemplate>
  44. </asp:TemplateField>
  45. <asp:TemplateField>
  46. <HeaderTemplate>
  47. Delete
  48. </HeaderTemplate>
  49. <ItemTemplate>
  50. <asp:Button ID="delete" runat="server" CommandName="Delete" formnovalidate Text="Delete"/>
  51. </ItemTemplate>
  52. </asp:TemplateField>
  53. </Columns>
  54. </asp:GridView>
  55. <asp:Label ID="Label1" runat="server"></asp:Label>
  56. </div>
  57. </form>
  58. </body>
  59. </html>
Then in the code behind for defult.asp.cs:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if(!IsPostBack)
  14. {
  15. ShowTest();
  16. }
  17. }
  18. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  19. {
  20. string index = GridView1.DataKeys[e.RowIndex].Value.ToString();
  21. int id = int.Parse(index);
  22. TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
  23. SqlConnection con = new SqlConnection(connection());
  24. SqlCommand cmd = new SqlCommand("update Test set name='"+name.Text+"' where id=" + id + "", con);
  25. con.Open();
  26. int i = cmd.ExecuteNonQuery();
  27. con.Close();
  28. if (i > 0)
  29. {
  30. Label1.Text = "data updated sucessfully";
  31. }
  32. else
  33. {
  34. Label1.Text = "data not updated";
  35. }
  36. GridView1.EditIndex = -1;
  37. ShowTest();
  38. }
  39. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  40. {
  41. GridView1.EditIndex = e.NewEditIndex;
  42. ShowTest();
  43. }
  44. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  45. {
  46. string index = GridView1.DataKeys[e.RowIndex].Value.ToString();
  47. int id=int.Parse(index);
  48. SqlConnection con = new SqlConnection(connection());
  49. SqlCommand cmd = new SqlCommand("delete from Test where id="+id+"",con);
  50. con.Open();
  51. int i=cmd.ExecuteNonQuery();
  52. con.Close();
  53. if(i>0)
  54. {
  55. Label1.Text = "data deleted sucessfully";
  56. }
  57. else
  58. {
  59. Label1.Text = "data not deleted";
  60. }
  61. GridView1.EditIndex = -1;
  62. ShowTest();
  63. }
  64. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  65. {
  66. GridView1.EditIndex = -1;
  67. ShowTest();
  68. }
  69. public void ShowTest()
  70. {
  71. SqlConnection con = new SqlConnection(connection());
  72. SqlDataAdapter da = new SqlDataAdapter("select * from Test", con);
  73. DataTable dt = new DataTable();
  74. da.Fill(dt);
  75. GridView1.DataSource = dt;
  76. GridView1.DataBind();
  77. }
  78. public string connection()
  79. {
  80. string con = "Data Source=localhost;Initial Catalog=UsersDB;Integrated Security=True;";
  81. return con;
  82. }
  83. }
Note: UsersDB is my database name. You can (I did) use Windows Authentication.

The final way it looks in the browser:

output