I have 3 dropdownlist named as dd1,dd2 and dd3.
in dd1 i have datas getting from database.
then in dd2 bound the data by selecting the dd1 datas...
tat coding is
========

protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {      
        string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue;
        adp = new SqlDataAdapter(a, con);
        ds = new DataSet();
        adp.Fill(ds, "swift");
        dd2.DataSource = ds.Tables["swift"].DefaultView;
        dd2.DataTextField = "descriptions";
        dd2.DataValueField = "sid";
        dd2.DataBind();
    }

like wise dd3 also will be filled. i have set autopostback=true also.
but my problem is when the dd1 has only one data
then dd2 is not filled.because i have written in selectedindexchanged so based on the dd1 value it ll change
but if it has one data then it s not coming.
so pls suggest me for this problem .

Thank you,

For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution

if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.

Temp Solution

On Page Load Event.

Please do the following,

Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.

Regards
razool

or u can do like..............
check the item count of dd1 if it is 1 then call selected index change event.

For your kind information on SelectedIndexChanged events fires only when index of item changes. it wont fires if there is only one item in drop down.
Permant Solution

if you realy want to achive that, please add a dummy item like ["---Select a value--"] in the dropdownbox.

Temp Solution

On Page Load Event.

Please do the following,

Load the dropdown1 with data
Load dropdown2 with default firstitems dropdown1 value.
Load dropdown3 with default firstitems dropdown2 value.

Regards
razool

===========================================================

hi
when i add the <!--select the value--> it show an error so help me to do pls

the error is...........A potentially dangerous Request.Form value was detected from the client (dd3="<!--Select the item-...").

code behind coding
----------------------

protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection("server=SOFTWARE4;uid=sa;pwd=;database=Helpdesk1");
        con.Open();                            
        txt_date.Text = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");       
        if (!Page.IsPostBack)
        {
            loaddata();           
        }          
    }
    void loaddata()
    {
        string s = "select * from swift where swiftid='blk' and sid in(1,2,3,4,5)";
        adp = new SqlDataAdapter(s, con);
        ds = new DataSet();
        adp.Fill(ds, "swift");
        dd1.DataSource = ds.Tables["swift"].DefaultView;
        dd1.DataTextField = "descriptions";
        dd1.DataValueField = "sid";
        dd1.DataBind();                          
    }       
     protected void Button1_Click(object sender, EventArgs e)
    {    
        string a = "insert into swiftresult values(" + dd1.SelectedValue + "," + dd2.SelectedValue + "," + dd3.SelectedValue + ","+dd5.SelectedValue+",'" + dd4.SelectedValue + "','" + TextBox1.Text + "','"+txt_date.Text+"','n',null,null,null,null,null)";
        SqlCommand cmd = new SqlCommand(a, con);
        cmd.ExecuteNonQuery();
        string b = "select callno from swiftresult where callno=(select max(callno) from swiftresult)";
        SqlCommand cmd1 = new SqlCommand(b, con);
        SqlDataReader dr = cmd1.ExecuteReader();
        if (dr.Read())
        {          
            Session["callno"] = dr["callno"];
        }
        dr.Close();      
       Response.Redirect("regist.aspx");                
    }
    protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {    
        string a = "select distinct a.descriptions,a.sid from swift a,pc_link b where swiftid='loc'and a.sid=b.locationid and blockid="+dd1.SelectedValue;
        adp = new SqlDataAdapter(a, con);
        ds = new DataSet();
        adp.Fill(ds, "swift");
        dd2.DataSource = ds.Tables["swift"].DefaultView;
        dd2.DataTextField = "descriptions";
        dd2.DataValueField = "sid";
        dd2.DataBind();
    }
    protected void dd2_SelectedIndexChanged(object sender, EventArgs e)
    {      
        string b = "select distinct a.descriptions,b.depid from swift a,pc_link b where a.swiftid='dep' and b.blockid=" + dd1.SelectedValue + "  and b.locationid=" + dd2.SelectedValue + " and a.sid=b.depid";
        SqlDataAdapter adp1 = new SqlDataAdapter(b, con);
        DataSet ds = new DataSet();
        adp1.Fill(ds);
        dd3.DataSource = ds;
        dd3.DataTextField = "descriptions";        
        dd3.DataValueField ="depid";
        dd3.DataBind();
    }
       protected void dd3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string k = "select distinct a.descriptions,b.pcid from swift a,pc_link b where a.swiftid='pc' and b.blockid="+dd1.SelectedValue+" and b.locationid="+dd2.SelectedValue+"and a.sid=b.pcid and b.depid="+dd3.SelectedValue;
        SqlDataAdapter adp3 = new SqlDataAdapter(k, con);
        DataSet ds = new DataSet();
        adp3.Fill(ds);
        dd5.DataSource = ds;
        dd5.DataTextField = "descriptions";
        dd5.DataValueField = "pcid";
        dd5.DataBind();
    }

html coding
-------------------

<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HelpdeskConnectionString %>"
            SelectCommand="SELECT [blockid], [block] FROM [Helpdesk_block]"></asp:SqlDataSource>
        <asp:TextBox ID="txt_date" runat="server" Visible="False"></asp:TextBox>
       
        <asp:Panel ID="Panel1" runat="server" BackColor="Teal" BorderColor="Teal" Height="7px"
            Width="482px" Font-Bold="True" Font-Size="X-Large">           
            &nbsp;&nbsp;<asp:Panel ID="Panel2" runat="server" BackColor="White" Height="371px" Width="563px" BorderColor="Teal" BorderStyle="Solid">
                  <asp:Label ID="Label2" runat="server" Text="Block" Width="91px" Font-Bold="True" Font-Size="Medium"></asp:Label>
                <asp:DropDownList ID="dd1" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd1_SelectedIndexChanged" Font-Size="Small">
                </asp:DropDownList>
            
                <asp:Label ID="Label3" runat="server" Text="Location" Width="83px" Font-Bold="True" Font-Size="Medium"></asp:Label>
                
                <asp:DropDownList ID="dd2" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="dd2_SelectedIndexChanged" Font-Size="Small">
                </asp:DropDownList>&nbsp;<br />             
             <asp:Label ID="Label4" runat="server" Text="Departments" Width="116px" Font-Bold="True" Font-Size="Medium"></asp:Label>
              <asp:DropDownList ID="dd3" runat="server" Width="130px" OnSelectedIndexChanged="dd3_SelectedIndexChanged" AutoPostBack="True" Font-Size="Small">
                    <asp:ListItem>&lt;!--Select the item--&gt;</asp:ListItem>
                </asp:DropDownList><br />                
                <asp:Label ID="Label7" runat="server" Font-Size="Medium" Text="Pc-Name" Width="118px"></asp:Label>
               <asp:DropDownList ID="dd5" runat="server" AutoPostBack="True" Width="130px" Font-Size="Small">
                </asp:DropDownList><br />
                <br />
               <asp:Label ID="Label5" runat="server" Text="Problem Type" Width="115px" Font-Bold="True" Font-Size="Medium"></asp:Label>
               <asp:DropDownList ID="dd4" runat="server" AutoPostBack="True"  Width="130px" OnSelectedIndexChanged="dd4_SelectedIndexChanged" Font-Size="Small">
                    <asp:ListItem>Software</asp:ListItem>
                    <asp:ListItem Selected="True">Hardware</asp:ListItem>
                    <asp:ListItem>Others</asp:ListItem>
                </asp:DropDownList><br />
               <asp:DropDownList ID="dd6" runat="server"
                                AutoPostBack="True" OnSelectedIndexChanged="dd6_SelectedIndexChanged" Width="130px" Visible="False" Font-Size="Small">
                            </asp:DropDownList><br />
                <br />               
                  <div style="z-index: 102; left: 245px; width: 113px; position: absolute; top: 361px;
                    height: 35px">
                <asp:Button ID="Button1" runat="server" Text="Save" Width="114px" OnClick="Button1_Click" /></div>
                
                
                <div style="z-index: 101; left: 116px; width: 179px; position: absolute; top: 283px;
                    height: 20px">
                    &nbsp; &nbsp; &nbsp;
                    <asp:Label ID="Label6" runat="server" Text="Software Definitions" Width="104px" Font-Bold="True" Font-Size="Medium" Visible="False"></asp:Label>
                    &nbsp; &nbsp; &nbsp;</div>
                
            </asp:Panel>
           </asp:Panel>
        </div>
      
    
    </div>
      
        <div style="z-index: 102; left: 395px; width: 181px; position: absolute; top: 398px;
            height: 47px">
            &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" Text="Hardware Definitions"
                        Width="109px" Visible="False"></asp:Label></div>
        <div style="z-index: 103; left: 398px; width: 371px; position: absolute; top: 406px;
            height: 49px">
            &nbsp; &nbsp; &nbsp;<asp:Label ID="Label8" runat="server" Font-Size="Medium"
                            Text="Others Definitions" Visible="False" Font-Bold="True"></asp:Label>
            &nbsp; &nbsp;&nbsp;<asp:TextBox ID="TextBox1" runat="server" Height="37px"
                        TextMode="MultiLine" Visible="False" Width="185px"></asp:TextBox></div>
    </form>
</body>

the coding is

For adding the dummy item try this code

ddlRecNature.Items.Insert(0, new ListItem("Select"));
            ddlRecNature.SelectedIndex = 0;

You actually don't even need to set the selected index, since you are inserting it into the default first position. But yes, that would work perfectly:

dd1.Items.Insert(0, new ListItem("-- SELECT HERE --"));

0 is the position, since it is 0 based and not 1, 0 means first position.

commented: Our forum is Lucky to have SheSaidImaPregy +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.