异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 27:  {
行 28:  DataRow SmallClassName =DBOperate.GetDataRow("select * from Small_Class where  SmallClass_ID="+SmallClass_ID,"Small_Class","DBConnection");
行 29:  return SmallClassName["SmallClass_ID"].ToString();
行 30:  }
行 31:  }
 源文件: c:\Inetpub\wwwroot\TragramMini\WebTragMini\App_Code\Navigation.cs    行: 29 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   TangramMiniWeb.Navigation.GetSmallClassName(Int32 SmallClass_ID) in c:\Inetpub\wwwroot\TragramMini\WebTragMini\App_Code\Navigation.cs:29
   TangramMiniWeb.pith.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\TragramMini\WebTragMini\tan\pith.aspx.cs:132
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061它的源码:
public static string  GetBigClassName(int BigClass_ID)
{
DataRow BigClassName =DBOperate.GetDataRow("select * from Big_Class where BigClass_ID="+BigClass_ID,"Big_Class","DBConnection");
            return BigClassName["BigClass_ID"].ToString();
        

} public static string GetSmallClassName(int SmallClass_ID)
{
DataRow SmallClassName =DBOperate.GetDataRow("select * from Small_Class where  SmallClass_ID="+SmallClass_ID,"Small_Class","DBConnection");
return SmallClassName["SmallClass_ID"].ToString();-------此句有问题,就是它测试不过去的?????????
}
用到的数据库的表为:
CREATE TABLE [dbo].[Small_Class](
[SmallClass_Id] [int] IDENTITY(1,1) NOT NULL,
[BigClass_Id] [int] NULL,
[SmallClass_Name] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
[Enable] [int] NULL,
[Memo] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL
primary key ([SmallClass_Id])
)
 

解决方案 »

  1.   

    应该是通过你的SQL语句没有取到对应的行数据,即SmallClassName为null中断看看你的BigClassName是否为null
      

  2.   

    DataRow SmallClassName =DBOperate.GetDataRow("select * from Small_Class where  SmallClass_ID="+SmallClass_ID,"Small_Class","DBConnection");
    return SmallClassName["SmallClass_ID"].ToString(); //这句出错误???SmallClassName可能没有得到值SmallClassName["SmallClass_ID"]就会出错.
    断点看SmallClassName是否能得到值.没得到那就是DBOperate.GetDataRow(...)有问题了.
      

  3.   

    这是DBOperate.GetDataRow()的源码:
    //返回一条记录
            public static DataRow GetDataRow(string sql, string TableName, string dbname)
            {            SqlConnection sqlConnection = new SqlConnection(strCon);
                SqlDataAdapter sqlAdapter1 = new SqlDataAdapter(sql, sqlConnection);
                DataSet product = new DataSet();
                sqlAdapter1.Fill(product, TableName);
                return product.Tables[0].Rows.Count == 0 ? null : product.Tables[0].Rows[0];
            }
      

  4.   

    我发现是SmallClassName没有返回值所造成的,我数据库中有数据。这个我已经可以确认了。
      

  5.   

    应该是你那个查询语句 select * from Small_Class where  SmallClass_ID="+SmallClass_ID,"Small_Class","DBConnection" 返回一个NULL,使得
    DataRow SmallClassName = null了。
      

  6.   

    返回值為null,就會有這樣的錯誤
      

  7.   

    DataRow   SmallClassName   =DBOperate.GetDataRow("select   *   from   Small_Class   where     SmallClass_ID='" + SmallClass_ID +"'","Small_Class","DBConnection"); 
    改成这样试试,貌似是字符串少了两个单引号
      

  8.   

    不是数据库的问题
    DataRow   SmallClassName   =DBOperate.GetDataRow("select   *   from   Small_Class   where     SmallClass_ID= '" + SmallClass_ID +" '","Small_Class","DBConnection");
    我就用这个测试的select * from Small_Class where  SmallClass_ID=1结果还是
    异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例。
    这进一步缩小了范围。
    问题就在:return   SmallClassName["SmallClass_ID"].ToString();
    还可能是    public   static   DataRow   GetDataRow(string   sql,   string   TableName,   string   dbname) 
                    {                         SqlConnection   sqlConnection   =   new   SqlConnection(strCon); 
                            SqlDataAdapter   sqlAdapter1   =   new   SqlDataAdapter(sql,   sqlConnection); 
                            DataSet   product   =   new   DataSet(); 
                            sqlAdapter1.Fill(product,   TableName); 
                            return   product.Tables[0].Rows.Count   ==   0   ?   null   :   product.Tables[0].Rows[0]; ?????????????????????????????有问题
                    } 
      

  9.   

    源文件: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7128eae2\3d99563e\App_Web_z2q25dsb.16.cs    行: 0 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       Discuz.ForumPage.showforum.ShowPage() +779
       Discuz.Web.UI.Pages.BasePage..ctor() +6443
       Discuz.ForumPage.showforum..ctor() +451
       ASP.aspx_1_showforum_aspx..ctor() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7128eae2\3d99563e\App_Web_z2q25dsb.16.cs:0
       __ASP.FastObjectFactory_app_web_z2q25dsb.Create_ASP_aspx_1_showforum_aspx() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7128eae2\3d99563e\App_Web_z2q25dsb.19.cs:0
       System.Web.Compilation.BuildResultCompiledType.CreateInstance() +49
       System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +115
       System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +31
       System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
       System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +139
       System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +128
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +161
         我刚开始学···不懂···请问前辈们这个应该怎么解决····?
     
      

  10.   

    SmallClassName[ "SmallClass_ID "]
    是不是为空呀。
    我的已经解决了,原因是
    Request.QueryString["id"]为空了。
    希望能给你参考一下。
      

  11.   

    肯定可以解决的方法:是权限的问题:把整个文件夹只读属性去掉,设置everyone 完全控制.