首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

未将对象引用设置到对象的实例。解决方法

2012-01-08 
未将对象引用设置到对象的实例。急未将对象引用设置到对象的实例。说明:执行当前Web请求期间,出现未处理的异

未将对象引用设置到对象的实例。急
未将对象引用设置到对象的实例。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例。

源错误:  


行   35:                           sqlconn   =   New   SqlConnection(constr)
行   36:  
行   37:                           sqlconn.Open()
行   38:                           Dim   daohang   As   SqlCommand
行   39:                           Dim   daohangreader   As   SqlDataReader
 
--------------
行   37:                           sqlconn.Open()报错

源码如下:

Imports   System.Data.SqlClient
Imports   System.Data

Public   Class   top
        Inherits   System.Web.UI.UserControl

#Region   "   Web   窗体设计器生成的代码   "

        '该调用是   Web   窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()>   Private   Sub   InitializeComponent()

        End   Sub
        Protected   WithEvents   ImageButton1   As   System.Web.UI.WebControls.ImageButton
        Protected   WithEvents   Label1   As   System.Web.UI.WebControls.Label

        '注意:   以下占位符声明是   Web   窗体设计器所必需的。
        '不要删除或移动它。
        Private   designerPlaceholderDeclaration   As   System.Object

        Private   Sub   Page_Init(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Init
                'CODEGEN:   此方法调用是   Web   窗体设计器所必需的
                '不要使用代码编辑器修改它。
                InitializeComponent()
        End   Sub

#End   Region

        'Protected   root   As   String   =   ConfigurationSettings.AppSettings( "webroot ")
        Protected   webtitle   As   String   =   ConfigurationSettings.AppSettings( "webtitle ")
        Private   constr   As   String   =   ConfigurationSettings.AppSettings( "app_connstr ")
        Private   Sub   Page_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load
                '在此处放置初始化页的用户代码
                If   Not   Page.IsPostBack   Then
                        Dim   sqlconn   As   SqlConnection
                        sqlconn   =   New   SqlConnection(constr)



                        sqlconn.Open()
                        Dim   daohang   As   SqlCommand
                        Dim   daohangreader   As   SqlDataReader

                        daohang   =   New   SqlCommand( "select   Video_TypeName,Video_TypeID   from   tb_VideoType ",   sqlconn)
                        daohangreader   =   daohang.ExecuteReader()

                        While   daohangreader.Read
                                Label1.Text   +=   "|&nbsp; <A   href= 'list.aspx?type_id= "   +   daohangreader.Item( "Video_TypeID ").ToString   +   " '> "   +   daohangreader.Item( "Video_TypeName ").ToString   +   " </a> &nbsp; "
                        End   While

                        daohangreader.Close()
                        sqlconn.Close()
                End   If
        End   Sub

End   Class
--------------
Web.config

<configuration>
<appSettings>

<add   key= "app_connstr "   value= "Data   Source=.;uid=sa;pwd=;database=eMedia "/>
-------------
Web.config在根目录下,报错文件在次目录下
--------
断点调试.报这个错误
ServerVersion <error:   an   exception   of   type:   {System.InvalidOperationException}   occurred> String


[解决办法]
<add key= "app_connstr " value= "Data Source=.;uid=sa;pwd=;database=eMedia "/>

你的數據庫鏈接字符串有問題。

"Data Source=Aron1;Initial Catalog=pubs;User Id=myUsername;Password=myPassword; "

具體的 鏈接方法看這個網站
http://www.connectionstrings.com/index.asp

[解决办法]
<add key= "app_connstr " value= "Data Source=.;Initial Catalog=eMedia;persist security info=True;User Id=sa;Password=; "/>

同时检查下Sql的Sa用户密码是否为空
[解决办法]
数据库连接字符串问题,其实不用自己写,有个方法取到:
从工具箱--数据选项卡--拖也个sqldataAdapter组件,它会自动生成这个连接字符串的(配制下就行了).
LZ可以试下
[解决办法]
发个我平常用的一个Sql操作类,用此类试试
'****************************************************************************************************
'类名:clsSqlDb
'说明:SQLServer数据库相关处理类
'引用:System.Data.SqlClient
'编辑:雅博
'****************************************************************************************************

Imports System.Data
Imports System.Data.SqlClient
Public Class clsSqlDb
'1.定义接口
'1-1.数据库连接字符串接口
Private vConnStr As String = " "


Public Property ConnStr() As String
Get
Return vConnStr
End Get
Set(ByVal value As String)
vConnStr = value
End Set
End Property

'1-2.定义传递的信息字符
Private vMessageInfo As String = " "
Public Property MessageInfo() As String
Get
Return vMessageInfo
End Get
Set(ByVal value As String)
vMessageInfo = value
End Set
End Property

'重写New
Public Sub New(Optional ByVal vConnString As String = " ")
If vConnString.Trim <> " " Then ConnStr = vConnString
End Sub


'2.操作的过程函数
'2-1.定义检查数据记录是否存在
Public Function IsRecordExists(ByVal sqlString As String) As Boolean
MessageInfo = " "
Dim myCn As New SqlConnection(ConnStr)
Try
myCn.Open()
Dim myCmd As New SqlCommand(sqlString, myCn)
Dim dr As SqlDataReader = myCmd.ExecuteReader
Dim i As Integer = 0
While dr.Read
i += 1
Exit While
End While
dr.Close()
If i > 0 Then
MessageInfo = "数据记录存在。 "
Return True
Else
MessageInfo = "数据记录不存在。 "
Return False
End If
Catch ex As Exception
MessageInfo = ex.ToString.Trim + "。 "
Return False
Finally
If myCn.State = ConnectionState.Open Then myCn.Close()
myCn = Nothing
End Try
End Function

'2-2.定义执行SQL语句
Public Function ExecuteSqlString(ByVal sqlString As String) As Boolean
Dim myCn As New SqlConnection(ConnStr)
Try
myCn.Open()
Dim myCmd As New SqlCommand(sqlString, myCn)
Dim dr As SqlDataReader = myCmd.ExecuteReader()
MessageInfo = "执行SQL语句完成。 "
Return True
Catch ex As Exception
MessageInfo = ex.ToString.Trim + "。 "
Return False
Finally
If myCn.State = ConnectionState.Open Then myCn.Close()
myCn = Nothing
End Try
End Function

'2-3.定义取得表单中某一个特定条件下的具体字段的值
Public Function GetFieldValueFromTableBySqlString(ByVal sqlStr As String) As String
Dim fieldValueString As String = " "
Dim myCn As New SqlConnection(ConnStr)
Try
myCn.Open()
Dim myCmd As New SqlCommand(sqlStr, myCn)
Dim Dr As SqlDataReader = myCmd.ExecuteReader
While Dr.Read
fieldValueString = Dr(0).ToString.Trim
Exit While
End While
Dr.Close()
Catch ex As Exception
Finally
If myCn.State = ConnectionState.Open Then myCn.Close()
myCn = Nothing
End Try
Return fieldValueString
End Function

'2-4.定义取得表单中某一个特定条件下的具体字段的值
Public Function GetFieldValueFromTableByOtherFieldLimit(ByVal conditionFieldName As String, ByVal conditionFieldValue As String, ByVal GetValueFieldName As String, ByVal tableName As String) As String
Dim sqlStr As String = "select " & GetValueFieldName & " from " & tableName & " where " & conditionFieldName & "= ' " & conditionFieldValue & " ' "
Return GetFieldValueFromTableBySqlString(sqlStr)
End Function

'2-5.获得DataSet
Public Function GetDataSetBySql(ByVal sqlString As String) As DataSet
Try
Dim adapter As SqlDataAdapter = New SqlDataAdapter(sqlString, ConnStr)
Dim ds As New DataSet
adapter.Fill(ds)
Return ds
Catch ex As Exception
Return Nothing
End Try
End Function

'2-6.定义获得DataTable
Public Function GetDataTableBySql(ByVal sqlstring As String) As DataTable


Try
Dim adapter As SqlDataAdapter = New SqlDataAdapter(sqlstring, ConnStr)
Dim tbl As New DataTable
adapter.Fill(tbl)
Return tbl
Catch ex As Exception
Return Nothing
End Try
End Function

'2-7.获得数据项目
Public Function GetDataToStrBySql(ByVal sqlstring As String, ByVal ColSplit As String, ByVal RowSplit As String) As String
If ColSplit = " " Then ColSplit = "| "
If RowSplit = " " Then RowSplit = "√ "
Dim ds As DataSet = GetDataSetBySql(sqlstring)
Dim str As String = " "
If Not ds Is Nothing Then
If ds.Tables.Count > 0 Then
Dim tbl As DataTable = ds.Tables(0)
Dim i As Integer = 0, j As Integer = 0, k As Integer = tbl.Rows.Count
Dim strR(k) As String
Dim row As DataRow
For Each row In tbl.Rows
strR(i) = row(0).ToString.Trim
For j = 1 To tbl.Columns.Count - 1
strR(i) += ColSplit + row(j).ToString.Trim
Next
i += 1
Next
str = strR(0)
For j = 1 To i - 1
str += RowSplit + strR(j)
Next

End If
End If

Return str
End Function

'2-8.定义获取数据库连接字符串
Public Function GetConnectionString(ByVal Param_DataSource As String, ByVal Param_DbName As String, ByVal Param_SSPI As String, ByVal Param_UID As String, ByVal Param_Psw As String) As String
Dim myConnStr As String = "Data Source= " + Param_DataSource + ";Initial Catalog= " + Param_DbName
If LCase(Param_SSPI) = "sspi " Then
myConnStr += ";integrated security=SSPI;persist security info=False; "
Else
myConnStr += ";persist security info=True;user id= " + Param_UID & ";password= " + Param_Psw
End If
Return myConnStr
End Function

'2-9.定义检查数据库服务器连接是否正常
Public Function IsSqlConnectionSucceed(ByVal cnSrv As String, ByVal cnSSPI As String, ByVal cnUid As String, ByVal cnPsw As String) As Boolean
Dim myCn As SqlConnection = Nothing
Dim Param_ConnStr As String = Me.GetConnectionString(cnSrv, "master ", cnSSPI, cnUid, cnPsw)
MessageInfo = " "
Try
myCn = New SqlConnection(Param_ConnStr)
myCn.Open()
If myCn.State = ConnectionState.Open Then
MessageInfo = "数据库连接成功。 "
Return True
Else
MessageInfo = "数据库连接不成功。 "
Return False
End If
Catch ex As Exception
MessageInfo = ex.Message.Trim
Return False
Finally
If myCn.State = ConnectionState.Open Then myCn.Close()
If Not myCn Is Nothing Then myCn = Nothing
End Try
End Function

End Class
[解决办法]
daohang = New SqlCommand( "select Video_TypeName,Video_TypeID from tb_VideoType ", sqlconn)
字段和数据库里字段是否有出入
[解决办法]
你没连上数据库..连接字符串没写错.是你登录错误.
给你一个验证sa密码的方法:
sql-> 查询分析器-> File-> 连接-> sqlserver身份验证...试试你能不能用sa+空密码登录.如果不能就去sql的安全里重设密码.OK.

热点排行