Saturday 8 February 2014

Insert, Edit, Update, Delete data in Gridview and DataBinding DropDownList Inside GridView In ASP .NET Using C#

Hello, I will explain  Insert,Update,Edit,Delete operation in Gridview and also Bind DropDownList Inside GridView .

  1. Open Visual Studio
  2. Create a new Empty Web Site
  3. Add new item>WebForm>name it(GridOperation.aspx)

I have created one aspx page with One gridview control to insert,update,edit,delete data and DataBinding DropDownList.
I have three dropdown inside gridview for bind data Country,State,City . for bind country,state,city dropdownlist I need to populate state dropdown based on country dropdown and I need to populate region dropdown based on state dropdown for what I have created three table of country,state,city tables in sql server that shown in below.

County table> Name it dashrath_Country


 State table> Name it dashrath_State


City table> Name it dashrath_city

After design the aspx page.
HTML source of page  GridOperation.aspx
GridOperation.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridOperation.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="background-color: Silver">
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"
            AllowPaging="True" BackColor="White" BorderColor="#999999" BorderStyle="None"
            BorderWidth="1px" CellPadding="3" GridLines="Vertical" OnRowCommand="GridView1_RowCommand"
            Caption="GRIDVIEW EXAMPLE" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Height="348px"
            ShowFooter="True" Width="767px">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:TemplateField HeaderText="EMP_ID" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblemp_id" runat="server" Text='<%#Eval("emp_id") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="lbl_edit" runat="server" Text='<%# Eval("emp_id") %>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="FirstName">
                    <ItemTemplate>
                        <asp:Label ID="lblfname" runat="server" Text='<%#Eval("fname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_fname" runat="server" Text='<%# Eval("fname") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="txt_fname"
                            ErrorMessage="*" ValidationGroup="update1"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtf_fname" runat="server" Width="128px"></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server" ControlToValidate="txtf_fname"
                            ErrorMessage="*" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="LastName">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%#Eval("lname") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_lname" runat="server" Text='<%# Eval("lname") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_lname"
                            ErrorMessage="*" ValidationGroup="update1"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtf_lname" runat="server"></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtf_lname"
                            ErrorMessage="*" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Address">
                    <ItemTemplate>
                        <asp:Label ID="lbladdress" runat="server" Text='<%#Eval("address") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_address" runat="server" Text='<%# Eval("address") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="txt_address"
                            ErrorMessage="*" ValidationGroup="update1"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtf_address" runat="server"></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtf_address"
                            ErrorMessage="*" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblcon" runat="server" Text='<%#Eval("country1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="lblcountry" runat="server" Text='<%#Eval("country") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="drp_country" runat="server" OnSelectedIndexChanged="drp_country_SelectedIndexChanged"
                            AutoPostBack="true">
                        </asp:DropDownList>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="drp_country"
                            ErrorMessage="*" InitialValue="0" ValidationGroup="update1"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="footer_country" runat="server" AutoPostBack="True" OnSelectedIndexChanged="footer_country_SelectedIndexChanged">
                        </asp:DropDownList>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="footer_country"
                            ErrorMessage="*" InitialValue="0" ValidationGroup="submit"></asp:RequiredFieldValidator>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" ControlToValidate="footer_country"
                            ErrorMessage="*" InitialValue="0" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblstcon" runat="server" Text='<%#Eval("state1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblcity" runat="server" Text='<%#Eval("city") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="drp_city" runat="server" AutoPostBack="true">
                        </asp:DropDownList>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ControlToValidate="drp_city"
                            ErrorMessage="*" ValidationGroup="update1"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="footer_city" runat="server">
                        </asp:DropDownList>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="footer_city"
                            ErrorMessage="*" InitialValue="0" ValidationGroup="submit"></asp:RequiredFieldValidator>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ControlToValidate="footer_city"
                            ErrorMessage="*" InitialValue="0" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ContactNo">
                    <ItemTemplate>
                        <asp:Label ID="lblcontactno" runat="server" Text='<%#Eval("contactno") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_contactno" runat="server" Text='<%# Eval("contactno") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_contactno"
                            ErrorMessage="*" ValidationExpression="\d{10}" ValidationGroup="update1"></asp:RegularExpressionValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtf_contactno" runat="server"></asp:TextBox>
                        &nbsp;
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtf_contactno"
                            ErrorMessage="*" ValidationExpression="\d{10}" ValidationGroup="submit"></asp:RegularExpressionValidator>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server" ControlToValidate="txtf_contactno"
                            ErrorMessage="*" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email_ID">
                    <ItemTemplate>
                        <asp:Label ID="lblemailid" runat="server" Text='<%#Eval("emailid") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txt_id" runat="server" Text='<%# Eval("emailid") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txt_id"
                            ErrorMessage="*" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                            ValidationGroup="update1"></asp:RegularExpressionValidator>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtf_emailid" runat="server"></asp:TextBox>
                        &nbsp;
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtf_emailid"
                            ErrorMessage="*" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                            ValidationGroup="submit"></asp:RegularExpressionValidator>
                        &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server" ControlToValidate="txtf_emailid"
                            ErrorMessage="*" ValidationGroup="submit"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="deleteitem" CommandArgument='<%# Eval("emp_id") %>'
                            CausesValidation="False">Delete</asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="btn_add" runat="server" Text="Insert" OnClick="btn_add_Click" ValidationGroup="submit" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit IN Gridview">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="edit" ValidationGroup="update">Edit</asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton4" runat="server" CommandName="update" ValidationGroup="update1">update</asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton5" runat="server" CommandName="cancel">cencel</asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#DCDCDC" />
        </asp:GridView>
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    </form>
</body>
</html>
C# code of GridOperation.aspx.cs
GridOperation.aspx.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;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["city-state-country"].ConnectionString.ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
            drpfooter_country();
        }
    }
    public void bind()
    {
        SqlCommand cmd = new SqlCommand("select e1.emp_id,e1.fname,e1.lname,e1.address,e1.country as country1, c1.country,e1.state as state1,e1.city as city1,s1.state,c2.city, e1.contactno,e1.emailid from dasharth_employee as e1,dashrath_Country as c1,dashrath_State as s1,dashrath_city as c2 where e1.country=c1.country_id and e1.state=s1.state_id and e1.city=c2.city_id order by e1.emp_id asc");
        cmd.Connection = con;
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
        {
            DropDownList ddlcountry = (DropDownList)e.Row.FindControl("drp_country");
            DropDownList ddlstate = (DropDownList)e.Row.FindControl("drp_state");
            DropDownList ddlcity = (DropDownList)e.Row.FindControl("drp_city");
            Label ddlcon = (Label)e.Row.FindControl("lblcon");
            Label ddlcon1 = (Label)e.Row.FindControl("lblstcon");
            Label ddlcon2 = (Label)e.Row.FindControl("lblcty");
            int con1 = Convert.ToInt32(ddlcon.Text);
            int con2 = Convert.ToInt32(ddlcon1.Text);
            DataTable dt = new DataTable();
            string sql = "select * From dashrath_Country";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            ddlcountry.DataSource = dt;
            ddlcountry.DataTextField = "country";
            ddlcountry.DataValueField = "country_id";
            ddlcountry.DataBind();
            ddlcountry.SelectedValue = ddlcon.Text;
            ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
            ddlstate.Items.Insert(0, new ListItem("--select--", "0"));
       
            DataTable dt1 = new DataTable();
            string sql1 = "select * from dashrath_State where country_id='" + con1 + "'";
            SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
            da1.Fill(dt1);
            ddlstate.DataSource = dt1;
            ddlstate.DataTextField = "state";
            ddlstate.DataValueField = "state_id";
            ddlstate.DataBind();
            ddlstate.SelectedValue = ddlcon1.Text;
            DataTable dt2 = new DataTable();
            string sql2 = "select * from dashrath_city where state_id='" + con2 + "'";
            SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);
            da2.Fill(dt2);
            ddlcity.DataSource = dt2;
            ddlcity.DataTextField = "city";
            ddlcity.DataValueField = "city_id";
            ddlcity.DataBind();
            ddlcity.SelectedValue = ddlcon2.Text;
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
        drpfooter_country();
    }
    protected void drp_country_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drpcon = (DropDownList)sender;
        GridViewRow gr = (GridViewRow)drpcon.NamingContainer;
        DropDownList ddcon = (DropDownList)gr.FindControl("drp_country");
        DropDownList ddst = (DropDownList)gr.FindControl("drp_state");
        DropDownList ddcct = (DropDownList)gr.FindControl("drp_city");
        DataTable dt = new DataTable();
        string sql = "select * from dashrath_State where country_id='" + ddcon.SelectedValue + "'";
        SqlDataAdapter adp = new SqlDataAdapter(sql, con);
        adp.Fill(dt);
        ddst.DataSource = dt;
        ddst.DataTextField = "state";
        ddst.DataValueField = "state_id";
        ddst.DataBind();
        ddst.Items.Insert(0, new ListItem("--select--", "0"));
        ddcct.Items.Clear();
        ddcct.Items.Insert(0, new ListItem("--select--", "0"));
    }
    protected void drp_state_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drlst = (DropDownList)sender;
        GridViewRow grc = (GridViewRow)drlst.NamingContainer;
        DropDownList ddst = (DropDownList)grc.FindControl("drp_state");
        DropDownList drct = (DropDownList)grc.FindControl("drp_city");
        DataTable dt = new DataTable();
        string sql = "select * from dashrath_city where state_id='" + ddst.SelectedValue + "'";
        SqlDataAdapter adp = new SqlDataAdapter(sql, con);
        adp.Fill(dt);
        drct.DataSource = dt;
        drct.DataTextField = "city";
        drct.DataValueField = "city_id";
        drct.DataBind();
        drct.Items.Insert(0, new ListItem("--select--", "0"));
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            drpfooter_country();
            con.Close();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteitem")
        {
            int id2 = Convert.ToInt32(e.CommandArgument);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Connection.Open();
            cmd.CommandText = "usp_delete";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@emp_id", id2);
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            bind();
            drpfooter_country();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.PageIndex = e.NewPageIndex;
        bind();
        drpfooter_country();
        Label1.Visible = false;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label id = ((Label)GridView1.Rows[e.RowIndex].FindControl("lbl_edit"));
        int id2 = Convert.ToInt32(id.Text);
        TextBox txfname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_fname");
        TextBox txlname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_lname");
        TextBox txaddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_address");
        DropDownList drcountry = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp_country");
        DropDownList drstate = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp_state");
        DropDownList drcity = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp_city");
        TextBox txcontactno = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_contactno");
        TextBox txemailid = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_id");
        SqlCommand cmd1 = new SqlCommand("update dasharth_employee set fname='" + txfname.Text + "',lname='" + txlname.Text + "',address='" + txaddress.Text + "',country='" + drcountry.SelectedValue + "',state='" + drstate.SelectedValue + "',city='" + drcity.SelectedValue + "',contactno='" + txcontactno.Text + "',emailid='" + txemailid.Text + "' where emp_id= " + id2, con);
        cmd1.Connection = con;
        cmd1.Connection.Open();
        cmd1.ExecuteNonQuery();
        cmd1.Connection.Close();
        GridView1.EditIndex = -1;
        bind();
        drpfooter_country();
        Label1.Text = txfname.Text + "" + "updated successfully";
        Label1.Visible = true;
    }
    public void drpfooter_country()
    {
        GridViewRow grd1 = (GridViewRow)GridView1.FooterRow;
        DropDownList drplstc = (DropDownList)grd1.FindControl("footer_country");
        DropDownList ddlstate = (DropDownList)grd1.FindControl("footer_state");
        DropDownList ddlcity = (DropDownList)grd1.FindControl("footer_city");
        DataTable dt = new DataTable();
        string sql = "select * From dashrath_Country";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        da.Fill(dt);
        drplstc.DataSource = dt;
        drplstc.DataTextField = "country";
        drplstc.DataValueField = "country_id";
        drplstc.DataBind();
        drplstc.Items.Insert(0, new ListItem("--Select--", "0"));
        DataTable dt1 = new DataTable();
        string sql2 = "select * from dashrath_State ";
        SqlDataAdapter da1 = new SqlDataAdapter(sql2, con);
        da1.Fill(dt1);
        ddlstate.DataSource = dt1;
        ddlstate.DataTextField = "state";
        ddlstate.DataValueField = "state_id";
        ddlstate.DataBind();
        ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
        ddlcity.Items.Insert(0, new ListItem("--select--", "0"));
    }
    protected void footer_country_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drpfoocon = (DropDownList)sender;
        GridViewRow gr = (GridViewRow)drpfoocon.NamingContainer;
        DropDownList ddconf_country = (DropDownList)gr.FindControl("footer_country");
        DropDownList ddstf_state = (DropDownList)gr.FindControl("footer_state");
        DropDownList ddfooter_ct = (DropDownList)gr.FindControl("footer_city");
        DataTable dt = new DataTable();
        string sql = "select * from dashrath_State where country_id='" + ddconf_country.SelectedValue + "'";
        SqlDataAdapter adp = new SqlDataAdapter(sql, con);
        adp.Fill(dt);
        ddstf_state.DataSource = dt;
        ddstf_state.DataTextField = "state";
        ddstf_state.DataValueField = "state_id";
        ddstf_state.DataBind();
        ddstf_state.Items.Insert(0, new ListItem("--select--", "0"));
        ddfooter_ct.Items.Clear();
        ddfooter_ct.Items.Insert(0, new ListItem("--select--", "0"));  
    }
    protected void footer_state_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drrlst = (DropDownList)sender;
        GridViewRow grc = (GridViewRow)drrlst.NamingContainer;
        DropDownList ddfooter_st = (DropDownList)grc.FindControl("footer_state");
        DropDownList ddfooter_ct = (DropDownList)grc.FindControl("footer_city");
        DataTable dt = new DataTable();
        string sql = "select * from dashrath_city where state_id='" + ddfooter_st.SelectedValue + "'";
        SqlDataAdapter adp = new SqlDataAdapter(sql, con);
        adp.Fill(dt);
        ddfooter_ct.DataSource = dt;
        ddfooter_ct.DataTextField = "city";
        ddfooter_ct.DataValueField = "city_id";
        ddfooter_ct.DataBind();
        ddfooter_ct.Items.Insert(0, new ListItem("--select--", "0"));
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        GridViewRow grd = (GridViewRow)GridView1.FooterRow;
        TextBox txtfname_foo = (TextBox)grd.FindControl("txtf_fname");
        TextBox txtlname_foo = (TextBox)grd.FindControl("txtf_lname");
        TextBox txtaddress_foo = (TextBox)grd.FindControl("txtf_address");
        DropDownList ddrcountry_foo = (DropDownList)grd.FindControl("footer_country");
        DropDownList ddrstate_foo = (DropDownList)grd.FindControl("footer_state");
        DropDownList ddrcity_foo = (DropDownList)grd.FindControl("footer_city");
        TextBox txtcontact_foo = (TextBox)grd.FindControl("txtf_contactno");
        TextBox txtemail_foo = (TextBox)grd.FindControl("txtf_emailid");
        SqlDataAdapter da = new SqlDataAdapter("select fname,lname from dasharth_employee where fname='" + txtfname_foo.Text + "' and lname='" + txtlname_foo.Text + "' ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Label1.Text = txtfname_foo.Text + "" + "Already Exist..Try Another Name";
            Label1.Visible = true;
            txtfname_foo.Text = "";
            txtlname_foo.Text = "";
            txtaddress_foo.Text = "";
            txtcontact_foo.Text = "";
            txtemail_foo.Text = "";
        }
        else
        {
            SqlCommand cmd = new SqlCommand("insert into dasharth_employee(fname,lname,address,country,state,city,contactno,emailid) values('" + txtfname_foo.Text + "','" + txtlname_foo.Text + "','" + txtaddress_foo.Text + "','" + ddrcountry_foo.SelectedValue + "','" + ddrstate_foo.SelectedValue + "','" + ddrcity_foo.SelectedValue + "','" + txtcontact_foo.Text + "','" + txtemail_foo.Text + "') ", con);
            cmd.Connection = con;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            bind();
            Label1.Text = txtfname_foo.Text + "" + "inserted succesfully";
            Label1.Visible = true;
            drpfooter_country();
        }
    }
}
Run the application and see the output as below.





0 comments:

Post a Comment

Popular Posts

Recent Posts

Sample Text

Stats

Powered by Blogger.

Popular Posts

Popular Posts

Join US on Facebook