Wednesday 13 March 2013

Naming Container Example for Insert,Update,Delete record in Gridview using NamingContainer in asp.net




 What is Naming Container:-      
        
A naming container is a marker interface (INamingContainer), meaning that it does not implicitly enforce the hierarchy of controls under it but does create a naming scope for all contained controls.

The naming containers you will be most familiar with are master pages, content place holders and data bound controls like GridView.

For ex:-
            ImageButton btndetails = sender as ImageButton;

        GridViewRow row = (GridViewRow)btndetails.NamingContainer;

        Label l1 = (Label)row.FindControl("Label10");

All ASP.NET server controls include an ID property that uniquely identifies the control and is the means by which the control is programmatically accessed in the code-behind class. Similarly, the elements in an HTML document may include an id attribute that uniquely identifies the element; these id values are often used in client-side script to programmatically reference a particular HTML element. Given this, you may assume that when an ASP.NET server control is rendered into HTML, its ID value is used as the id value of the rendered HTML element. This is not necessarily the case because in certain circumstances a single control with a single ID value may appear multiple times in the rendered markup. Consider a GridView control that includes a TemplateField with a Label Web control with an ID value of ProductName. When the GridView is bound to its data source at runtime, this Label is repeated once for every GridView row. Each rendered Label needs a unique id value.

To handle such scenarios, ASP.NET allows certain controls to be denoted as naming containers. A naming container serves as a new ID namespace. Any server controls that appear within the naming container have their rendered id value prefixed with the ID of the naming container control. For example, the GridView and GridViewRow classes are both naming containers. Consequently, a Label control defined in a GridView TemplateField with ID ProductName is given a rendered id value of GridViewID_GridViewRowID_ProductName. Because GridViewRowID is unique for each GridView row, the resulting id values are unique.

to show Example of insert,update,delete in gridview using WCF Service please click here WCF Service For Insert,update,Delete
             
to show Example of insert,update,delete in gridview using LINQ please click here insert,update,delete using Linq

to show Example of insert,update,delete in gridview using Naming Container please click here Naming Container for insert update Delete in Gridview

to show Example of insert,update,delete in gridview using Modal Popup please click here insert,update,delete in Modal Popup

 to show Example of insert,update,delete in gridview using Stored Procedure please click here insert,update,delete through stored Procedure

  to show Example of insert,update,delete in XML File and bind to Gridview please click here insert,upadte,delete in XML File

  to show Example of insert,update,delete in gridview using Three Tier Architecture please click here Three Tier Architecture For insert,update,Delete

To show Example of Insert,update,Delete in MVC please click Here insert,update,delete record in MVC




namingcontainer.aspx:-

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

<!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"
         
          onrowdeleting="GridView1_RowDeleting"  
              >
          
        <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" />
                </ItemTemplate>
                </asp:TemplateField>
      
           <asp:TemplateField>
                <ItemTemplate>
            <asp:ImageButton ID="imgbtn" ImageUrl="~/ed.jpeg" runat="server" Width="25" Height="25"  onclick="imgbtn_Click" CausesValidation="false" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
              <asp:ImageButton ID="imgbtn1" ImageUrl="~/delete.png" runat="server" Width="25" Height="25" OnClientClick="return confirm('are you sure to Delete Record ?');" CausesValidation="false" CommandName="Delete" />
            </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>





namingcontainer.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 namingcontainer : 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("show3.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("show3.aspx");

            conn.Close();



        }

    }



    protected void imgbtn_Click(object sender, ImageClickEventArgs e)
    {

        ImageButton btndetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        Label l1 = (Label)row.FindControl("Label10");
        Label l2 = (Label)row.FindControl("Label1");
        Label l3 = (Label)row.FindControl("Label2");
        Label l4 = (Label)row.FindControl("Label3");
        Label l5 = (Label)row.FindControl("Label4");
        Label l6 = (Label)row.FindControl("Label5");
        Label l7 = (Label)row.FindControl("Label6");
        Label l8 = (Label)row.FindControl("Label7");
        Label l9 = (Label)row.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;


    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        /*foreach (GridViewRow r1 in GridView1.Rows)
        {
            for (int i = 0; i <= GridView1.Rows.Count; i++)
            {
                Label l = (Label)GridView1.Rows[i].Cells[0].FindControl("Label10");
                if (GridView1.DataKeys[e.RowIndex].Value.ToString().Equals(l.Text))
                {
                    Session["id"] = l.Text;*/
        dt.Rows.RemoveAt(e.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