How asp.net dropdown1 selection fill the dropdown2 in C#?

I have two sql server tables 
User and Book
User Table columns: UserID, USerName
Book Columns: BookID, Books, UserID

  <asp:ListBox id="ddlUserGrp" runat="server" AppendDataBoundItems="true"  Rows="6" SelectionMode="Multiple" 
                            CssClass="CriteriaField" DataSourceID="sdsUserGrp"  AutoPostBack="true" 
                            OnSelectedIndexChanged="ddlUserGrp_SelectedIndexChanged"
                            DataTextField="UserName"  DataValueField="UserID" Width="200px">
                     <asp:ListItem Text="All" Value="0" Selected="True" />
                  </asp:ListBox>	
     
<asp:ListBox id="ddlBook" runat="server" AppendDataBoundItems="true"  Rows="6" SelectionMode="Multiple"  
                            CssClass="CriteriaField"    DataSourceID="sdsBooks"
                            DataTextField="Books" DataValueField="BookID" Width="200px">
                   <asp:ListItem Text="All" Value="0" Selected="True" />
                  </asp:ListBox>		

     <asp:SqlDataSource ID="sdsUserGrp" runat="server"
		 ConnectionString="<%$ ConnectionStrings:ConnString %>"
		 SelectCommand="[dbo].[usp_GetUserGrp]"
		 SelectCommandType="StoredProcedure">
		<SelectParameters>	
		</SelectParameters>
        </asp:SqlDataSource>

         <asp:SqlDataSource ID="sdsBooks" runat="server"
		 ConnectionString="<%$ ConnectionStrings:ConnString %>"
		 SelectCommand="[dbo].[usp_GetBooks]"
		 SelectCommandType="StoredProcedure">
		<SelectParameters>	
		</SelectParameters>
        </asp:SqlDataSource>
	

On Page Load, I want to display all the users in the User Dropdown and all the books in the book dropdown.
When All in the user dropdown  will display, display all books in the book dropdown.

If user is selected, depending upn the selection , second dropdownlist will populate.

c#
 protected void ddlUserGrp_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string sUserGrp = ""; 
            
       foreach (ListItem li in ddlUserGrp.Items)
            {
                if (li.Selected)
                {
                    sUserGrp += li.Value + ",";
                }
            }
            sUserGrp = sUserGrp.TrimEnd(',');

            if (sUserGrp == "0")
            {
                ddlBook.DataBind();
            }
            else
            {
                PopulateBookDDForSelectedGroup(sUserGrp);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
    }

 protected void PopulateBookDDForSelectedGroup(string sUserGrp)
    {
        try
        {
           this.ddlBook.Items.Clear();
            SqlDataReader myDataReader;
            SqlConnection myConnection = new SqlConnection(connectionString);
            DataLayer objBook = new DataLayer(myConnection.ConnectionString);


            myDataReader = objBook.GetBooksForSelectedGroups(sUserGrp);
            if (myDataReader.HasRows)
            {
                ddlBook.DataSource = myDataReader;
                ddlBook.DataTextField = "Books";
                ddlBook.DataValueField = "BookID";
                ddlBook.DataBind();
              
                objBook = null;
                
                //this.lblMessage.Visible = false;
            }
            
            myDataReader.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }//

   public SqlDataReader GetBooksForSelectedGroups(string UserGrpID)
    {
        SqlConnection objConn = new SqlConnection(_connectionString);
        // create new Command using stored proc name and Connection
        SqlCommand objCommand = new SqlCommand("usp_GetBooksForSelectedGroups", objConn);
        objCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter paramSecAcctGroupID = new SqlParameter("@UserGrpID", SqlDbType.VarChar, 50);
        objCommand.Parameters.Add(paramSecAcctGroupID);
        paramSecAcctGroupID.Value = SecAcctGroupID;

        try
        {
            // Open connection to the database
            objConn.Open();

            // Execute the stored proc to initialize the DataReader
            // Connection will be closed when DataReader goes out of scope
            return objCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception objErr)
        {
            throw (objErr);
        }
    }

Open in new window

Saroj13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sammySeltzerCommented:
So, what happens if your user ignores the user dropdown list and only selects from the book dropdown list?

I think you are approaching this the wrong way.

That's my two crusty coppers.
YZlatCommented:
Agree with PP. You should have all users listed in the first listbox at the Page_Load, and then, based on the Users listbox selection, filter second SqlDataSource to fill the second listbox with list of books for the selected user

 <asp:SqlDataSource ID="sdsBooks" runat="server"
		 ConnectionString="<%$ ConnectionStrings:ConnString %>"
		 SelectCommand="[dbo].[usp_GetBooks]"
		 SelectCommandType="StoredProcedure">
		<SelectParameters>
		<asp:ControlParameter Name="Name" ControlID="ddlUserGrp" PropertyName="SelectedValue" />
 		</SelectParameters>
</asp:SqlDataSource>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.