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++;
                }
            }
        }
    }
  
  
  
}
       
   
   

Friday, July 4, 2014

Two Dropdowns in GridView Country and State

<%@ 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:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:abc %>"
            SelectCommand="SELECT * FROM Country"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Countryid"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Countryid" HeaderText="Countryid" InsertVisible="False"
                    ReadOnly="True" SortExpression="Countryid" />
                <asp:TemplateField HeaderText="Categories">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlCategories" AutoPostBack="true" DataTextField="CountryName"
                            DataValueField="Countryid" DataSourceID="SqlDataSource1" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Countryid") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Products">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlProducts" DataTextField="StateName" DataValueField="Stateid"
                            DataSourceID="SqlDataSource2" runat="server" />
                        <asp:SqlDataSource runat="server" ID="sqlDataSource2" ConnectionString="<%$ ConnectionStrings:abc %>"
                            SelectCommand="SELECT * FROM State"
                            FilterExpression="Countryid = '{0}'">
                            <FilterParameters>
                                <asp:ControlParameter Name="categoryParam" ControlID="ddlCategories" PropertyName="SelectedValue" />
                            </FilterParameters>
                        </asp:SqlDataSource>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 -----------------------------------------------------------------------------------
WEB.CONFIG
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
       <connectionStrings>
              <add name="abc" connectionString="Data Source=KCLINK-PC\SQLEXPRESS;Initial Catalog=kclink;Integrated Security=True" providerName="System.Data.SqlClient"/>
       </connectionStrings>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
       </system.web>
</configuration>



Thursday, July 3, 2014

Triggers With Insert Command



create trigger abc on travel1
after insert
as
declare
@places varchar(20),
@introduction varchar(20),
@packages int

select @places=i.places from inserted i;
select @introduction=i.introduction from inserted i;
select @packages=i.packages from inserted i;
insert into travel(places,introduction,packages) values(@places,@introduction,@packages)
go

insert into travel1 values('hyderabad','Telangana',800)



Data List And Repeater

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" 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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <asp:Repeater ID="rpt" runat="server">
      <ItemTemplate>
<table>
            <tr>
              <td>
<asp:Label ID="lblfirstname" runat="server" Text='<%#Bind("Firstname") %>' Font-Bold="true"></asp:Label>
<asp:Label ID="lblLastname" runat="server" Text='<%#Bind("LastName") %>' Font-Bold="true"></asp:Label>
            
              </td>
            </tr>
            <tr>
              <td>
                <asp:Label ID="lbldesignation" runat="server" Text='<%#Bind("designation") %>' Font-Bold="true"></asp:Label>

              </td>
            
            </tr>
            <tr>
              <td>
                <asp:Label ID="lblsalary" runat="server" Text='<%#Bind("Salary") %>' Font-Bold="true"></asp:Label>

              </td>
            </tr>
          </table>
      </ItemTemplate>
    </asp:Repeater>
      <asp:DataList ID="dtl" runat="server" RepeatColumns="1" BorderColor="Gray" BorderStyle="Solid">
        <ItemTemplate>
          <table>
            <tr>
              <td>
<asp:Label ID="lblfirstname" runat="server" Text='<%#Bind("Firstname") %>' Font-Bold="true"></asp:Label>
<asp:Label ID="lblLastname" runat="server" Text='<%#Bind("LastName") %>' Font-Bold="true"></asp:Label>
            
              </td>
            </tr>
            <tr>
              <td>
                <asp:Label ID="lbldesignation" runat="server" Text='<%#Bind("designation") %>' Font-Bold="true"></asp:Label>

              </td>
            
            </tr>
            <tr>
              <td>
                <asp:Label ID="lblsalary" runat="server" Text='<%#Bind("Salary") %>' Font-Bold="true"></asp:Label>

              </td>
            </tr>
            <tr>
             <td>
               <asp:Button ID="btnsubmit" runat="server" Text="Submit" CommandArgument=<%#Bind("Salary") %> OnCommand="submit_commend" />
             </td>
            </tr>
          </table>
        </ItemTemplate>
      </asp:DataList>
    </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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Firstname", typeof(string));
        dt.Columns.Add("LastName", typeof(string));
        dt.Columns.Add("designation", typeof(string));
        dt.Columns.Add("Salary", typeof(string));
        dt.Rows.Add("Trinadh", "K", "Sr.developer", "100000");
        dt.Rows.Add("srinivas", "M", "Sr.developer", "1000000");
        dt.Rows.Add("Siva", "Master", "Sr.developer", "500000");
        dt.Rows.Add("Mahesh", "Agadu", "Sr.developer", "700000");
        dt.Rows.Add("Vishnu", "Agutadu", "Sr.developer", "800000");
        dt.Rows.Add("Rakesh", "Danush", "Sr.developer", "900000");

        dtl.DataSource = dt;
        dtl.DataBind();

        rpt.DataSource = dt;
        rpt.DataBind();



    }
    public void submit_commend(object sender, CommandEventArgs e)
  
    {
        string id = e.CommandArgument.ToString();
    }
}



Monday, June 30, 2014

multi Stored Procedure Binding DropDown list Selected Values To bing Gridview Using Sessions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class drpgrid : System.Web.UI.Page
{
    adosession obj;
    protected void Page_Load(object sender, EventArgs e)
    {

        obj = new adosession();



        DataSet ds = obj.bindgrid(Convert.ToInt32(Session["id"]), "ById");


        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(int));
        dt.Columns.Add("name", typeof(string));
        dt.Columns.Add("address", typeof(string));

        if (Session["dt"] == null)
        {
            dt.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString());
            grd.DataSource = dt;
            grd.DataBind();
            Session["dt"] = dt;
        }
        else
        {
            DataTable dt2 = (DataTable)Session["dt"];
            dt2.Rows.Add(ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString(), ds.Tables[0].Rows[0][2].ToString());
            Session["dt"] = dt2;
            grd.DataSource = dt2;
            grd.DataBind();
        }

    }
}

 -----------------------------------------------------------------------------------
Stored Procedure
USE [naresh]
GO

/****** Object:  StoredProcedure [dbo].[sp_getgridvalues]    Script Date: 06/30/2014 13:04:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_getgridvalues] 
@id int,
@type varchar(50)

as
begin
if(@type='Byid')

select id,name,address from gridsession where id=@id
else
select * from gridsession
end

GO

Multi Stored Procedute

USE [naresh]
GO

/****** Object:  StoredProcedure [dbo].[sp_getgridvalues]    Script Date: 06/30/2014 13:04:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[sp_getgridvalues] 
@id int,
@type varchar(50)

as
begin
if(@type='Byid')

select id,name,address from gridsession where id=@id
else
select * from gridsession
end

GO