protected void RefList_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Define ADO.NET objects.
        string selectSQL;
        selectSQL = "SELECT * FROM Project ";
        selectSQL += "WHERE Ref#= '"+RefList.SelectedItem.Text +"'";
     //   Label14.Text = RefList.SelectedItem.Text;
        SqlConnection con = new SqlConnection();

        con.ConnectionString = "Data Source=.;Initial Catalog=Project;User ID=sa;Password=786";
        SqlCommand cmd = new SqlCommand(selectSQL, con);
        SqlDataReader reader;
        // Try to open database and read information.
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            reader.Read();

            TextBox1.Text = reader["Description"].ToString();
            TextBox2.Text = reader["Purchase_Price"].ToString();
            TextBox3.Text = reader["Sale_Price"].ToString();
            TextBox4.Text = reader["Quantity"].ToString();
            TextBox5.Text = reader["Warning_Quantity"].ToString();
            TextBox6.Text = reader["Expiry_Date"].ToString();
            TextBox7.Text = reader["Vendors"].ToString();
            TextBox8.Text = reader["Ref#"].ToString();

/*           TextBox1.Text = reader["0"].ToString();
            TextBox2.Text = reader["1"].ToString();
            TextBox3.Text = reader["2"].ToString();
            TextBox4.Text = reader["3"].ToString();
            TextBox5.Text = reader["4"].ToString();
            TextBox6.Text = reader["5"].ToString();
            TextBox7.Text = reader["6"].ToString();
            TextBox8.Text = reader["7"].ToString(); */


            reader.Close();
            lblStatus.Text = "";
        }
        catch (Exception err)
        {
            lblStatus.Text = "Error in displaying data. ";
            lblStatus.Text += err.Message;
        }
        finally
        {
            con.Close();
        }
    }

Recommended Answers

All 3 Replies

What error are you getting?
Your connection string might need a security statement like this:
http://msdn.microsoft.com/en-us/library/d7469at0.aspx

Aslo, please remove the SPACE after the equal-sign in "Ref#= "

Also, do you really have a column with a # sign in the name?
If you query this table with any other tool, does it wrap that column namne in single quotes ('') or grave accents (``) or double quotes ("")?
...testing...

I made a test in Microsoft Query and the SQL looked like this:

SELECT has_hash."ref#"
FROM MYDB.dbo.has_hash has_hash

...which would mean the SQL in a string would look like:

string selectSQL="SELECT has_hash.\"ref#\" FROM MYDB.dbo.has_hash"

Also, you should check to see if your reader can read the data

if(reader.Read())
{
   //get data from columns
}
//or...
while(reader.Read())
{
   //get data from columns
}

I also noticed your datasource is ".". Is that intentional?

You could modify the code (a little) so when you're stepping through this in the debugger, you'll know exactly where it happens:

protected void RefList_SelectedIndexChanged(object sender, EventArgs e)
      {
         lblStatus.Text = "reading...";
         string selectSQL = selectSQL =
            "SELECT * FROM Project " + 
            "WHERE \"Ref#\"='" + RefList.SelectedItem.Text + "'";

         try
         {
            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder()
            {
               DataSource = ".",
               InitialCatalog = "Project",
               UserID = "sa",
               Password = "786",
              //IntegratedSecurity = true ///probably not needed
            };

            using (SqlConnection con = new SqlConnection(csb.ToString()))
            {
               con.Open();

               using (SqlDataReader reader = (new SqlCommand(selectSQL, con)).ExecuteReader())
               {
                  if (reader.Read())
                  {
                     TextBox1.Text = reader["Description"].ToString().Trim();
                     TextBox2.Text = reader["Purchase_Price"].ToString().Trim();
                     TextBox3.Text = reader["Sale_Price"].ToString().Trim();
                     TextBox4.Text = reader["Quantity"].ToString().Trim();
                     TextBox5.Text = reader["Warning_Quantity"].ToString().Trim();
                     TextBox6.Text = reader["Expiry_Date"].ToString().Trim();
                     TextBox7.Text = reader["Vendors"].ToString().Trim();
#if UNTESTED         //this will need to be tested
                     TextBox8.Text = reader["Ref#"].ToString().Trim();
#endif
                  }
                  //
                  reader.Close();
               }

               con.Close();

               lblStatus.Text = "";
            }
         }
         catch (Exception err)
         {
            lblStatus.Text = "Error in displaying data: " + err.Message;
         }
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.