使用下面的代码得到空引用错误 [英] Am getting a null reference error with the code below
问题描述
好的大师我收到此错误IBCARIP.exe中发生了'System.NullReferenceException'类型未处理的异常
附加信息:对象引用未设置为一个对象的实例。
当我点击保存按钮时会发生这种情况。我想做的是在工资计算中保存来自labells和textboxes的信息表单到访问数据库。代码必须检查表名,如果存在则将值插入新记录中,否则它会创建表,记录和更新。我突出显示代码中的错误点,
gooday gurus i am getting this error "An unhandled exception of type 'System.NullReferenceException' occurred in IBCARIP.exe
Additional information: Object reference not set to an instance of an object."
this happens when i click the save button.what i am trying to do is to save information from labells and textboxes on a salary calculation form to an access database. the code has to check for the table name, if it exists it inserts the values into a new record, if not then it creates both table and record and updates. I HAVE HIGHLIGHTED WHERE THE ERROR POINTS TO IN THE CODE,
Private Sub lblPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblPrint.Click
Dim Cn As New OleDb.OleDbConnection
Cn.ConnectionString = ("Provider=Microsoft.ace.OLEDB.12.0;Data Source=C:\Users\Blessing\Documents\IBCARIP.accdb")
'Open the connection
Cn.Open()
'Get database schema
Dim dbSchema As DataTable = Cn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, "Jongilanga_High", "TABLE"})
Cn.Close()
' If the table exists, the count = 1
If dbSchema.Rows.Count > 0 Then
' do whatever you want to do if the table exists
Cn.Open()
Dim sql2 As String = _
"insert into Jongilanga_High(EmpCode, Surname&Initials, Days, NRate, Total,Hours,O_T Rate,T_Overtime,Gross,UIF,Deductions,T_Deductions,Net_Salary) " _
& "values('" & txtID.Text & "', '" _
& lblSAI.Text & "', " & lblSAI.Text & "', " & txtDays.Text & "','" & lblRate.Text & "','" & lblTSal.Text & "','" _
& txtHours.Text & "','" & lblOT.Text & "','" & lblTOvertym.Text & "','" & lblGross.Text & "','" & lblUIF.Text & "','" _
& txtDed.Text & "','" & lblTDed.Text & "','" & lblNet.Text & ")"
Dim da2 As New OleDb.OleDbDataAdapter(sql2, Cn)
Dim dsC As New DataSet
da2.InsertCommand.CommandText = sql2
da2.InsertCommand.ExecuteNonQuery()
Cn.Close()
Cn.Open()
Dim sql3 As String = "update Jongilanga_High set = '" & txtID.Text & "', '" _
& lblSAI.Text & "', " & lblSAI.Text & "', " & txtDays.Text & "','" & lblRate.Text & "','" & lblTSal.Text & "','" _
& txtHours.Text & "','" & lblOT.Text & "','" & lblTOvertym.Text & "','" & lblGross.Text & "','" & lblUIF.Text & "','" _
& txtDed.Text & "','" & lblTDed.Text & "','" & lblNet.Text & ")"
da2.UpdateCommand.CommandText = sql3 <<<<<<ERROR POINTS THIS LINE<<<<<< da2.UpdateCommand.ExecuteNonQuery()
Cn.Close()
Exit Sub
MsgBox("New Record added to the Database")
Else
'Try
'do whatever you want to do if the table does not exist
' e.g. create a table
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE [" + lblCCS.Text + "] ([EmpCode] TEXT(30), [Surname&Initials] TEXT(50), [Days] text(5), [NRate] text(10), [Total] text(20), [Hours] text(5), [O_T Rate] text(20), [T_Overtime] text(20), [Gross] text(20), [UIF] text(20), [Deductions] text(20), [T_Deductions] text(20), [Net_Salary] text (20))", Cn)
Cn.Open()
cmd.ExecuteNonQuery()
Cn.Close()
Try
Cn.Open()
Dim sql2 As String = _
"insert into Jongilanga_High(EmpCode, Surname&Initials, Days, NRate,Total,Hours,O_T Rate,T_Overtime,Gross,UIF,Deductions,T_Deductions,Net_Salary) " _
& "values('" & txtID.Text & "', '" _
& lblSAI.Text & "', " & lblSAI.Text & "', " & txtDays.Text & "','" & lblRate.Text & "','" & lblTSal.Text & "','" _
& txtHours.Text & "','" & lblOT.Text & "','" & lblTOvertym.Text & "','" & lblGross.Text & "','" & lblUIF.Text & "','" _
& txtDed.Text & "','" & lblTDed.Text & "','" & lblNet.Text & ")"
Dim da2 As New OleDb.OleDbDataAdapter(sql2, Cn)
Dim dsC As New DataSet
da2.InsertCommand.CommandText = sql2
da2.InsertCommand.ExecuteNonQuery()
Catch exceptionObject As Exception
MsgBox(exceptionObject.Message)
Finally
Cn.Close()
End Try
Try
Cn.Open()
Dim sql3 As String = "update Jongilanga_High set = '" & txtID.Text & "', '" _
& lblSAI.Text & "', " & lblSAI.Text & "', " & txtDays.Text & "','" & lblRate.Text & "','" & lblTSal.Text & "','" _
& txtHours.Text & "','" & lblOT.Text & "','" & lblTOvertym.Text & "','" & lblGross.Text & "','" & lblUIF.Text & "','" _
& txtDed.Text & "','" & lblTDed.Text & "','" & lblNet.Text & ")"
Dim da2 As New OleDb.OleDbDataAdapter(sql3, Cn)
Dim dsC As New DataSet
da2.UpdateCommand.CommandText = sql3
da2.UpdateCommand.ExecuteNonQuery()
Catch ex As Exception
'
MsgBox(ex.Message)
Finally
Cn.Close()
End Try
MsgBox("New Record added to the Database")
End If
推荐答案
因此,在该行上放置一个断点并查看变量。当您发现哪一个为null时,将断点置高并再次运行。这一次,单步查看变为null的值,并找出它从not-null切换到null的时间。
So, put a breakpoint on the line and look at the variables. The when you find out which one is null, put a breakpoint higher up and run it again. This time, single step through look at the value that became null, and find out when it switched from "not-null" to "null".
这篇关于使用下面的代码得到空引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!