Monday, July 7, 2014

Grid DropDowns Dynamically Add

Create table script:
CREATE TABLE [dbo].[EmployeeDetails](
          [empid] [varchar](50) NULL,
          [name] [varchar](100) NULL,
          [designation] [varchar](100) NULL,
          [city] [varchar](50) NULL,
          [country] [varchar](50) NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[Designation](
          [id] [int] NULL,
          [designation] [varchar](50) NULL
) ON [PRIMARY]


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%" AutoGenerateColumns="false"
            ShowFooter="true" OnRowCommand="gvEmployeeDetails_RowCommand" OnRowDataBound="gvEmployeeDetails_OnRowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Employee ID">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlDesignation" runat="server">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCity" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="lblCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCountry" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action">
                    <FooterTemplate>
                        <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </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;

public partial class _Default : System.Web.UI.Page
{
   SqlConnection conn = new SqlConnection("Data Source=SPIDER;Initial Catalog=Demo;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
     protected void BindData()
    {
        DataSet ds = new DataSet();    
        conn.Open();
        string cmdstr = "Select * from EmployeeDetails";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvEmployeeDetails.DataSource = ds;
        gvEmployeeDetails.DataBind();       
    }

    protected void gvEmployeeDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("ADD"))
        {
            TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddEmpID");
            TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddName");
            DropDownList ddlDesignation = (DropDownList)gvEmployeeDetails.FooterRow.FindControl("ddlDesignation");
            TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCity");
            TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.FindControl("txtAddCountry");
          
            conn.Open();
            string cmdstr = "insert into EmployeeDetails(empid,name,designation,city,country) values(@empid,@name,@designation,@city,@country)";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
            cmd.Parameters.AddWithValue("@name", txtAddName.Text);
            cmd.Parameters.AddWithValue("@designation", ddlDesignation.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
            cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
            BindData();
        }     
    }   
    protected void gvEmployeeDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList ddlDesignation = (DropDownList)e.Row.FindControl("ddlDesignation");
            DataSet ds = new DataSet();
            conn.Open();
            string cmdstr = "Select * from Designation";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);           
            ddlDesignation.DataSource = ds.Tables[0];
            ddlDesignation.DataTextField = "designation";
            ddlDesignation.DataValueField = "id";
            ddlDesignation.DataBind();
            ddlDesignation.Items.Insert(0, new ListItem("--Select--""0"));
            conn.Close();
        } 
    }
}

.Net Flow

CLR :( Common Lalguage Runtime ):

It is responsible For Language Intigration And Managing Memory Process

OR

ClR  Must know Wt Type Of Language Developer Developed... With The Help Of CTS and CLS...

Delegate Interface AbstarctClass Pointer

Delegate: Delegate is A function Pointer. Wch Is Used to Point the Address Of Another Variable.
Delegate Are Two Types:
--> SingleCast Delegate
--> Multicast Delegate

When ever delegate Object refer to The addrss Of the Single method then it is Said to be singlevast Delegate.

When Ever delegate object Refer To multiple methods then it is said to be multicast Delegate

Pointer:
Pointer Is A Variable That Stores the Address Of Another Variable.

Interface:
InterFace contains undefined method..
OR
Its is a collection of members.. Without Any Defination..

Abstract Class:

Abstract Class Contains Defined And Undefined Methos

Or
It is A collection members.. With OR Without Any Any Defination

Trigger

It Is a One type Of stored procedure. Automatically Database Control hits The Trigger When Ever Event Accor 

Sunday, July 6, 2014

Two Dropdowns gridview



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

<!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>
    <form id="form1" runat="server">
    <div>
     <asp:GridView ID="outergrid" runat="server" AutoGenerateColumns="false"  OnRowDataBound="drop" ShowFooter="true"  DataKeyNames="id" >
          
            <Columns>
               
                <asp:TemplateField HeaderText="textbox1">
                    <ItemTemplate>
                        <asp:TextBox ID="txtid" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            
                <asp:TemplateField HeaderText="textbox2">
                    <ItemTemplate>
                        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
             
                <asp:TemplateField HeaderText="coutrydrop">
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="dropc" AppendDataBoundItems="true"  OnSelectedIndexChanged="dropc_SelectedIndexChanged"
AutoPostBack="true" >
                        <asp:ListItem Value="-1">--Select--</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="statedrop">
                    <ItemTemplate>
                        <asp:DropDownList runat="server" ID="drops" AppendDataBoundItems="true"  >
                        <asp:ListItem Value="-1">--Select--</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
             
          
                <asp:TemplateField HeaderText="innergrid">
                    <ItemTemplate>
                        <asp:GridView ID="innergrid" runat="server">
                        </asp:GridView>
                    </ItemTemplate>

                    <FooterTemplate>
                      <asp:Button ID="btnadd" Text="AddRows" runat="server" onclick="btnadd_Click" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
     

 
    </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;


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

    public string con = ConfigurationManager.ConnectionStrings["con"].ToString();
    SqlConnection cn;
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //ViewState.Clear();
            for (int i = 0; i < 10; i++)
            {
                CreateGridView();
               
            }
        }
    }

 
    public void CreateGridView()
    {

        //int numbers = 10;
        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("txtid", typeof(string));
        dt.Columns.Add("txtname", typeof(string));
        dt.Columns.Add("countrydrop", typeof(string));
        dt.Columns.Add("statedrop", typeof(string));
        dt.Columns.Add("innergrid", typeof(string));

        dt.Rows.Add("", "", "", "", "","");
        if (ViewState["dt"] == null)
        {
            dt.Rows.Add("", "", "", "", "","");
            ViewState["dt"] = dt;
            outergrid.DataSource = dt;
            outergrid.DataBind();
        }
        else
        {
            DataTable dt2 = (DataTable)ViewState["dt"];
            dt2.Rows.Add("", "", "", "", "","");
            ViewState["dt"] = dt2;

            outergrid.DataSource = dt2;
            outergrid.DataBind();

        }
        //if (ViewState["dt"] == null)
        //{
        //    dt.Rows.Add("", "", "", "", "");
        //    ViewState["dt"] = dt;
        //    outergrid.DataSource = dt;
        //    outergrid.DataBind();
        //}

    }
    protected void drop(object sender, GridViewRowEventArgs e)
    {
    

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var drop = (DropDownList)e.Row.FindControl("dropc");
            var drops = (DropDownList)e.Row.FindControl("drops");
            SqlConnection cnn = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand("select * from country");
            cmd.Connection = cnn;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds);
            drop.DataSource = ds;
            drop.DataTextField = "cname";
            drop.DataValueField = "cid";
            drop.DataBind();
          
             int CID = Convert.ToInt32(drop.SelectedValue);
            SqlCommand cmd1 = new SqlCommand("select * from state1 where cid=" + CID, cnn);
            SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            drops.DataSource = ds1;
            drops.DataTextField = "sname";
            drops.DataValueField = "sid";
            drops.DataBind();
              

        }
     
    }
  public void dropc_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropc = (DropDownList)sender;
        GridViewRow row = (GridViewRow)dropc.NamingContainer;
        DropDownList drops = (DropDownList)row.FindControl("drops");
        int CID = Convert.ToInt32(dropc.SelectedValue);
        cn = new SqlConnection(con);
        cn.Open();
        cmd = new SqlCommand("select * from state1 where cid=" + CID, cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        drops.DataSource = ds;
        drops.DataTextField = "sname";
        drops.DataValueField = "sid";
        drops.DataBind();
        ViewState["dt4"] = ds.Tables.Add();

        // DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        // Session.Clear();
        if (ViewState["dt"] != null)
        {

            DataTable dt1 = (DataTable)ViewState["dt"];
            DataRow dr = null;
            if (dt1.Rows.Count > 0)
            {
                dr = dt1.NewRow();
                dr["id"] = dt1.Rows.Count + 1;
                dt1.Rows.Add(dr);
                ViewState["dt"] = dt1;
                for (int i = 0; i < dt1.Rows.Count - 1; i++)
                {
                    DropDownList ddp = (DropDownList)outergrid.Rows[i].Cells[2].FindControl("dropc");
                    dt1.Rows[i]["countrydrop"] = ddp.SelectedItem.Text;
                    //DropDownList dds = (DropDownList)outergrid.Rows[i].Cells[3].FindControl("drops");
                    //dt1.Rows[i]["statedrop"] = dds.SelectedItem.Text;
                    ////dr = dt1.NewRow();
                    ViewState["dt"] = dt1;

                }
                outergrid.DataSource = dt1;
                outergrid.DataBind();
                ViewState["dt"] = dt1;

                //DropDownList dropc = (DropDownList)sender;
                //GridViewRow row = (GridViewRow)dropc.NamingContainer;
                //DropDownList drops = (DropDownList)row.FindControl("drops");
               
            }
        }
        else
        {
            Response.Write("view is null");
        }

        previousdata();

    }

   
    private void previousdata()
    {
        int a = 0;
        if (ViewState["dt"] != null)
        {
            DataTable dt3 = (DataTable)ViewState["dt"];
         
            if (dt3.Rows.Count > 0)
            {
                for (int i = 0; i < dt3.Rows.Count; i++)
                {
                    DropDownList ddp = (DropDownList)outergrid.Rows[a].Cells[2].FindControl("dropc");
                    DropDownList dds = (DropDownList)outergrid.Rows[a].Cells[3].FindControl("drops");
                    //dt2.Columns.Add("countrydrop", typeof(string));
                    //dt3.Rows[a]["countrydrop"] = ddp.SelectedItem.Text;
                    ddp.SelectedValue = dt3.Rows[i]["countrydrop"].ToString();
                    dds.SelectedValue = dt3.Rows[i]["statedrop"].ToString();
                  
                    if (i < dt3.Rows.Count - 1)
                    {
                        ddp.ClearSelection();
                        ddp.Items.FindByText(dt3.Rows[i]["countrydrop"].ToString()).Selected = true;
                        //dds.ClearSelection();
                        //dds.Items.FindByText(dt3.Rows[i]["statedrop"].ToString()).Selected = true;
                      
                    }
                    a++;
                }
            }
        }
    }
  
  
  
}