1

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?

6
  • 1
    Define "it does not work." In what way does it not work? How and when does the observed behavior deviate from the expected behavior? Commented Aug 31, 2012 at 12:44
  • When a item is chosen from the list it does not show/fill the labels they just stay blank. Commented Aug 31, 2012 at 12:47
  • What is the database query being performed when this happens? Are there even any records being returned? My first guess would be that you're probably re-setting the value of the drop-down-list on 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. Commented Aug 31, 2012 at 12:49
  • what issue you are having?did you debug and checked the Values? Commented Aug 31, 2012 at 12:52
  • The page load code is empty there is nothing in it and i am really noob at this so uhm yeah this is all the code i have at the moment, when you say loop is that the while(reader.Read()) statement ? Commented Aug 31, 2012 at 12:54

2 Answers 2

5

The problem is that your reader is null. Set reader = SqlCommand.ExecuteReader()

Example:

reader = cmdReade.ExecuteReader(); 
Sign up to request clarification or add additional context in comments.

1 Comment

@Nirre welcome. Kindly mark it as your answer. There is a tick mark below the vote button. Click on it to accept it as your answer.
1

You Missed

reader =cmdSelect.ExecuteReader();  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.