Program of Insert, Update, Delete, Select
Program (Insert, Update, Delete, Select)
1. Design (default.aspx)
2. Code(default.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "insert into stu values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
cm = new SqlCommand(k,cn);
cm.ExecuteNonQuery();
}
protected void Button2_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "delete from stu where rn='"+TextBox1.Text+"' ";
cm = new SqlCommand(k,cn);
cm.ExecuteNonQuery();
}
protected void Button3_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "Update stu set name = '"+TextBox2.Text+"', age = '"+TextBox3.Text+"' where rn = '"+TextBox1.Text+"'";
cm = new SqlCommand(k, cn);
cm.ExecuteNonQuery();
}
protected void Button4_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "select * from stu where rn = '"+TextBox1.Text+"'";
cm = new SqlCommand(k,cn);
dr = cm.ExecuteReader();
if (dr.Read())
{
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
}
3. Output
Click Here For Insert Output
https://aspdotnetpb.blogspot.in/2017/06/insert-query.html
Click Here For Delete Output
https://aspdotnetpb.blogspot.in/2017/06/delete-query.html
Click Here For Update Output
https://aspdotnetpb.blogspot.in/2017/06/update-query.html
Click Here For Select Output
https://aspdotnetpb.blogspot.in/2017/06/select-query.html
1. Design (default.aspx)
2. Code(default.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "insert into stu values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
cm = new SqlCommand(k,cn);
cm.ExecuteNonQuery();
}
protected void Button2_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "delete from stu where rn='"+TextBox1.Text+"' ";
cm = new SqlCommand(k,cn);
cm.ExecuteNonQuery();
}
protected void Button3_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "Update stu set name = '"+TextBox2.Text+"', age = '"+TextBox3.Text+"' where rn = '"+TextBox1.Text+"'";
cm = new SqlCommand(k, cn);
cm.ExecuteNonQuery();
}
protected void Button4_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
cn = new SqlConnection(cs);
cn.Open();
string k = "select * from stu where rn = '"+TextBox1.Text+"'";
cm = new SqlCommand(k,cn);
dr = cm.ExecuteReader();
if (dr.Read())
{
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
}
3. Output
Click Here For Insert Output
https://aspdotnetpb.blogspot.in/2017/06/insert-query.html
Click Here For Delete Output
https://aspdotnetpb.blogspot.in/2017/06/delete-query.html
Click Here For Update Output
https://aspdotnetpb.blogspot.in/2017/06/update-query.html
Click Here For Select Output
https://aspdotnetpb.blogspot.in/2017/06/select-query.html