1

Could someone please tell me what I am doing wrong? I am trying to call the method automatically when the page loads, however, it does not work.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        customerInformation();

    }
}

protected void ddNames_SelectedIndexChanged(object sender, EventArgs e)
{
    customerInformation();
}

private void customerInformation()
{
    string dbString = ConfigurationManager.ConnectionStrings["TechSupportDBConString"].ConnectionString;
    string query = "SELECT * FROM Customers WHERE Name='" + ddNames.Text + "'";

    SqlConnection Connection = new SqlConnection(dbString);
    Connection.Open();

    SqlCommand Com = new SqlCommand(query, Connection);
    SqlDataReader reader = Com.ExecuteReader();
    if (reader.Read())
    {
        lblName.Text = reader["Name"].ToString() + "'s Personal Information";
        lblAddress.Text = reader["Address"].ToString() + "\n" + reader["City"].ToString() + " " + reader["State"].ToString() + " " + reader["ZipCode"].ToString();
        lblPhone.Text = reader["Phone"].ToString();
        lblEmail.Text = reader["Email"].ToString();

        reader.Close();
        Connection.Close();
    }
}
cnom
  • 3,071
  • 4
  • 30
  • 60
HereToLearn
  • 292
  • 5
  • 16
  • Have you tried debugging? – Alex Mar 02 '16 at 08:52
  • what exactly is the problem?`IsPostBack` means your `customerInformation` will only be called on first time page load.in subsequent postbacks this method would not be called again which is expected behaviour.However if you want to load customer info on some button click you need to explicitly call `customerInformation` – Navoneel Talukdar Mar 02 '16 at 08:55
  • @Neel I want all the labels to be updated with the proper data from the database as the page loads for the first time. Since there will already be a value selected in the dropdown list. – HereToLearn Mar 02 '16 at 08:59
  • The Dispose and Close logic in your method is wrong. You could be leaking Connection objects. – H H Mar 02 '16 at 09:01
  • 2
    Dropdown selected item should be `ddNames.SelectedItem.Text` or in case of value `ddNames.SelectedValue` – Navoneel Talukdar Mar 02 '16 at 09:02
  • Please explain what do you mean "It does not work". For example, is the customerInformation() called? or is the SqlDataReader Read() executing?? elaborate..! – cnom Mar 02 '16 at 09:08

1 Answers1

2

Ok looking through your code I can see the connection of reader and database is closed in loop itself which should not be done.

SqlDataReader reader = command.ExecuteReader();    
if (reader.HasRows)
{
     while (reader.Read())
     {
                //populate fields from reader
     }
}
else
{
     lbl.Text = "No records found.";
}
reader.Close();
Connection.Close();

Next thing which I noticed that dropdown selected text/value should be either ddNames.SelectedItem.Text or in case of value passing ddNames.SelectedValue.

Next thing make sure that each reader name should exactly match with table column name.

Last but not least always do a proper null check before applying ToString().if any column has null value ToString() would fail to convert.

Hope this helps.

Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
  • Strangely enough I get the "No records found." message as the page loads. But the dropdown list works perfectly fine. – HereToLearn Mar 02 '16 at 09:32
  • So clearly reader is null..Now go and check your connection string and query.(if possible with dropdown selected value test is sql first) – Navoneel Talukdar Mar 02 '16 at 09:34
  • On the right path but the reader and connection should be in `using() {...}` blocks. And there is no loop in the original code. – H H Mar 02 '16 at 10:16