how to insert ,update,delete and show data in gridview
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="Test29317.Employee" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ajax:ToolkitScriptManager ID="kk" runat="server"></ajax:ToolkitScriptManager>
<div>
<table>
<tr>
<td>Name :</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gender :</td>
<td><asp:RadioButtonList ID="rblgender" runat="server" RepeatColumns="3">
<asp:ListItem Text="male" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="female" Value="2"></asp:ListItem>
<asp:ListItem Text="others" Value="3"></asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td>Country :</td>
<td><asp:DropDownList ID="ddlcountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td>State :</td>
<td><asp:DropDownList ID="ddlstate" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td>Hobbies :</td>
<td><asp:CheckBoxList ID="cblhobbies" runat="server" RepeatColumns="4">
<asp:ListItem Text="cricket" Value="1"></asp:ListItem>
<asp:ListItem Text="football" Value="2"></asp:ListItem>
<asp:ListItem Text="music" Value="3"></asp:ListItem>
<asp:ListItem Text="movies" Value="4"></asp:ListItem>
<asp:ListItem Text="badminton" Value="5"></asp:ListItem>
<asp:ListItem Text="chess" Value="6"></asp:ListItem>
<asp:ListItem Text="dancing" Value="7"></asp:ListItem>
<asp:ListItem Text="dangal" Value="8"></asp:ListItem>
</asp:CheckBoxList></td>
</tr>
<tr>
<td>Files :</td>
<td><asp:FileUpload ID="fufiles" runat="server"></asp:FileUpload></td>
</tr>
<tr>
<td>Date of Birth :</td>
<td><asp:TextBox ID="txtdob" runat="server"></asp:TextBox>
<ajax:CalendarExtender ID="call" runat="server" PopupButtonID="txtdob" PopupPosition="BottomRight" TargetControlID="txtdob"></ajax:CalendarExtender>
</td>
</tr>
<tr>
<td>IsActive :</td>
<td><asp:CheckBox ID="chkisactive" runat="server"></asp:CheckBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td>
</tr>
<tr>
<td></td>
<td><asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<%#Eval("gender").ToString()=="1"?"male":Eval("gender").ToString()=="2"?"female":"others" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<%#Eval("cname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<%#Eval("sname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hobbies">
<ItemTemplate>
<%#Eval("Hobbies") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="date of birth">
<ItemTemplate>
<%#Convert.ToDateTime(Eval("dob").ToString()).ToShortDateString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Files">
<ItemTemplate>
<asp:Image ID="img" runat="server" ImageUrl='<%#Eval("files","~/uploads/{0}") %>' Width="80px" Height="60px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<%#Eval("isactive").ToString()=="1"?"yes":"no" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" runat="server" Text="Edit" CommandArgument='<%#Eval("empid") %>' CommandName="EDT"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtndelet" runat="server" Text="Delete" CommandArgument='<%#Eval("empid") %>' CommandName="Del"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</div>
</form>
</body>
</html>
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;
using System.IO;
namespace Test29317
{
public partial class Employee : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Fill_Country();
ddlstate.Items.Insert(0, new ListItem("--Select State--", "0"));
Fill_Grid();
}
}
public void Fill_Grid()
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_emp_country_state_select", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
grd.DataSource = ds;
grd.DataBind();
}
con.Close();
}
public void Fill_Country()
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_country_select", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
ddlcountry.DataValueField = "cid";
ddlcountry.DataTextField = "cname";
ddlcountry.DataSource = ds;
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select Country--", "0"));
}
con.Close();
}
protected void btnsave_Click(object sender, EventArgs e)
{
string HOB = "";
for (int i = 0; i < cblhobbies.Items.Count; i++)
{
if (cblhobbies.Items[i].Selected == true)
{
HOB += cblhobbies.Items[i].Text + ",";
}
}
HOB = HOB.TrimEnd(',');
string FN = "";
FN = Path.GetFileName(fufiles.PostedFile.FileName);
if (btnsave.Text == "Save")
{
fufiles.SaveAs(Server.MapPath("uploads" + "\\" + FN));
con.Open();
SqlCommand cmd = new SqlCommand("usp_emp_insert_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("empid", 0);
cmd.Parameters.AddWithValue("name", txtname.Text);
cmd.Parameters.AddWithValue("gender", rblgender.SelectedValue);
cmd.Parameters.AddWithValue("country", ddlcountry.SelectedValue);
cmd.Parameters.AddWithValue("state", ddlstate.SelectedValue);
cmd.Parameters.AddWithValue("hobbies", HOB);
cmd.Parameters.AddWithValue("dob", txtdob.Text);
cmd.Parameters.AddWithValue("files", DateTime.Now.Ticks.ToString() + FN);
cmd.Parameters.AddWithValue("isactive", chkisactive.Checked == true ? 1 : 0);
cmd.ExecuteNonQuery();
con.Close();
}
else
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_emp_insert_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("empid", ViewState["ID"]);
cmd.Parameters.AddWithValue("name", txtname.Text);
cmd.Parameters.AddWithValue("gender", rblgender.SelectedValue);
cmd.Parameters.AddWithValue("country", ddlcountry.SelectedValue);
cmd.Parameters.AddWithValue("state", ddlstate.SelectedValue);
cmd.Parameters.AddWithValue("hobbies", HOB);
cmd.Parameters.AddWithValue("dob", txtdob.Text);
if (FN != "")
{
FN = DateTime.Now.Ticks.ToString() + FN;
cmd.Parameters.AddWithValue("files", FN);
File.Delete(Server.MapPath("uploads" + "\\" + ViewState["FL"]));
fufiles.SaveAs(Server.MapPath("uploads" + "\\" + FN));
}
else
{
cmd.Parameters.AddWithValue("files", ViewState["FL"]);
}
cmd.Parameters.AddWithValue("isactive", chkisactive.Checked == true ? 1 : 0);
cmd.ExecuteNonQuery();
con.Close();
}
Fill_Grid();
}
public void Fill_State_by_country(int CIDD)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("usp_state_select", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter = "cid=" + CIDD;
ddlstate.DataValueField = "sid";
ddlstate.DataTextField = "sname";
ddlstate.DataSource = dv;
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("--Select State--", "0"));
}
con.Close();
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
Fill_State_by_country(int.Parse(ddlcountry.SelectedValue));
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EDT")
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_emp_edit", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@eid", e.CommandArgument);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
txtname.Text = ds.Tables[0].Rows[0]["name"].ToString();
rblgender.SelectedValue = ds.Tables[0].Rows[0]["gender"].ToString();
txtdob.Text = ds.Tables[0].Rows[0]["dob"].ToString();
ddlcountry.SelectedValue = ds.Tables[0].Rows[0]["country"].ToString();
Fill_State_by_country(int.Parse(ds.Tables[0].Rows[0]["country"].ToString()));
ddlstate.SelectedValue = ds.Tables[0].Rows[0]["state"].ToString();
if (ds.Tables[0].Rows[0]["isactive"].ToString() == "1")
{
chkisactive.Checked = true;
}
else
{
chkisactive.Checked = false;
}
string[] arr = ds.Tables[0].Rows[0]["hobbies"].ToString().Split(',');
cblhobbies.ClearSelection();
for (int i = 0; i < cblhobbies.Items.Count; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (cblhobbies.Items[i].Text == arr[j])
{
cblhobbies.Items[i].Selected = true;
break;
}
}
}
ViewState["FL"] = ds.Tables[0].Rows[0]["files"].ToString();
btnsave.Text = "Update";
ViewState["ID"] = e.CommandArgument;
con.Close();
}
}
else if (e.CommandName == "Del")
{
con.Open();
SqlCommand cmd = new SqlCommand("usp_emp_delete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@empid", e.CommandArgument);
cmd.ExecuteNonQuery();
con.Close();
Fill_Grid();
}
}
}
}