Insert Update Delete Using Grid-view
Step:-1 ) Using SQL Server Create Table
Create Table UserTest(
id int primary key identity(1,1),
Name nvarchar(200),
EmailId nvarchar(200),
DOB varchar(50),
City nvarchar(200),
Company nvarchar(200),
Salary int,
);
Step :- 2) Using Visual Studio Web Application
TestWebForm.aspx
-----------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWebForm.aspx.cs" Inherits="TestWebApp.TestWebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function validateForm() {
var txtName = document.getElementById('txtName');
var txtEmailId = document.getElementById('txtEmailId');
var txtDOB = document.getElementById('txtDOB');
var txtCity = document.getElementById('txtCity');
var txtCompany = document.getElementById('txtCompany');
var txtSalary = document.getElementById('txtSalary');
if (txtName.value == '') { alert('Enter Name'); txtName.focus(); return false; }
if (txtEmailId.value == '') { alert('Enter Email-Id'); txtEmailId.focus(); return false; }
if (txtDOB.value == '') { alert('Enter Date of Birth'); txtDOB.focus(); return false; }
if (txtCity.value == '') { alert('Enter City Name'); txtCity.focus(); return false; }
if (txtCompany.value == '') { alert('Enter Company Name'); txtCompany.focus(); return false; }
if (txtSalary.value == '') { alert('Enter Sallary'); txtSalary.focus(); return false; }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>ID</td>
<td>
<input type="text" id="txtID" readonly="true" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" id="txtName" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>EmailID</td>
<td>
<input type="text" id="txtEmailId" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>Date of Birth</td>
<td>
<input type="text" id="txtDOB" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>City</td>
<td>
<input type="text" id="txtCity" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>Company</td>
<td>
<input type="text" id="txtCompany" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td>Salary</td>
<td>
<input type="text" id="txtSalary" class="inputTxt" runat="server" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button runat="server" ID="btnSave" CssClass="btn" Text="Save" OnClientClick="return validateForm()" OnClick="btnSave_Click" />
<asp:Button runat="server" ID="btnUpdate" CssClass="btn" Text="Update" Visible="false" OnClientClick="return validateForm()" OnClick="btnUpdate_Click" />
<asp:Button runat="server" ID="btnCancel" CssClass="btn" Text="Cancel" OnClick="btnCancel_Click" /></td>
</tr>
</table>
<br />
<br />
<asp:GridView ID="grvTest" runat="server" DataKeyNames="id" OnRowDeleting="grvTest_RowDeleting" OnSelectedIndexChanged="grvTest_SelectedIndexChanged">
<Columns>
<asp:CommandField HeaderText="Update" ShowSelectButton="true" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
TestWebForm.aspx.cs
--------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestWebApp
{
public partial class TestWebForm : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TestApp;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
con.Open();
string str = "INSERT INTO UserTest(Name,EmailId,DOB,City,Company,Salary)";
str += " VALUES('" + txtName.Value + "','" + txtEmailId.Value + "','" + Convert.ToDateTime(txtDOB.Value) + "','" + txtCity.Value + "','" + txtCompany.Value + "','" + txtSalary.Value + "');";
SqlCommand cmd = new SqlCommand(str, con);
int i = cmd.ExecuteNonQuery();
con.Close();
resetForm();
BindGridView();
}
catch (Exception)
{
throw;
}
finally
{
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
resetForm();
Response.Redirect("TestWebForm.aspx");
}
public void resetForm() {
txtCity.Value = "";
txtCompany.Value = "";
txtDOB.Value = "";
txtEmailId.Value = "";
txtID.Value = "";
txtName.Value = "";
txtSalary.Value = "";
}
public void BindGridView()
{
try
{
DataSet ds = new DataSet();
con.Open();
string str = "SELECT * FROM UserTest;";
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter(str, con);
sda.Fill(ds);
con.Close();
grvTest.DataSource = ds;
grvTest.DataBind();
}
catch (Exception)
{
throw;
}
finally
{
}
}
protected void grvTest_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
con.Open();
int id = Convert.ToInt32(grvTest.DataKeys[e.RowIndex].Value);
string str = "DELETE FROM UserTest WHERE id='" + id + "'; ";
SqlCommand cmd = new SqlCommand(str, con);
int i = cmd.ExecuteNonQuery();
con.Close();
BindGridView();
}
catch (Exception)
{
throw;
}
finally
{
}
}
protected void grvTest_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow newRow = grvTest.SelectedRow;
txtID.Value = newRow.Cells[2].Text;
txtName.Value = newRow.Cells[3].Text;
txtEmailId.Value = newRow.Cells[4].Text;
txtDOB.Value = newRow.Cells[5].Text;
txtCity.Value = newRow.Cells[6].Text;
txtCompany.Value = newRow.Cells[7].Text;
txtSalary.Value = newRow.Cells[8].Text;
btnSave.Visible = false;
btnUpdate.Visible = true;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
con.Open();
string str = "UPDATE UserTest SET Name='" + txtName.Value + "',EmailId='" + txtEmailId.Value + "',DOB='" + txtDOB.Value + "',City='" + txtCity.Value + "',Company='" + txtCompany.Value + "',Salary='" + txtSalary.Value + "' WHERE id='" + txtID.Value + "'; ";
SqlCommand cmd = new SqlCommand(str, con);
int i = cmd.ExecuteNonQuery();
con.Close();
BindGridView();
resetForm();
}
catch (Exception)
{
throw;
}
finally
{
}
}
}
}
Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog on. Dot Net Online course
ReplyDelete