Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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.Configuration;


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

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            FillDropDownList();
        }
    }

    private void FillDropDownList()
    {

        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select pid, pcode from project";
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataTextField = "pcode";
            DropDownList1.DataValueField = "pid";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--select--");
        }
        
    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int pid =Convert.ToInt32(DropDownList1.SelectedValue.ToString());
        Filldrop1();
       
    }

    private void Filldrop1()
    {

        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select  fyyear from yearly where pid=@pid";
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "fyyear";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "--select--");
        }
       
    }
}

this is my code behind file....


and this below is my aspx code

ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/grid.master" AutoEventWireup="true" CodeFile="yearwiseex.aspx.cs" Inherits="Default2" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Select Project Code"></asp:Label>
    <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="pid"
        Height="20px" Width="130px"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
&nbsp;
&nbsp;
    <asp:Label ID="Label2" runat="server" Text="Select Financial Year"></asp:Label>
    &nbsp;&nbsp;
&nbsp;<asp:DropDownList ID="DropDownList2" runat="server" DataValueField="autoid"
        Height="20px" Width="130px">
    </asp:DropDownList>
    &nbsp;
    <asp:Button ID="Button1" runat="server" Text="GO" />
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
    </asp:Content>




now what i m not able to do is ...i cant get the 2nd dropdownlist to populate

ill send u my database tables that m using :

project table:
pid ---int
pcode---varchar
pname---varchar
pi----varchar
copi----varchar
startyear---varchar
endyear---varchar


yearly table:
autoid---int
pcode---varchar
fyyear----varchar
yearlyalloc---int
salary---int
ta---int
contingency---- int
Posted
Updated 31-Oct-12 0:24am
v2
Comments
MT_ 31-Oct-12 6:26am    
Arent you asking the same question again http://www.codeproject.com/Questions/485959/Howplustoplusbindplusorpluspopulateplusdataplusint There were answers to that question !!
[no name] 31-Oct-12 6:26am    
ur assigning int value to ur 2nd dropdown i.e DataValueField="autoid" and trying to fetch fyyear that is varchar value..change as per requirement...

Hello
I think there is a problem in

cmd.CommandText = "select fyyear from yearly where pid=@pid";

change it to

cmd.CommandText = "select fyyear from yearly where pid='" + pid + "'";

This may solve your problem...

Good Luck
 
Share this answer
 
Update your Filldrop1 function to this:
C#
private void Filldrop1()
{
    string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    SqlConnection con = new SqlConnection(connstr);
    con.Open();
    SqlCommand cmd = new SqlCommand("select  fyyear from yearly where pid='"+DropDownList1_SelectedValue+"'", con);
    DataSet ds = new DataSet();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataTextField = "fyyear";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, "--select--");
    }   
}



Hope it helps!!
       --Amit
 
Share this answer
 
Try :
XML
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="pid"
       Height="20px" Width="130px"
       onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
   </asp:DropDownList><asp:dropdownlist id="DropDownList2" runat="server" datavaluefield="autoid" xmlns:asp="#unknown">
        Height="20px" Width="130px">
    </asp:DropDownList></asp:dropdownlist>


and in the .cs :

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       int pid =Convert.ToInt32(DropDownList1.SelectedValue.ToString());
       Filldrop1(pid);
   }


 private void Filldrop1(int pid)
    {
 
        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select  fyyear from yearly where pid="+pid;
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "fyyear";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "--select--");
        }
       
    }
 
Share this answer
 
v2
try this

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int pid =Convert.ToInt32(DropDownList1.SelectedValue.ToString());
        Filldrop1(pid);
       
    }
 
    private void Filldrop1(int pid)
    {
 
        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select  fyyear from yearly where pid="+pid+"";
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "fyyear";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "--select--");
        }
       
    }



Your dropdown2 is not getting value of pid as it is not declared globally. hope the above code helps.
 
Share this answer
 
v3
Try this, you are missing to pass parameter

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       int pid =Convert.ToInt32(DropDownList1.SelectedValue.ToString());
       Filldrop1(pid);

   }

private void Filldrop1(int varpid)
    {
 
        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select  fyyear from yearly where pid=@pid";
        cmd.Parameters.Add(New SqlParameter("@pid", varpid))
        DataSet ds = new DataSet();
        SqlDataAdapter adp = new SqlDataAdapter();
        adp.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DropDownList2.DataSource = ds.Tables[0];
            DropDownList2.DataTextField = "fyyear";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "--select--");
        }
       
    }
 
Share this answer
 
Comments
a2ulthakur 1-Nov-12 22:09pm    
do i have 2 join my sql tables also?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900