C# for Save and Retrive Data
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;
using System.Configuration;
public partial class GridviewSample : System.Web.UI.Page
{
//string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
//SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
try
{
//Page.Request.Form["datepickernm"] = "10/12/2015";
txtCustomerName.Focus();
if (!IsPostBack)
{
FillGrid();
}
}
catch
{
}
}
void FillGrid()
{
try
{
// con = new SqlConnection(strConnString);
cmd = new SqlCommand();
cmd.CommandText = "Select CustomerID,CustomerName,PhoneNumber,Address,dob from tblCustomers where IsActive=1";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvDepartments.DataSource = ds;
gvDepartments.DataBind();
}
catch
{
}
}
void ClearControls()
{
try
{
txtCustomerName.Text = "";
txtPhoneNumber.Text = "";
txtAddress.Text = "";
hidCustomerID.Value = "";
btnSave.Visible = true;
btnUpdate.Visible = false;
rbMale.Checked = false;
rbFemale.Checked = false;
}
catch
{
throw;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
string text = datepicker.Value;
string gender = string.Empty;
Int16 val = 0;
if (rbMale.Checked)
{
gender = "1";
val = Int16.Parse(gender);
}
else {
gender = "2";
val = Int16.Parse(gender);
}
//con = new SqlConnection(strConnString);
cmd = new SqlCommand();
cmd.CommandText = "insert into tblCustomers (CustomerName,PhoneNumber,Address,IsActive,dob,gender) values (@CustomerName,@PhoneNumber,@Address,1,@dob,@gender)";
cmd.Parameters.AddWithValue("@CustomerName", txtCustomerName.Text);
cmd.Parameters.AddWithValue("@PhoneNumber", txtPhoneNumber.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@dob", text);
cmd.Parameters.AddWithValue("@gender", val);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
ClearControls();
lblMessage.Text = "Saved Successfully.";
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
try
{
ClearControls();
}
catch
{
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
try
{
string textdate;
ClearControls();
Button btn = sender as Button;
GridViewRow grow = btn.NamingContainer as GridViewRow;
hidCustomerID.Value = (grow.FindControl("lblCustomerID") as Label).Text;
txtCustomerName.Text = (grow.FindControl("lblCustomerName") as Label).Text;
txtPhoneNumber.Text = (grow.FindControl("lblPhoneNumber") as Label).Text;
txtAddress.Text = (grow.FindControl("lblAddress") as Label).Text;
textdate = (grow.FindControl("lblDOB") as Label).Text;
var i = rbMale.Checked;
var j = rbMale.Text;
if (rbMale.Checked == true)
{
rbMale.Checked = true;
}
else {
rbFemale.Checked = true;
}
datepicker.Value = textdate;
btnSave.Visible = false;
btnUpdate.Visible = true;
}
catch
{
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
String text = Page.Request.Form["datepickernm"];
// con = new SqlConnection(strConnString);
cmd = new SqlCommand();
cmd.CommandText = "update tblCustomers set CustomerName=@CustomerName,PhoneNumber=@PhoneNumber,Address=@Address,dob=@date where CustomerID=@CustomerID";
cmd.Parameters.AddWithValue("@CustomerName", txtCustomerName.Text);
cmd.Parameters.AddWithValue("@PhoneNumber", txtPhoneNumber.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@CustomerID", hidCustomerID.Value);
cmd.Parameters.AddWithValue("@date", text);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
ClearControls();
lblMessage.Text = "Updated Successfully.";
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
// con = new SqlConnection(strConnString);
ClearControls();
Button btn = sender as Button;
GridViewRow grow = btn.NamingContainer as GridViewRow;
cmd = new SqlCommand();
cmd.CommandText = "update tblCustomers set IsActive=0 where CustomerID=@CustomerID";
cmd.Parameters.AddWithValue("@CustomerID", (grow.FindControl("lblCustomerID") as Label).Text);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
lblMessage.Text = "Deleted Successfully.";
}
catch
{
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
}
}
client side coding
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewSample.aspx.cs" Inherits="hailsap.GridviewSample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta charset="utf-8" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function () {
$("#datepicker").datepicker();
});
</script>
<title>Gridview Basics example.</title>
<style type="text/css">
.gv
{
font-family: Arial;
margin-top: 30px;
font-size: 14px;
}
.gv th
{
background-color: #5D7B9D;
font-weight: bold;
color: #fff;
padding: 2px 10px;
}
.gv td
{
padding: 2px 10px;
}
input[type="submit"]
{
margin: 2px 10px;
padding: 2px 20px;
background-color: #5D7B9D;
border-radius: 10px;
border: solid 1px #000;
cursor: pointer;
color: #fff;
}
input[type="submit"]:hover
{
background-color: orange;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" style="position: relative; top: 20px;">
<tr>
<td>
<table align="center">
<tr>
<td>
Customer Name :
</td>
<td>
<asp:TextBox ID="txtCustomerName" runat="server" MaxLength="50" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Phone Number :
</td>
<td>
<asp:TextBox ID="txtPhoneNumber" runat="server" MaxLength="10" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address :
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" MaxLength="200" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Date :
</td>
<td>
<input type="text" runat="server" id="datepicker" name="datepickernm"/>
</td>
</tr>
<tr>
<td>
Gender :
</td>
<td>
<asp:RadioButton ID="rbMale" Text="Male" runat="server" GroupName="Gender" />
<asp:RadioButton ID="rbFemale" Text="Female" runat="server" GroupName="Gender" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click"
Visible="false" />
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<br />
<asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="Blue"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvDepartments" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True"
EmptyDataText="No Records Found" GridLines="both" CssClass="gv" EmptyDataRowStyle-ForeColor="Red">
<Columns>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%#Eval("CustomerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone Number">
<ItemTemplate>
<asp:Label ID="lblPhoneNumber" runat="server" Text='<%#Eval("PhoneNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Birth">
<ItemTemplate>
<asp:Label ID="lblDOB" runat="server" Text='<%#Eval("dob") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" OnClick="btnEdit_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure? want to delete the department.');"
OnClick="btnDelete_Click" />
<asp:Label ID="lblCustomerID" runat="server" Text='<%#Eval("CustomerID") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<input type="hidden" runat="server" id="hidCustomerID" />
</div>
</form>
</body>
</html>