The user must select one of the values from the dropdown list, then the data from the database should be fetched and put in to the labels on the page (i.e. BookName, Author, Rating, Comments).
This is what I have so far but it does not work:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
con.Open();
OleDbCommand cmdReade = new OleDbCommand();
OleDbDataReader reader = null;
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM Books WHERE bookName = '" + DdlSelectBook.SelectedItem.ToString() + "'");
while (reader.Read())
{
lblBookName.Text = reader["bookName"].ToString();
lblAuthor.Text = reader["Author"].ToString();
lblRating.Text = reader["Rating"].ToString();
lblComments.Text = reader["Comments"].ToString();
}
}
}
The database tables are BookName, Author, Rating, Comments, plus there is an image that should be displayed from a folder in the project that matches the book displayed.
What is wrong with my code?
Page_Load, but I can't be certain without seeing that code. Also, out of curiosity, why do you set the labels in a loop? They'll only show the last record. If you only want to see the last record, only select that record. No sense looping through stuff you're just going to ignore.