Insert, Update, Delete, Search, Display, Single Click Insert Delete Both (Single click many operation) In GridView Using ASP.Net C#
******************** SQL Command *********************
create database test
create table new(name varchar (20)
,age int
,mobile bigint
)
********************* .aspx ********************
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
<asp:Textbox ID="textbox2" runat="server"></asp:Textbox><br />
<asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
<asp:Textbox ID="textbox3" runat="server"></asp:Textbox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Insert" />
<asp:Button ID="Button2" runat="server" Text="Update" OnClick="Update" />
<asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Delete" />
<asp:Button ID="Button5" runat="server" Text="Display" OnClick="Display" />
<asp:Button ID="Button6" runat="server" Text="Search" OnClick="Search" /> <br />
<asp:Button ID="Button7" runat="server" Text="Single_Click_Insert_Delete_Both" OnClick="Single_Click_Insert_Delete_Both" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
********************** .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;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=test;Integrated Security=True");
public void display()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from new";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
public void delete()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Insert(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Update(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update new set name='" + TextBox1.Text + "',mobile='" + textbox3.Text + "' where age='" + textbox2.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
}
protected void Delete(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Display(object sender, EventArgs e)
{
display();
}
protected void Search(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
protected void Single_Click_Insert_Delete_Both(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
// cmd.ExecuteNonQuery();
// con.Close();
// display();
//con.Open();
// SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
// delete();
display();
}
}
******************** SQL Command *********************
create database test
create table new(name varchar (20)
,age int
,mobile bigint
)
********************* .aspx ********************
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
<asp:Textbox ID="textbox2" runat="server"></asp:Textbox><br />
<asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
<asp:Textbox ID="textbox3" runat="server"></asp:Textbox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Insert" />
<asp:Button ID="Button2" runat="server" Text="Update" OnClick="Update" />
<asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Delete" />
<asp:Button ID="Button5" runat="server" Text="Display" OnClick="Display" />
<asp:Button ID="Button6" runat="server" Text="Search" OnClick="Search" /> <br />
<asp:Button ID="Button7" runat="server" Text="Single_Click_Insert_Delete_Both" OnClick="Single_Click_Insert_Delete_Both" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
********************** .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;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=test;Integrated Security=True");
public void display()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from new";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
public void delete()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Insert(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Update(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update new set name='" + TextBox1.Text + "',mobile='" + textbox3.Text + "' where age='" + textbox2.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
}
protected void Delete(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
display();
}
protected void Display(object sender, EventArgs e)
{
display();
}
protected void Search(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
protected void Single_Click_Insert_Delete_Both(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
// cmd.ExecuteNonQuery();
// con.Close();
// display();
//con.Open();
// SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
// delete();
display();
}
}