Wednesday, 13 March 2013

insert,update,delete record in gridview using Datakey in asp.net




What is DataKey:-

When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control. The DataKey object contains the values of the field or fields specified in the DataKeyNames property. The DataKey objects are then added to the control's DataKeys collection. Use the DataKeys property to retrieve the DataKey object for a specific data row in the GridView control.

For Ex:-

            <asp:GridView ID="GridView1" runat="server" Font-Size="Large" ForeColor="#333333" ShowFooter="True" CellPadding="4" GridLines="None" AutoGenerateColumns="False" DataKeyNames="cid">

 string cid = GridView1.DataKeys[r1.RowIndex].Value.ToString();

The simple use of Datakey is Retrival of data From the Gridview. Through Datakey you can perform all operation like insert,update,Delete very Easily because you can get the key of each record before perform the operation.

            you can Bind,Insert,Update,Delete Record are performed in gridview not in DataBase. this Type of program is very useful when shopping a multiple Item in online shopping because of user can select and reject Item operation are not performed in database it is performed temprory in gridview.

Sorting Record in Gridview Sorting Record Based on Coloum name in Gridview

Handle Concurrency in Linq to SQL Concurrency example in .Net

AutoComplete TextBox Search Example in Ajax Autocomplete TextBox




 datakey.aspx:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datakey.aspx.cs" Inherits="datakey" %>

<!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 id="Head1" runat="server">

<link rel="stylesheet" type="text/css" href="styles.css">

<script language="javascript">

    function changeImg(valueTemp) {

        var a = document.getElementById("<%= img.ClientID %>");

        a.src = valueTemp.value;

    }

</script>

    <title></title>

  

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Panel ID="p" runat="server" BorderColor="#FFFF99" BorderStyle="Solid"

       GroupingText="Registration"  ScrollBars="Auto"

        >

  

  <center>

<label for="email">company_id</label>

   <asp:TextBox ID="txtcid" runat="server" CssClass="txtfield"

                    > </asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator3"

             runat="server"

          

             ControlToValidate="txtcid"

       

             ErrorMessage="Id can't be left blank"

             SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>

      <asp:CompareValidator ID="CompareValidator1" runat="server"

          ErrorMessage="only digit is allowed" ControlToValidate="txtcid" Display="Dynamic"

          Font-Bold="True" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>

    <label for="password">Company Name</label> <asp:TextBox ID="txtcnm" runat="server" CssClass="txtfield"> </asp:TextBox>

    <asp:RequiredFieldValidator ID="rfvFirstName"

                 runat="server"

                 ControlToValidate="txtcnm"

                ErrorMessage="company Name can't be left blank"

                Display="Dynamic"> </asp:RequiredFieldValidator>

 <label for="email">person_name</label>

    <asp:TextBox ID="txtpnm" runat="server" TextMode="MultiLine" CssClass="txtfield"></asp:TextBox>

    <asp:RequiredFieldValidator

             ID="RequiredFieldValidator1" runat="server"

             ControlToValidate="txtpnm"

             ErrorMessage="person name can't be left blank"

             Display="Dynamic"> </asp:RequiredFieldValidator>

     <label for="email">company phoneno</label>

   <asp:TextBox ID="txtcphno" runat="server" CssClass="txtfield"

                    > </asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator4"

             runat="server"

             ControlToValidate="txtcphno"

             ErrorMessage="phoneno can't be left blank"

             SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"

            ErrorMessage="only number is allowed and 6 digit number" ControlToValidate="txtcphno"

            Display="Dynamic" ValidationExpression="^[0-9]{6}"></asp:RegularExpressionValidator>

   <label for="email">mobile no</label>

   <asp:TextBox ID="txtccellno" runat="server" CssClass="txtfield"

                    > </asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator5"

             runat="server"

             ControlToValidate="txtccellno"

             ErrorMessage="cellno can't be left blank"

             SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"

            ErrorMessage="only number is allowed" ControlToValidate="txtccellno"

            Display="Dynamic" ValidationExpression="^[0-9]{10}"></asp:RegularExpressionValidator>

    <label for="email">Company_address</label>

    <asp:TextBox ID="txtcadd" runat="server" TextMode="MultiLine" CssClass="txtfield"></asp:TextBox>

    <asp:RequiredFieldValidator

             ID="RequiredFieldValidator2" runat="server"

             ControlToValidate="txtcadd"

             ErrorMessage="Address can't be left blank"

             Display="Dynamic"> </asp:RequiredFieldValidator>

      <asp:ScriptManager ID="ScriptManager1" runat="server">

      </asp:ScriptManager>

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">

     <ContentTemplate>

 <label for="email">state</label>

      <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="state"

          DataValueField="state" AutoPostBack="True" CssClass="txtfield"

          onselectedindexchanged="DropDownList1_SelectedIndexChanged">

      </asp:DropDownList>

       <label for="email">city</label>

      <asp:DropDownList ID="DropDownList2" runat="server" DataTextField="city"

          DataValueField="city" AutoPostBack="True" CssClass="txtfield">

      </asp:DropDownList>

      <br />

      <table  class="txtfield" >

      <tr>

      <td rowspan="2"><b>Gender</b></td>

      <td> <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"

          GroupName="abc" Text="male"  />

     <br />

      <asp:RadioButton ID="RadioButton2" runat="server" GroupName="abc"

          AutoPostBack="True" Text="female"/>

     </td>

      </tr>

      </table>

          </ContentTemplate>

          </asp:UpdatePanel>

    <label for="email">choose photo</label>

      <asp:FileUpload ID="FileUpload1" runat="server" onchange="changeImg(this)" CssClass="btn"/>

       <asp:Image ID="img" runat="server" AlternateText="aaa" style="position:absolute; left:650px;" Width="100" Height="50"  />

   <br /> <asp:Button id="Button1"

           Text="Register"

           CommandName="register"

           CommandArgument="register"

           OnCommand="Button1_Click1" CssClass="btn"

           runat="server"/>

           <asp:Button id="Button4"

           Text="clear"

       

      

            CssClass="btn"

           runat="server" CausesValidation="False" onclick="Button4_Click"/>

         

<asp:Button id="Button3"

           Text="Update"

           CommandName="update"

           CommandArgument="update"

           OnCommand="Button1_Click1" CssClass="btn" Visible="false"

           runat="server"/>

      

        <asp:GridView ID="GridView1" runat="server"

      

       Font-Size="Large"

        ForeColor="#333333" ShowFooter="True" CellPadding="4" GridLines="None"

           

              AutoGenerateColumns="False" DataKeyNames="cid"

         

          

              >

          

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

        <AlternatingRowStyle BackColor="White" />

        <Columns>

       <asp:TemplateField HeaderText="ID">

                <ItemTemplate>

                    <asp:Label ID="Label10" runat="server" Text='<%# Eval("cid") %>'></asp:Label>

                </ItemTemplate>

                 <EditItemTemplate>

                     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("cid") %>'></asp:TextBox>

                 </EditItemTemplate>

             </asp:TemplateField>

         

             <asp:TemplateField HeaderText="Company_name">

                <ItemTemplate>

                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("cname") %>'></asp:Label>

                </ItemTemplate>

                <EditItemTemplate>

                     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("cname") %>'></asp:TextBox>

                 </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Person_name">

                <ItemTemplate>

                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("pname") %>'></asp:Label>

                </ItemTemplate>

                <EditItemTemplate>

                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("pname") %>'></asp:TextBox>

                 </EditItemTemplate>

                </asp:TemplateField>

            <asp:TemplateField HeaderText="Phone_no">

                <ItemTemplate>

                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("cphoneno") %>'></asp:Label>

                </ItemTemplate>

                <EditItemTemplate>

                     <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("cphoneno") %>'></asp:TextBox>

                 </EditItemTemplate>

                </asp:TemplateField>

            <asp:TemplateField HeaderText="Mobile_no">

                <ItemTemplate>

                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("mobileno") %>'></asp:Label>

                </ItemTemplate>

                 <EditItemTemplate>

                     <asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("mobileno") %>'></asp:TextBox>

                 </EditItemTemplate>

             

                </asp:TemplateField>

            <asp:TemplateField HeaderText="Address">

                <ItemTemplate>

                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("caddress") %>'></asp:Label>

                </ItemTemplate>

                 <EditItemTemplate>

                     <asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("caddress") %>'></asp:TextBox>

                 </EditItemTemplate>

             

                </asp:TemplateField>

                 <asp:TemplateField HeaderText="state">

                <ItemTemplate>

                    <asp:Label ID="Label6" runat="server"></asp:Label>

                </ItemTemplate>

                </asp:TemplateField>

      

                <asp:TemplateField HeaderText="city">

                <ItemTemplate>

                    <asp:Label ID="Label7" runat="server"></asp:Label>

                </ItemTemplate>

                </asp:TemplateField>

            <asp:TemplateField HeaderText="Gender">

                <ItemTemplate>

                    <asp:Label ID="Label8" runat="server"></asp:Label>

                </ItemTemplate>

                </asp:TemplateField>

        <asp:TemplateField HeaderText="Image">

                <ItemTemplate>

                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>' Width="80" Height="80" />

                    <asp:Label ID="i1" runat="server" Visible="false" Text='<%# Eval("image") %>'></asp:Label>

                </ItemTemplate>

                </asp:TemplateField>

      

           <asp:TemplateField>

                <ItemTemplate>

            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="l1" CommandArgument='<%# Eval("cid") %>'   CausesValidation="False">Delete</asp:LinkButton>

            </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField>

            <ItemTemplate>

             <asp:LinkButton ID="LinkButton1" runat="server"  OnClick="l2" CommandArgument='<%# Eval("cid") %>' CausesValidation="False">Edit</asp:LinkButton>

            </ItemTemplate>

            </asp:TemplateField>

       

            </Columns>

         

    </asp:GridView>

        <asp:Button ID="Button2" runat="server" Text="submit" CssClass="btn" onclick="Button2_Click" />

        </center>

     </asp:Panel>



    </div>

    </form>

</body>

</html>




 datakey.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.SqlClient;

using System.Data;

using System.Web.UI.HtmlControls;

using System.Collections;

using System.IO;

using System.Web.Security;

using System.Web.UI.WebControls.WebParts;

public partial class datakey : System.Web.UI.Page
{

    DataTable dt;

    DataTable ds;

    string no = null;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {

            Session["dtd"] = null;

            fetchstate();

            if (Request.QueryString["id"] == null)

                city("gujarat");

        }

        dt = Session["dtd"] as DataTable;

        if (Request.QueryString["id"] != null)
        {

            ds = new DataTable();

            string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

            string q = "select * from hk_company where cid ='" + Request.QueryString["id"].ToString() + "'";

            SqlConnection conn = new SqlConnection(con);

            SqlCommand cmd = new SqlCommand(q, conn);

            SqlDataAdapter sa = new SqlDataAdapter();

            conn.Open();

            cmd.ExecuteNonQuery();

            sa.SelectCommand = cmd;

            sa.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataBind();

            dt = ds;

            //Session["dtd"] = dt;

            SqlDataReader sr = cmd.ExecuteReader();

            string st, ct, gt = null;

            int i = 0;

            while (sr.Read())
            {

                st = sr["state"].ToString();

                ct = sr["city"].ToString();

                gt = sr["gender"].ToString();

                Label l6 = (Label)GridView1.Rows[i].FindControl("Label6");

                Label l7 = (Label)GridView1.Rows[i].FindControl("Label7");

                Label l8 = (Label)GridView1.Rows[i].FindControl("Label8");

                l6.Text = DropDownList1.Items[int.Parse(st)].Value.ToString();

                //DropDownList1.SelectedValue = l6.Text;

                city(l6.Text);

                l7.Text = DropDownList2.Items[int.Parse(ct)].Value.ToString();

                switch (gt)
                {

                    case ("1"):

                        l8.Text = "male";

                        break;

                    case ("2"):

                        l8.Text = "female";

                        break;

                }

                l6.DataBind();

                l7.DataBind();

                l8.DataBind();

                i++;

            }

        }

    }

    public void fetchstate()
    {

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

        string se = "select distinct state from tempstate";

        SqlConnection conn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(se, conn);

        DataSet ds = new DataSet();

        SqlDataAdapter sa = new SqlDataAdapter();

        conn.Open();

        sa.SelectCommand = cmd;

        sa.Fill(ds);

        DropDownList1.DataSource = ds;

        DropDownList1.DataBind();

        conn.Close();

    }

    public void city(string a)
    {

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

        string se = "select city from tempstate where state ='" + a + "'";

        SqlConnection conn = new SqlConnection(con);

        SqlCommand cmd = new SqlCommand(se, conn);

        DataSet ds = new DataSet();

        SqlDataAdapter sa = new SqlDataAdapter();

        conn.Open();

        sa.SelectCommand = cmd;

        sa.Fill(ds);

        DropDownList2.DataSource = ds;

        DropDownList2.DataBind();

        conn.Close();

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        city(DropDownList1.SelectedValue);

    }

    protected void Button1_Click1(object sender, CommandEventArgs e)
    {

        if (e.CommandName == "register")
        {

            if (FileUpload1.HasFile)
            {

                string fileExtension = Path.GetExtension(FileUpload1.FileName.ToString());

                fileExtension.ToLower();

                if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".png")
                {

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "ke", "<script>alert('only jpeg,jpg,png,gif photo is allowed');</script>");

                    return;

                }

                string serverPath = Server.MapPath(@"~/" + FileUpload1.FileName);

                FileUpload1.SaveAs(serverPath);

            }

            else
            {

                ClientScript.RegisterClientScriptBlock(this.GetType(), "ke", "<script>alert('plz browse the image');</script>");

                FileUpload1.Focus();

                return;

            }

            if (dt == null)
            {

                dt = new DataTable();

                dt.Columns.Add("cid").ToString();

                dt.Columns.Add("cname").ToString();

                dt.Columns.Add("pname").ToString();

                dt.Columns.Add("cphoneno").ToString();

                dt.Columns.Add("mobileno").ToString();

                dt.Columns.Add("caddress").ToString();

                dt.Columns.Add("state").ToString();

                dt.Columns.Add("city").ToString();

                dt.Columns.Add("gender").ToString();

                dt.Columns.Add("image").ToString();

                // GridView1.DataKeyNames = new string[] {"cid"};

            }

            // DataTable dt = (DataTable)Session["temp"];

            if (RadioButton1.Checked)

                no = "male";

            else

                no = "female";

            DataRow dr = dt.NewRow();

            dr["cid"] = txtcid.Text;

            dr["cname"] = txtcnm.Text;

            dr["pname"] = txtpnm.Text;

            dr["cphoneno"] = txtcphno.Text;

            dr["mobileno"] = txtccellno.Text;

            dr["caddress"] = txtcadd.Text;

            dr["state"] = DropDownList1.SelectedValue;

            dr["city"] = DropDownList2.SelectedValue;

            dr["gender"] = no;

            dr["image"] = FileUpload1.FileName;

            dt.Rows.Add(dr);

            GridView1.DataSource = dt;

            int t = GridView1.Rows.Count;

            GridView1.DataBind();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                Label l6 = (Label)GridView1.Rows[i].FindControl("Label6");

                Label l7 = (Label)GridView1.Rows[i].FindControl("Label7");

                Label l8 = (Label)GridView1.Rows[i].FindControl("Label8");

                l6.Text = dt.Rows[i][6].ToString();

                l7.Text = dt.Rows[i][7].ToString();

                l8.Text = dt.Rows[i][8].ToString();

                l6.DataBind();

                l7.DataBind();

                l8.DataBind();

            }

            Session["dtd"] = dt;

            RadioButton1.Checked = false;

            RadioButton2.Checked = false;

        }

        if (e.CommandName == "update")
        {

            if (FileUpload1.HasFile)
            {

                string fileExtension = Path.GetExtension(FileUpload1.FileName.ToString());

                fileExtension.ToLower();

                if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".png")
                {

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "ke", "<script>alert('only jpeg,jpg,png,gif photo is allowed');</script>");

                    return;

                }

                string serverPath = Server.MapPath(@"~/" + FileUpload1.FileName);

                FileUpload1.SaveAs(serverPath);

            }

            /* else

             {

                 ClientScript.RegisterClientScriptBlock(this.GetType(), "ke", "<script>alert('plz browse the image');</script>");

                 FileUpload1.Focus();

                 return;

             }*/

            // DataTable dt = (DataTable)Session["temp"];

            if (RadioButton1.Checked)

                no = "male";

            else

                no = "female";

            foreach (GridViewRow r1 in GridView1.Rows)
            {

                if (GridView1.DataKeys[r1.RowIndex].Value.ToString().Equals(Session["id"].ToString()))
                {

                    Label l = (Label)GridView1.Rows[r1.RowIndex].FindControl("i1");

                    dt.Rows[r1.RowIndex][0] = txtcid.Text;

                    dt.Rows[r1.RowIndex][1] = txtcnm.Text;

                    dt.Rows[r1.RowIndex][2] = txtpnm.Text;

                    dt.Rows[r1.RowIndex][3] = txtcphno.Text;

                    dt.Rows[r1.RowIndex][4] = txtccellno.Text;

                    dt.Rows[r1.RowIndex][5] = txtcadd.Text;

                    dt.Rows[r1.RowIndex][6] = DropDownList1.SelectedValue;

                    dt.Rows[r1.RowIndex][7] = DropDownList2.SelectedValue;

                    dt.Rows[r1.RowIndex][8] = no;

                    if (FileUpload1.HasFile)
                    {

                        dt.Rows[r1.RowIndex][9] = FileUpload1.FileName;

                        Session["im"] = FileUpload1.FileName;

                    }

                    else
                    {

                        dt.Rows[r1.RowIndex][9] = l.Text;

                        Session["im"] = l.Text;

                    }

                    if (no == "male")

                        Session["gn"] = "1";

                    else

                        Session["gn"] = "2";

                    break;

                }

            }

            GridView1.DataSource = dt;

            GridView1.DataBind();

            Button1.Visible = true;

            Button3.Visible = false;

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                Label l6 = (Label)GridView1.Rows[i].FindControl("Label6");

                Label l7 = (Label)GridView1.Rows[i].FindControl("Label7");

                Label l8 = (Label)GridView1.Rows[i].FindControl("Label8");

                l6.Text = dt.Rows[i][6].ToString();

                l7.Text = dt.Rows[i][7].ToString();

                l8.Text = dt.Rows[i][8].ToString();

                l6.DataBind();

                l7.DataBind();

                l8.DataBind();

            }

            Session["dtd"] = dt;

            RadioButton1.Checked = false;

            RadioButton2.Checked = false;

            ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "<script> alert('updated successfully');</script>");

        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        int j = 0;

        if (Request.QueryString["id"] == null)
        {

            string n = null;

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                string st = dt.Rows[i][6].ToString();

                string ct = dt.Rows[i][7].ToString();

                string gt = dt.Rows[i][8].ToString();

                if (gt.Equals("male"))

                    n = "1";

                else

                    n = "2";

                string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

                string se = "insert into hk_company values('" + dt.Rows[i][0].ToString() + "','" + dt.Rows[i][1].ToString() + "','" + dt.Rows[i][2].ToString() + "','" + dt.Rows[i][3].ToString() + "','" + dt.Rows[i][4].ToString() + "','" + dt.Rows[i][5].ToString() + "','" + DropDownList1.Items.IndexOf(new ListItem(st)).ToString() + "','" + DropDownList2.Items.IndexOf(new ListItem(ct)).ToString() + "','" + n + "','" + dt.Rows[i][9].ToString() + "')";

                SqlConnection conn = new SqlConnection(con);

                SqlCommand cmd = new SqlCommand(se, conn);

                conn.Open();

                j = cmd.ExecuteNonQuery();

                conn.Close();

            }

            if (j > 0)

                Response.Redirect("show4.aspx");

        }

        else
        {

            /* string fileExtension = Path.GetExtension(FileUpload1.FileName.ToString());

             fileExtension.ToLower();

             if (fileExtension != ".gif" && fileExtension != ".jpg" && fileExtension != ".jpeg" && fileExtension != ".png")

             {

                 ClientScript.RegisterClientScriptBlock(this.GetType(), "ke", "<script>alert('only jpeg,jpg,png,gif photo is allowed');</script>");

                 return;

             }

             string serverPath = Server.MapPath(@"~/" + FileUpload1.FileName);

             FileUpload1.SaveAs(serverPath);*/

            string gind = dt.Rows[0][8].ToString();

            string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";

            string se = "update hk_company set cname='" + txtcnm.Text + "',pname='" + txtpnm.Text + "',cphoneno='" + txtcphno.Text + "',mobileno='" + txtccellno.Text + "',caddress='" + txtcadd.Text + "',state='" + DropDownList1.SelectedIndex + "',city='" + DropDownList2.SelectedIndex + "',gender='" + Session["gn"].ToString() + "',image='" + Session["im"].ToString() + "' where cid='" + txtcid.Text + "'";

            SqlConnection conn = new SqlConnection(con);

            SqlCommand cmd = new SqlCommand(se, conn);

            conn.Open();

            j = cmd.ExecuteNonQuery();

            if (j > 0)

                Response.Redirect("show4.aspx");

            conn.Close();

        }

    }

    protected void l2(object sender, EventArgs e)
    {

        LinkButton l = (LinkButton)sender;

        foreach (GridViewRow r1 in GridView1.Rows)
        {

            string cid = GridView1.DataKeys[r1.RowIndex].Value.ToString();

            if (cid.Equals(l.CommandArgument.ToString()))
            {

                Label l1 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label10");

                Label l2 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label1");

                Label l3 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label2");

                Label l4 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label3");

                Label l5 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label4");

                Label l6 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label5");

                Label l7 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label6");

                Label l8 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label7");

                Label l9 = (Label)GridView1.Rows[r1.RowIndex].FindControl("Label8");

                Session["id"] = l1.Text;

                txtcid.Text = l1.Text;

                txtcid.ReadOnly = true;

                txtcnm.Text = l2.Text;

                txtpnm.Text = l3.Text;

                txtcphno.Text = l4.Text;

                txtccellno.Text = l5.Text;

                txtcadd.Text = l6.Text;

                DropDownList1.SelectedValue = l7.Text;

                city(l7.Text);

                DropDownList2.SelectedValue = l8.Text;

                if (l9.Text.Equals("male"))
                {

                    RadioButton1.Checked = true;

                }

                else
                {

                    RadioButton2.Checked = true;

                }

                Button3.Visible = true;

                Button1.Visible = false;

                break;

            }

        }

    }

    protected void l1(object sender, EventArgs e)
    {

        LinkButton l = (LinkButton)sender;

        foreach (GridViewRow r1 in GridView1.Rows)
        {

            string cid = GridView1.DataKeys[r1.RowIndex].Value.ToString();

            if (cid.Equals(l.CommandArgument.ToString()))
            {

                dt.Rows.RemoveAt(r1.RowIndex);

                break;

            }

        }

        GridView1.DataSource = dt;

        GridView1.DataBind();

    }



    protected void Button4_Click(object sender, EventArgs e)
    {

        txtcid.Text = "";

        txtcnm.Text = "";

        txtpnm.Text = "";

        txtcphno.Text = "";

        txtccellno.Text = "";

        txtcadd.Text = "";

        //DropDownList1.SelectedIndex = 0;

    }



}



0 comments:

Post a Comment