Ihechi Alozie

Ihechi Alozie

  • NA
  • 7
  • 6.3k

How to bind database data to DropdownList inside Repeater

Sep 14 2017 12:31 PM
Greetings mates,
This is my first time here. Please take it easy on me.
I have been trying for the past two days to try to bind a data from the database to DropDownList in Repeater control but no luck so far.
I have the following:

  1. private void SetInitialRow2(string registerNumber, string bregisterNumber)  
  2. {  
  3.     DataTable dt;  
  4.     if (ViewState["CurrentTable"] == null)  
  5.     {  
  6.         dt = new DataTable();  
  7.         DataRow dr = null;  
  8.         //Create DataTable columns  
  9.         dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));  
  10.         dt.Columns.Add(new DataColumn("PrevOnwerName", typeof(string)));  
  11.         dt.Columns.Add(new DataColumn("prevAddr", typeof(string)));  
  12.         dt.Columns.Add(new DataColumn("prevCity", typeof(string)));  
  13.         dt.Columns.Add(new DataColumn("PrevState", typeof(string)));  
  14.         dt.Columns.Add(new DataColumn("prevzip", typeof(string)));  
  15.   
  16.         //Create Row for each columns  
  17.         dr = dt.NewRow();  
  18.         dr["RowNumber"] = 1;  
  19.         dr["PrevOnwerName"] = string.Empty;  
  20.         dr["prevAddr"] = string.Empty;  
  21.         dr["prevCity"] = string.Empty;  
  22.         dr["PrevState"] = string.Empty;  
  23.         dr["prevzip"] = string.Empty;  
  24.         dt.Rows.Add(dr);  
  25.     }  
  26.     else  
  27.     {  
  28.         dt = (DataTable)ViewState["CurrentTable"];  
  29.     }  
  30.     //Store the DataTable in ViewState for future reference  
  31.     ViewState["CurrentTable"] = dt;  
  32.     if (dt.Rows.Count > 0)  
  33.     {  
  34.         //Bind the Repeater with the DataTable  
  35.         Repeater2.DataSource = dt;  
  36.         Repeater2.DataBind();  
  37.     }  
  38. }  

Then I have a stored procedure that pulls these records from the database.

The SP is called like this:

  1.     private void getRecs(int pin)  
  2.     {  
  3.         SqlConnection conn = new SqlConnection(connStr);  
  4.         SqlCommand cmd = new SqlCommand("sp_AllRecs", conn);  
  5.         cmd.CommandType = CommandType.StoredProcedure;  
  6.         SqlParameter p1 = new SqlParameter("@pin", pin);  
  7.         cmd.Parameters.Add(p1);  
  8.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  9.         DataTable dtPrevRecs = new DataTable();  
  10.         sda.Fill(dtPrevRecs);  
  11.         if (dtPrevRecs.Rows.Count > 0)  
  12.         {  
  13.             Repeater2.DataSource = dtPrevRecs;  
  14.             Repeater2.DataBind();  
  15.             ViewState["CurrentTable"] = dtPrevRecs;  
  16.         }  
  17. }  
I am also populating State DropDownList with the following:
      
  1. con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());  
  2.  string sSQL = "Select sID,sName from states ORDER By sName ASC";  
  3.  // Response.Write(sSQL);  
  4.  //Response.End();  
  5.  SqlCommand cmd3 = new SqlCommand(sSQL, con);  
  6.  con.Open();  
  7.  cstable = new DataTable();  
  8. able.Load(cmd3.ExecuteReader());  
 Finally, my markup:
  1. <asp:DropDownList ID="ddlPrevState" cssClass="disabledcss" runat="server" AppendDataBoundItems="True">  
  2. <asp:ListItem Value="" Selected="True"></asp:ListItem>  
  3. </asp:DropDownList>  
My question is when a user enters an account number, all records associated with that account populates the repeater form except the State dropdownlist.

Any ideas how to bind the data to the State dropdownlist? 

Answers (2)