0

My web page should allow me to select a title of a book from a drop-down list, press the select button, and textboxes(ie. Author, Year, Category) should change according to the book selected. However, when I select the second book from the list, it always loops back to show the details of the 1st book on the dropdown list, no matter what I select, it always only shows the first record.

Here is my code:

  protected void Page_Load(object sender, EventArgs e)
{
    LoadDetails();
}

private void LoadDetails()
{
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    string connectionString =ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT BookId, Title FROM Books", conn);
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        ddlSearch.DataSource = reader;
        ddlSearch.DataValueField = "BookId";
        ddlSearch.DataTextField = "Title";
        ddlSearch.DataBind();
        reader.Close();
    }

    catch
    {
        lblError.Text = "Error loading books";
    }

    finally { conn.Close(); }
}
protected void btnSearch_Click(object sender, EventArgs e)
{
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    string connectionString =ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT ISBN, Author, Title, Year, Category FROM Books Where BookId=@BookId", conn);
    comm.Parameters.Add("@BookId", System.Data.SqlDbType.Int);
    comm.Parameters["@BookId"].Value = ddlSearch.SelectedItem.Value;
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        if (reader.Read()) 
        {
            txtIsbn.Text = reader["ISBN"].ToString();
            txtAuthor.Text = reader["Author"].ToString();
            txtTitle.Text = reader["Title"].ToString();
            txtYear.Text = reader["Year"].ToString();
            txtCat.Text = reader["Category"].ToString();

        }
    }


    catch
    {
        lblError.Text = "Error laoding pages";
    }

    finally
    {
        conn.Close();

    }
}

I've tried many different variations of code but it doesnt seem to be working. It probably is a simple enough fix but I can't put my finger on it. Any help appreciated. Thanks!

SOFKiNG
  • 385
  • 1
  • 3
  • 20
user3249809
  • 83
  • 3
  • 14
  • Put breakpoints in your code. You'll notice that the `Click` event is being fired before the `Load` one. – emerson.marini Mar 24 '14 at 12:08
  • Add IsPostBack condition to your Page_Load – Ali Baghdadi Mar 24 '14 at 12:09
  • @user3249809: Just a suggestion...Why you code in button click? wouldn't it be better to you a separate class to handle database stuff? For e.g look at [How to implement 3 tiers architecture in c#](http://stackoverflow.com/questions/13786549/how-to-implement-3-tiers-architecture-in-c-sharp) – huMpty duMpty Mar 24 '14 at 12:12

3 Answers3

2

Your problem is in the Page_Load, the btnSearch_Click event is fired but after that it will enter Page_Load event and since you didn't check if the page posts back it will reload the details again, this will solvethe problem:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    LoadDetails();
}
Ali Baghdadi
  • 648
  • 1
  • 5
  • 17
2

That thing what happens with you is called a post back, What you should do here is

     protected void Page_Load(object sender, EventArgs e)
      {
         if(!isPostBack)   
              LoadDetails();
      }

And u should do something is to enable viewstates, You can find that in properties windows

Check these links

What is a postback?

http://msdn.microsoft.com/en-us/library/ms972976.aspx

Community
  • 1
  • 1
Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32
1

try this code hope it will help

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
      LoadDetails();
   }
}
Aftab Ahmed
  • 1,727
  • 11
  • 15