HTML
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Text="Insert New" OnClick="InsertNew" runat="server" />
<br />
<table id="tbInsert" runat="server" visible="false">
<tr runat="server" id="trId">
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server" />
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox ID="txtCity" runat="server" />
</td>
</tr>
</table>
<asp:Button ID="btnUpdate" Text="Update" OnClick="Update" Visible="false" runat="server" />
<br />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerId">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="CustomerName" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="City" HeaderText="Country" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" ToolTip="Edit Unit" OnClick="lnkEdit_Click"
CausesValidation="False" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" ToolTip="Delete Unit" OnClick="Delete"
CausesValidation="False" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
Namespace
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populate();
}
}
private void Populate()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
protected void Add(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
int customerId = Convert.ToInt32(this.GridView1.DataKeys[clickedRow.RowIndex].Value);
string sqlStatment = "INSERT INTO [Customers](CustomerName,City) VALUES (@CustomerName,@City)";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@CustomerName", Convert.ToInt32(this.txtName.Text));
cmd.Parameters.AddWithValue("@City", Convert.ToInt32(this.txtCity.Text));
cmd.ExecuteNonQuery();
con.Close();
}
}
this.Populate();
}
protected void InsertNew(object sender, EventArgs e)
{
this.tbInsert.Visible = true;
this.trId.Visible = true;
}
protected void Delete(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
int customerId = Convert.ToInt32(this.GridView1.DataKeys[clickedRow.RowIndex].Value);
string sqlStatment = "DELETE From Customers WHERE CustomerId = @id";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@Id", customerId);
cmd.ExecuteNonQuery();
con.Close();
}
}
this.Populate();
}
protected void lnkEdit_Click(object sender, EventArgs e)
{
this.btnUpdate.Visible = true;
this.tbInsert.Visible = true;
this.trId.Visible = false;
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
ViewState["RowId"] = clickedRow.Cells[0].Text;
this.txtId.Text = clickedRow.Cells[0].Text;
this.txtName.Text = clickedRow.Cells[1].Text;
txtCity.Text = clickedRow.Cells[2].Text;
this.btnUpdate.Text = "Update";
}
protected void Update(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string sqlStatment = "UPDATE Customers SET CustomerName =@CustomerName, City = @City WHERE CustomerId = @id";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@Id", Convert.ToInt32(this.txtId.Text));
cmd.Parameters.AddWithValue("@CustomerName", this.txtName.Text);
cmd.Parameters.AddWithValue("@City", this.txtCity.Text);
cmd.ExecuteNonQuery();
con.Close();
}
}
this.Populate();
}
SQL
CREATE TABLE [dbo].[Customers](
[CustomerId] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](50) NOT NULL,
[City] [varchar](50) NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO