在尝试对LABEL进行valorise时,对象引用未设置为对象的实例

时间:2015-05-18 11:39:44

标签: c#

你能帮帮我吗? 当我尝试使用SqlDataReader的值对标签进行valorise时,我总是将消息(对象引用未设置为对象的实例。)。 你可以找到我的代码。问题出在Bold中。

protected void Submit_Click(object sender, EventArgs e) 
{ 

   string cname1;

   string selectSQL = 
     "SELECT candidate.Name as cName, IDGroupe, " 
      + "ExamName, groupTab.Name as gName " 
      + "FROM candidate INNER JOIN groupTab ON " 
      + "[groupTab].ID = candidate.IDGroupe " 
      + "WHERE candidate.ID = " 
      + "@ID"; 

     SqlConnection con = new SqlConnection(connectionString); 

     SqlCommand cmd = new SqlCommand(selectSQL, con); 

     cmd.Parameters.AddWithValue("@ID", HiddenField1.Value); 


     SqlDataReader reader; 

     try 
     { 
          con.Open(); 

          reader = cmd.ExecuteReader(); 

          if (reader.Read()) 
          { 

               if (reader.GetString(0) != null) 

               { 

                    cname1 = reader.GetString(0); 

                    **CName.Text = cname1;** 

                } 

           } 

           reader.Close(); 

      } 

      catch (Exception err) 

      { 

           lblResult.Text = "Error reading list of names"; 

           lblResult.Text += err.Message; 

      } 

      finally 

      { 

           con.Close(); 

      } 

 } 

谢谢, Hadil

0 个答案:

没有答案