0% found this document useful (0 votes)
60 views3 pages

Inserting and Deleting Records

The document describes how to insert, retrieve, update, and delete records from an SQL database table using ASP.NET controls and code behind. It includes text boxes and buttons to get user input for the record key and fields to insert/update. On button clicks, it opens a database connection, executes the appropriate SQL command, and displays the results or status messages. The grid view control is used to display the full table contents.

Uploaded by

Abhishek Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views3 pages

Inserting and Deleting Records

The document describes how to insert, retrieve, update, and delete records from an SQL database table using ASP.NET controls and code behind. It includes text boxes and buttons to get user input for the record key and fields to insert/update. On button clicks, it opens a database connection, executes the appropriate SQL command, and displays the results or status messages. The grid view control is used to display the full table contents.

Uploaded by

Abhishek Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Inserting and deleting records:-

<div style="margin-left: 120px">


Enter reg no:-&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Enter reg name:-&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Enter age:-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>


<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Insert" onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" Text="Retrieve"
onclick="Button2_Click" />
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button3" runat="server" Text="Delete" onclick="Button3_Click" />
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button4" runat="server" Text="Display"
onclick="Button4_Click" />
</div>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
static string str = @"Data Source=Lab-13\sqlexpress;Initial Catalog=Demo;Integrated
Security=True;Pooling=False";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd;
SqlDataAdapter da;
int sg;

protected void Page_Load(object sender, EventArgs e)


{
sg = 0;
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into employee(Reg_no,Reg_name,Reg_age) values(" + TextBox1.Text + ",'"
+ TextBox2.Text + "'," + TextBox3.Text + ")", con);
cmd.ExecuteNonQuery();
Label1.Text = "Data Successfully added";
con.Close();

}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
cmd=new SqlCommand("select Reg_name,Reg_age from Employee where Reg_no=" + TextBox1.Text + "", con);
cmd.ExecuteNonQuery();
SqlDataReader rdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
rdr.Read();
TextBox2.Text = rdr[0].ToString();
TextBox3.Text = rdr[1].ToString();
rdr.Close();
con.Close();

}
protected void Button3_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("delete from employee where Reg_no=" + TextBox1.Text + "", con);
cmd.ExecuteNonQuery();
Label1.Text = "Data deleted successfully..... ";
con.Close();

}
protected void Button4_Click(object sender, EventArgs e)
{
con.Open();
da = new SqlDataAdapter("select * from employee ", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();

}
}

Output:-

Inserting data into table:

Display data:
Retrieve data from table:

Delete data from table:

You might also like