Md. Didarul Islam Md. Didarul Islam - 6 months ago 55
HTML Question

How can I load DropdownList box in Repeater by using ASP.NET and SQL QUERY

I can load textbox but i failed to load dropdownbox:

<asp:Repeater ID="RtpDetails" runat="server">
<HeaderTemplate>
<tr>
<td width="20%" align="left"><strong>Item Name</strong></td>
<td width="20%" align="left"><strong>Price</strong></td
</tr>
</HeaderTemplate>
<ItemTemplate>

<td><asp:TextBox ID="Txtitem" Value='<%#Eval("Product_Name") %>' runat="server"></asp:TextBox></td>
<td><asp:DropDownList ID="drp" runat="server"> </asp:DropDownList></td>

</ItemTemplate>
</asp:Repeater>


C# :
I am using this to into textbox now I am trying to get value according to below SQL qurey

protected void BtnSrc_Click(object sender, EventArgs e)
{

RtpDetails.DataSource = GetData();
RtpDetails.DataBind();
}
public DataTable GetData()
{
string sql = "SELECT Auto_SL, OrderNo, Date, CustomerName, ProductID, Price, Total, Quantity, Product_Name FROM Order_Details WHERE OrderNo='" + TxtSearch.Text+ "'"; ///---- For test
Connstring.conn.Open();
SqlCommand cmd = new SqlCommand(sql, Connstring.conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Connstring.conn.Close();
return dt;
}

Answer

you should have to add data source in drop down list. the below is the sample code.

   <asp:Repeater ID="RtpDetails" runat="server">
            <HeaderTemplate>
                <td>Item Name
                </td>
                <td>Price
                </td>
            </HeaderTemplate>
            <ItemTemplate>

                <td>
                    <asp:TextBox ID="Txtitem" runat="server"></asp:TextBox></td>
                <td>
                    <asp:DropDownList ID="drp" runat="server" DataTextField="Product_Name" DataValueField="Product_ID" DataSource='<%# setProduct()%>'>
                    </asp:DropDownList></td>

            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="BtnSrc" Text="Source" runat="server" OnClick="BtnSrc_Click" />

Code behind file

    protected void BtnSrc_Click(object sender, EventArgs e)
    {
        RtpDetails.DataSource = GetData();
        RtpDetails.DataBind();

    }

    public DataTable setProduct()
    {
        DataTable dt = new DataTable();
        return GetData();
    }

    public DataTable GetData()
    {
        string sql = "SELECT Product_ID, Product_Name FROM  Order_Details"; ///---- For test  
        SqlConnection Connstring = new SqlConnection("Data Source=localhost;Initial Catalog=test;User ID=sa;Password=123;");
        Connstring.Open();
        SqlCommand cmd = new SqlCommand(sql, Connstring);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Connstring.Close();
        return dt;
    }