How to fill all listItems on dropDownList - ASP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to fill all listItems on dropDownList - ASP



I want to insert the database of hotels names from the sql,
in each one of the ListItem. (trying so much time).


ListItem



This is the cs code:


<asp:DropDownList id="hotels" runat="server" >
<asp:ListItem Text="hotel1"></asp:ListItem>
<asp:ListItem Text="hotel2"></asp:ListItem>
<asp:ListItem Text="hotel3"></asp:ListItem>
<asp:ListItem Text="hotel4"></asp:ListItem>
<asp:ListItem Text="hotel5"></asp:ListItem>
<asp:ListItem Text="hotel6"></asp:ListItem>
</asp:DropDownList>



and this is the aspx code: which is where I made a huge mistakes.


public partial class Home : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)


string hotel = hotels.Text;// mistake ? or to add ?)
SqlConnection sqlCon;
sqlCon = new SqlConnection(@"Data Source=ELAD_SSDSQLEXPRESS;Initial Catalog=Travels;Integrated Security=True");
SqlCommand Command;
Command = new SqlCommand("SELECT Hotels.Destiny FROM Travels WHERE Hotel = @hotel", sqlCon);
Command.Parameters.Add(new SqlParameter("hotel", hotel));

DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(Command);
adapter.Fill(dt);


for (dt.Rows.Count > 0)

hotels.Text = dt.Rows.Count;// MISTAKE





How should I fill each itemList?


itemList



I know to send it through Server.Transfer("Hotels.aspx");


Server.Transfer("Hotels.aspx");





hotels.DataSource = dt; hotels.DataBind();
– VDWWD
Aug 6 at 7:12


hotels.DataSource = dt; hotels.DataBind();




1 Answer
1



I assume the listitems are just placeholders? I'd take them out. Then:


foreach (System.Data.DataRow dr in dt.Rows)

hotels.Items.Add(new System.Web.UI.WebControls.ListItem(dr["Destiny"]));



Appologies if this is not 100% correct as I've edited from a working copy. But you get the idea.





Hi, sorry but it doesn't work, because I don't understand what to change in my own code so it will work as right. " Items"- ? "Rows" - error too and the (dr["Destiny"]) - erorr
– Doy waser
Aug 6 at 9:04





Assuming that your .Fill of the datatable is working, you then need to iterate through the rows that are now in the datatable's rows collection, and for each row add a new listitem to the hotels items collection. The dr["Destiny"] is picking out the column named "Destiny". All this goes in place of your for loop.
– TomC
Aug 6 at 12:04





defintly not true and all error, just a huge mistake. there is nothing as "hotels.item" and "dr" I did: DataRow r = table.Rows[i]; Session["1"] = table.Rows[i]; Session["2"] = table.Rows[i]; Session["3"] = table.Rows[i]; Session["4"] = table.Rows[i]; Session["5"] = table.Rows[i];
– Doy waser
Aug 7 at 15:10






It is with site navigation (why there is no enter here?) I use Server.Transfer("Hotels.aspx"); the whole data transtered to Hotels.aspx page
– Doy waser
Aug 7 at 15:23







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard