Loading....
CRUD in C#:
Naming convention should be very important for Id and Method.
SQL Table Query:
        CREATE TABLE [tblStudents]( 
	        [Id] [int] IDENTITY(1,1) NOT NULL,
	        [Name] [varchar](40) NULL,
	        [Gender] [varchar](10) NULL,
	        [Email] [varchar](40) NULL,
	        [SubjectId] [int] NULL
            ) 

        CREATE TABLE [MasterSubjects](
	    [Id] [int] IDENTITY(1,1) NOT NULL,
	    [Sub_Name] [varchar](40) NULL
            )
    

NameSpace Used
    using System.Data;
    using System.Data.SqlClient;
                                

aspx C#
<form id="form1" runat="server">
<div>
<h1>Registration Form</h1>
<asp:Label ID="lblId" runat="server" Visible="false" ></asp:Label>
<table style="font-size:18px">
<tr>
    <td> Name</td>
    <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
    <td>Subject:</td>
    <td><asp:DropDownList ID="ddlSub" runat="server">
            <asp:ListItem>--Select Subjects-- </asp:ListItem>
            <asp:ListItem Value="1">Physics </asp:ListItem>
            <asp:ListItem Value="2">Chemistry </asp:ListItem>
            <asp:ListItem Value="3">Biology </asp:ListItem>
            <asp:ListItem Value="4">Maths </asp:ListItem>
            <asp:ListItem Value="5">English </asp:ListItem>
            <asp:ListItem Value="6">Computer </asp:ListItem>
            <asp:ListItem Value="7">History & Civics </asp:ListItem>
            </asp:DropDownList></td>
</tr>
<tr>
    <td>Email:</td>
    <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
<tr>
    <td>Gender:</td>
    <td>
        <asp:RadioButtonList ID="rbtnListGender" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Text="Male" Value="Male"></asp:ListItem>
            <asp:ListItem Text="Female" Value="Female"></asp:ListItem>
        </asp:RadioButtonList>
    </td>
</tr>
<tr>
    <td></td>
    <td>
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" Visible="false" />
        <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
    </td>
</tr>
</table>
<h2>Registration Data</h2>
<table border="1" style="border-collapse:collapse">
<tr >
    <th>Name</th>
    <th>Email</th>
    <th>Gender</th>
    <th>Subject</th>
    <th colspan="2">Action</th>
</tr>
<asp:Repeater ID="rptStudents" runat="server" OnItemCommand="rptStudents_ItemCommand">
    <ItemTemplate>
        <tr>
            <td><%# Eval("Name") %></td>
            <td><%# Eval("Email") %></td>
            <td><%# Eval("Gender") %></td>
            <td><%# Eval("Subject") %></td>
            <td>
                <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="Edit"
                      CommandArgument='<%# Eval("Id") %>'>Edit</asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete"
                         CommandArgument='<%# Eval("Id") %>'>Delete</asp:LinkButton>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
SqlConnection sqlConn  = new SqlConnection(@"Data Source= ; Initial Catalog= ; User ID= ; Password= ");
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bindStudents();
    }
}

protected void btnSave_Click(object sender, EventArgs e)
{
 try
   { 
    SqlCommand cmd = new SqlCommand("INSERT INTO tblStudents (Name, Email, Gender, SubjectId) VALUES " +
                  "('" + txtName.Text + "', '" + txtEmail.Text + "', '" + (rbtnListGender.SelectedValue)
                  + "'," +" '" + ddlSub.SelectedValue + "');", sqlConn);

    sqlConn.Open();
    cmd.ExecuteNonQuery();
    sqlConn.Close();
    Response.Write("<script>alert('Data saved Successfully')</script>");
    btnClear_Click(sender,e);
    bindStudents();
  }
 catch(Exception ex)
  {
    Console.WriteLine("An error occurred: " + ex.Message);
  } 
}

protected void rptStudents_ItemCommand(object source, RepeaterCommandEventArgs e)
{
  if (e.CommandName == "Edit")
  { 
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblStudents WHERE Id = " + e.CommandArgument, sqlConn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt); 
    lblId.Text = dt.Rows[0]["Id"].ToString();
    txtName.Text = dt.Rows[0]["Name"].ToString();
    txtEmail.Text = dt.Rows[0]["Email"].ToString();
    rbtnListGender.SelectedValue = dt.Rows[0]["Gender"].ToString();
    ddlSub.SelectedValue = dt.Rows[0]["SubjectId"].ToString();
    btnSave.Visible = false;
    btnUpdate.Visible = true;
  }
  else if (e.CommandName == "Delete")
  { 
    SqlCommand cmd = new SqlCommand("DELETE FROM tblStudents WHERE Id = " + e.CommandArgument, sqlConn);
    sqlConn.Open();
    cmd.ExecuteNonQuery();
    sqlConn.Close();
    Response.Write("<script>alert('Data Delete Successfully')</script>");
    bindStudents();
  }
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
  try
   { 
    SqlCommand cmd = new SqlCommand("UPDATE  tblStudents SET Name='" + txtName.Text + "',Email='" + txtEmail.Text
                         +"', SubjectId='" + ddlSub.SelectedValue + "',Gender='" + (rbtnListGender.SelectedValue)
                         + "' WHERE Id='"+ lblId.Text + "'", sqlConn); 

    sqlConn.Open();
    cmd.ExecuteNonQuery();
    sqlConn.Close();
    Response.Write("<script>alert('Data Update Successfully')</script>");
    btnClear_Click(sender, e);
    bindStudents();
  }
 catch (Exception ex)
  {
    Console.WriteLine("An error occurred: " + ex.Message);
  } 
}
protected void btnClear_Click(object sender, EventArgs e)
{
   txtName.Text = ""; ddlSub.SelectedIndex = 0; txtEmail.Text = ""; rbtnListGender.ClearSelection();
   lblId.Text = ""; btnSave.Visible = true; btnUpdate.Visible = false;
}

protected void bindStudents()
{
   SqlCommand cmd = new SqlCommand("SELECT st.Id, st.Name, st.Email, st.Gender, sb.Sub_Name AS Subject FROM
                     tblStudents"+" st left outer JOIN MasterSubjects sb ON sb.Id = st.SubjectId", sqlConn);

   SqlDataAdapter sda = new SqlDataAdapter(cmd);
   DataTable dt = new DataTable();
   sda.Fill(dt);
   rptStudents.DataSource = dt;
   rptStudents.DataBind();
}

Last Update: July 10, 2026  

July 9, 2026 27 vikas@crmhike.com  ASP.NET
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Add A Knowledge Base Question !

You will receive an email when your question will be answered.

+ = Verify Human or Spambot ?

Back To Top

Add A Knowledge Base Question !

You will receive an email when your question will be answered.

+ = Verify Human or Spambot ?