当前位置: 代码迷 >> ASP.NET >> asp.net高分:Object reference not set to an instance of an object
  详细解决方案

asp.net高分:Object reference not set to an instance of an object

热度:4832   发布时间:2013-02-26 00:00:00.0
asp.net高分求助:Object reference not set to an instance of an object
我查了论坛里很多相关的问题,大概是对象没有实例化的问题,但还是不知道解决方法,希望得到大家的帮助!

Exception   Details:   System.NullReferenceException:   Object   reference   not   set   to   an   instance   of   an   object.

Source   Error:  

Line   82:  
Line   83:  
Line   84:                           string   teacherName   =   ds.Tables[ "teacherName "].NewRow().ToString();  

贴出源码如下:

          string   strSql   =   "SELECT   college.collegeName,   teacher.teacherID,   teacher.teacherName,   college.collegeID   FROM   teacher   INNER   JOIN   college   ON   teacher.collegeID   =   college.collegeID   WHERE   (teacher.teacherID   =   @teacherID) ";

          string   sconn=ConfigurationSettings.AppSettings[ "conn "];
          SqlConnection   conn=   new   SqlConnection(sconn);
          conn.Open();
          SqlParameter   para   =   new   SqlParameter( "@teacherID ",SqlDbType.NVarChar);

          para.Value   =Session[ "sUser "].ToString();
          SqlDataAdapter   da   =   new   SqlDataAdapter(strSql,conn);
          da.SelectCommand.Parameters.Add(para);

          DataSet   ds   =   new   DataSet();
          da.Fill(ds);
          string   teacherName   =   ds.Tables[ "teacherName "].NewRow().ToString();  
          conn.Close();
 

 


------解决方案--------------------------------------------------------
string teacherName;
if(ds.Tables[0].Rows.Count> 0&&ds.Tables[0].Rows[0][ "teacherName "]!=DBNull.Value)
teacherName = ds.Tables[0].Rows[0][ "teacherName "].ToString();

  相关解决方案