问题描述
老师让编一个用户登陆的,我是个初学者,遇到这个问题整了半天了也没弄好,论坛的老师们帮忙看看哪错了未将对象引用设置到对象的实例。说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例。源错误:行9:ProtectedSubPage_Load(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMe.Load行10:IfPage.IsPostBackThen行11:DimconnAsConnDB=NewConnDB()行12:DimstrSQLAsString行13:conn.DBOpen()源文件:D:Backup我的文档VisualStudio2005WebSitesregeditregedit.aspx.vb行:11堆栈跟踪:[NullReferenceException:未将对象引用设置到对象的实例。]ConnDB..ctor()+48regedit.Page_Load(Objectsender,EventArgse)inD:Backup我的文档VisualStudio2005WebSitesregeditregedit.aspx.vb:11System.Web.UI.Control.OnLoad(EventArgse)+80System.Web.UI.Control.LoadRecursive()+49System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+3750
解决方案
解决方案二:
ConnDB就是一个实现链接数据库的vb类下面是代码ImportsMicrosoft.VisualBasicImportsSystem.Data.OleDbImportsSystem.DataPublicClassConnDBInheritsSystem.Web.UI.PagePrivateconnStrAsStringPrivateobjConAsOleDbConnectionPrivateobjCmdAsOleDbCommandPrivateobjDSAsData.DataSetPrivateobjDRAsOleDbDataReaderPrivateobjDAAsOleDbDataAdapterPublicSubNew()connStr="Provider="&System.Web.Configuration.WebConfigurationManager.ConnectionStrings("servername").ProviderNameconnStr&="DataSource="&System.Web.HttpContext.Current.Server.MapPath(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("severname").ConnectionString())objCon=NewOleDbConnection(connStr)EndSubPublicSubDBOpen()'打开数据库连接objCon.Open()EndSubPublicSubDBclose()'关闭数据库TryobjCon.Close()CatchexAsExceptionobjCon.Dispose()EndTryEndSubPublicFunctiongetDataSet(ByValsqlAsString)AsDataSetIfobjCon.State=ConnectionState.ClosedThenDBOpen()EndIfobjDA=NewOleDbDataAdapter(sql,objCon)objDS=NewDataSet()objDA.Fill(objDS,"regedit")ReturnobjDSEndFunction
解决方案三:
下面是首页出错误的代码谢谢各位前辈ImportsSystem.Data.OleDbImportsSystem.DataImportsMicrosoft.VisualBasicPartialClassregeditInheritsSystem.Web.UI.PageProtectedSubPage_Load(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMe.LoadIfPage.IsPostBackThenDimconnAsConnDB=NewConnDB()DimstrSQLAsStringconn.DBOpen()strSQL="SELECT*FROMregedit"DimdatastAsDataSet=NewDataSet()datast=conn.getDataSet(strSQL)DimobjDTrowAsDataRowobjDTrow=datast.Tables("regedit").NewRowobjDTrow("id")=id.TextobjDTrow("pass")=pass.TextDimemailAsStringemail=email1.Text&"@"&email2.TextobjDTrow("email")=emaildatast.Tables("regedit").Rows.Add(objDTrow)conn.DBclose()EndIfEndSubEndClass