求助,我用VB.NET生成的EXE文件运行报错:未将对象引用设置到对象的实例,蛋疼死了!
我编得一个SQL数据库有关的软件,在VisualStudion2012中调试都OK了,但生成EXE文件以后运行就报错,在网上查了很多也没能解决问题,求各位大神帮助呀!我现...
我编得一个SQL数据库有关的软件,在Visual Studion2012中调试都OK了,但生成EXE文件以后运行就报错,在网上查了很多也没能解决问题,求各位大神帮助呀!我现在只留下登陆界面的相关代码,请会的人帮我看看,感激不尽!
登陆界面代码:
Public Class denglu
Dim dengluyanzhengq As TensionInfoDB
Dim username As String
Public Sub Btnqueren_Click(sender As Object, e As EventArgs) Handles btnqueren.Click
dengluyanzhengq = New TensionInfoDB()
username = New String(txtyonghuming.Text)
If (username <> "") Then
Dim details As UsersDetail
details = New UsersDetail()
details = dengluyanzhengq.Getmima(username)
If details.Password = " " Then
MsgBox("用户不存在!")
ElseIf txtmima.Text = details.Password Then
MsgBox("登录成功")
Else
MsgBox("密码错误!")
End If
Else
MsgBox("请输入用户名!")
End If
End Sub
End Class
TensionInfoDB代码(数据库相关操作):
Imports System.Data.SqlClient
Public Class TensionInfoDB
Public connectionString As String
Dim dengluyanzheng As UsersDetail
Dim reader As SqlDataReader
Dim con As SqlConnection
Dim cmd As SqlCommand
Public Sub New()
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Tension").ConnectionString
End Sub
Public Function Getmima(ByVal username As String) As UsersDetail
dengluyanzheng = New UsersDetailonString)
cmd = New SqlCommand("", con)
cmd.CommandText = "SELECT * FROM Users WHERE UserName='" & username & "'"
Try
con.Open()
dengluyanzheng.Password = " "
reader = cmd.ExecuteReader()
While reader.Read
dengluyanzheng.UserName = reader("UserName")
dengluyanzheng.Password = reader("Password")
dengluyanzheng.Grade = reader("Grade")
End While
reader.Close()
Catch ex As SqlException
dengluyanzheng = Nothing
Throw New ApplicationException(ex.Message)
Finally
con.Close()
End Try
Return dengluyanzheng
End Function() 展开
登陆界面代码:
Public Class denglu
Dim dengluyanzhengq As TensionInfoDB
Dim username As String
Public Sub Btnqueren_Click(sender As Object, e As EventArgs) Handles btnqueren.Click
dengluyanzhengq = New TensionInfoDB()
username = New String(txtyonghuming.Text)
If (username <> "") Then
Dim details As UsersDetail
details = New UsersDetail()
details = dengluyanzhengq.Getmima(username)
If details.Password = " " Then
MsgBox("用户不存在!")
ElseIf txtmima.Text = details.Password Then
MsgBox("登录成功")
Else
MsgBox("密码错误!")
End If
Else
MsgBox("请输入用户名!")
End If
End Sub
End Class
TensionInfoDB代码(数据库相关操作):
Imports System.Data.SqlClient
Public Class TensionInfoDB
Public connectionString As String
Dim dengluyanzheng As UsersDetail
Dim reader As SqlDataReader
Dim con As SqlConnection
Dim cmd As SqlCommand
Public Sub New()
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Tension").ConnectionString
End Sub
Public Function Getmima(ByVal username As String) As UsersDetail
dengluyanzheng = New UsersDetailonString)
cmd = New SqlCommand("", con)
cmd.CommandText = "SELECT * FROM Users WHERE UserName='" & username & "'"
Try
con.Open()
dengluyanzheng.Password = " "
reader = cmd.ExecuteReader()
While reader.Read
dengluyanzheng.UserName = reader("UserName")
dengluyanzheng.Password = reader("Password")
dengluyanzheng.Grade = reader("Grade")
End While
reader.Close()
Catch ex As SqlException
dengluyanzheng = Nothing
Throw New ApplicationException(ex.Message)
Finally
con.Close()
End Try
Return dengluyanzheng
End Function() 展开
1个回答
展开全部
“未将对象引用到对象的实例”应该就是没有实例化或者没有初始化的问题。
具体是哪里出的问题?登录还是按钮?
具体是哪里出的问题?登录还是按钮?
追问
属入用户名密码之后点登录就弹出那个问题了。。。。实例化我百度了一下改了改,可能还没有改完,不知道哪块还没有实例化
追答
你的SqlConnection 实例化了吗?
另外我感觉应该先 con.Open(),
然后再 cmd = New SqlCommand("", con)
不过如果你调试能过的话应该就不是这些问题,如果不是这些问题你可以打几个输出点看一看是哪句代码出错了,多加几个messagebox.show()这样的代码可以调试出哪的问题...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询