I am creating a movie system.
I am using a dropdownlist to retrieve the date slots data from the movie database and display it into the dropdown list. Anyone have any idea how to do it with some sample asp.net codes? Thanks :eek:
Since confirmed the data will come from a database (and following 's question about hard‑coded vs DB), two practical ways to get multiple columns into a DropDownList are shown below: concatenate the fields in SQL and bind them as the DataTextField, or create ListItem objects server‑side so the displayed text is a combination but additional values (date/time/id) are still available.
Example — concatenate in SQL and bind:
SELECT SlotID,
CONVERT(varchar(10), ShowDate, 103) + ' ' + LEFT(CONVERT(varchar(8), ShowTime, 108),5) AS SlotText
FROM ShowSlots
ORDER BY ShowDate, ShowTime ASPX and C# bind:
<asp:DropDownList ID="ddlSlots" runat="server" DataTextField="SlotText" DataValueField="SlotID" />
// code-behind
ddlSlots.DataSource = dataTable;
ddlSlots.DataTextField = "SlotText";
ddlSlots.DataValueField = "SlotID";
ddlSlots.DataBind(); Example — keep separate fields and attach them to each option:
while(reader.Read())
{
var id = reader["SlotID"].ToString();
var text = Convert.ToDateTime(reader["ShowDate"]).ToString("dd/MM/yyyy")
+ " " + Convert.ToDateTime(reader["ShowTime"]).ToString("HH:mm")
+ " - " + reader["Auditorium"];
var li = new ListItem(text, id);
li.Attributes["data-date"] = Convert.ToDateTime(reader["ShowDate"]).ToString("yyyy-MM-dd");
li.Attributes["data-time"] = Convert.ToDateTime(reader["ShowTime"]).ToString("HH:mm");
ddlSlots.Items.Add(li);
} Troubleshooting notes: set DataTextField/DataValueField before calling DataBind; bind only when !IsPostBack to preserve selection; use parameterized queries to avoid SQL injection; for large numbers of slots consider an autocomplete or paging instead of a huge dropdown; store UTC in DB and format server‑side for consistent display.
Jump to Post— Paladine 138Are you meaning hard code the Dropdownlist or retrieve the data from a DB source?
Are you meaning hard code the Dropdownlist or retrieve the data from a DB source?
Retrieving from Database
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.