Add
{
and
}
around the code block that should be executed only when data is read. See the example below. The way your code is written would cause the assignment statements starting with
TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
to be executed whether there was data or not.
I added an
if
statement at the start of the code to check to be sure that there is data in
TextBoxINST_ID.Text
. This will handle the case when
TextBoxINST_ID.Text
does not yet contain a value. Also, see comment below about using SQLParameter class.
if (TextBoxINST_ID.Text.Trim().Length > 0)
{
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
con3.Open();
SqlCommand scmd3 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
SqlDataReader dr3 = scmd3.ExecuteReader();
if (dr3.Read())
{
TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
TextBoxTNNCC.Text = dr3["NCHC"].ToString();
}
con3.Close();
dr3.Close();
}
Also, it would be a best practice to use a parameterized query rather than
'" + TextBoxINST_ID.Text + "'"
in you SELECT statement. This prevents SQL Injection Attacks and provides better performance. See Microsoft help file documentation for
SQLParameter Class[
^]