在插入查询对象引用未处理的异常VB.Net
问题描述:
这是我在vb.net的代码,当我通过从VB.net插入查询到SQL服务器我在查询异常该对象引用未设置为实例对象, 在sql文件中允许为null,ID为自动增加。我的同伴的 一个说,使用记录集,并告诉条件的财产以后这样的,但我已经完全不知道有关的新的VB.net, 请帮我订的记录,我应该怎么办,在插入查询对象引用未处理的异常VB.Net
崩溃|复制代码
Imports System.Data
Imports System.Data.SqlClient
Public Class MaintenanceTask
Dim cn As New System.Data.SqlClient.SqlConnection
Sub connect()
cn = New System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=Fleet Maintainance;Integrated Security=True")
End Sub
Sub lockall()
cBx1.Enabled = False
cBx2.Enabled = False
tBx1.Enabled = False
tBx2.Enabled = False
tBx3.Enabled = False
End Sub
Sub unlockall()
cBx1.Enabled = True
cBx2.Enabled = True
tBx1.Enabled = True
tBx2.Enabled = True
tBx3.Enabled = True
End Sub
Sub setall()
cBx1.Text = ""
cBx2.Text = ""
tBx1.Text = ""
tBx2.Text = ""
tBx3.Text = ""
End Sub
Sub updatecombo1()
Call connect()
Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [Name],[NameID] FROM [RepairName] order by [Name]", cn)
Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
Dim ds As New DataSet
adp.Fill(ds)
'ComboBox2.Items.Clear()
cBx1.DisplayMember = "Name"
cBx1.ValueMember = "NameID"
cBx1.DataSource = ds.Tables(0)
End Sub
Sub updatecombo2()
Call connect()
Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [RepairType],[RepairTypeID] FROM [RepairType] order by [RepairType]", cn)
Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
Dim ds As New DataSet
adp.Fill(ds)
'ComboBox2.Items.Clear()
cBx2.DisplayMember = "RepairType"
cBx2.ValueMember = "RepairTypeID"
cBx2.DataSource = ds.Tables(0)
End Sub
Sub updatecombo3()
Call connect()
Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [Service],[ServiceID] FROM [Service] order by [Service]", cn)
Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
Dim ds As New DataSet
adp.Fill(ds)
'ComboBox2.Items.Clear()
cBx1.DisplayMember = "Service"
cBx1.ValueMember = "ServiceID"
cBx1.DataSource = ds.Tables(0)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If cBx1.Enabled = True Then
Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, LaborCost, Total) values (" & (cBx1.SelectedValue.ToString()) & ", " & (cBx2.SelectedValue.ToString()) & ", " & CInt(tBx1.Text) & "," & CInt(tBx2.Text) & "," & CInt(tBx3.Text) & ")"
Call connect()
Dim cd As New System.Data.SqlClient.SqlCommand(str, cn)
cd.Connection.Open()
cd.ExecuteNonQuery()
cd.Connection.Close()
MsgBox(" New Task is added successfully ")
Call lockall()
Me.Close()
Call IssueWorkOrder.listView2load()
Else
MsgBox(" Task is not added Try again ")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
AddService.Show()
AddService.Visible = True
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Text = "Preventive"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
RadioButton2.Text = "Repair"
End Sub
Private Sub RadioButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.Click
Call updatecombo3()
Label3.Text = "Service"
cBx2.Enabled = False
End Sub
Private Sub RadioButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.Click
Call updatecombo1()
Call updatecombo2()
Label3.Text = "Repair"
cBx2.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
答
您的主要问题是您的插入字符串。 你不检查组合的值是有效还是无效,并调用toString在VB项目NULL(没有)
Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, " & _
"LaborCost, Total) values (" & (cBx1.SelectedValue.ToString()) & _
", " & (cBx2.SelectedValue.ToString()) & ", " & CInt(tBx1.Text) & _
"," & CInt(tBx2.Text) & "," & CInt(tBx3.Text) & ")"
如果cBx1.SelectedValue为NULL你不能调用的方法(的ToString )在一个NULL对象上,这是收到的NullReferenceException。 但是你有一个名为Sql Injection一个更严重的问题(请阅读文章)
Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, " & _
"LaborCost, Total) values (@main, @tp, @cost, @labcost, @tot)"
Dim cd As New SqlCommand(str, cn)
cd.Parameters.AddWithValue("@main", if(cbx1.SelectedValue == null, DBNull.Value, cbx1.SelectedValue.ToString)
cd.Parameters.AddWithValue("@tp", if(cbx2.SelectedValue == null, DBNull.Value, cbx2.SelectedValue.ToString)
cd.Parameters.AddWithValue("@cost", Convert.ToInt32(tBx1.Text))
cd.Parameters.AddWithValue("@labcost", Convert.ToInt32(tBx2.Text))
cd.Parameters.AddWithValue("@totale", Convert.ToInt32(tBx3.Text))
cd.Connection.Open()
cd.ExecuteNonQuery()
cd.Connection.Close()
MsgBox(" New Task is added successfully ")
Call lockall()
Me.Close()
Call IssueWorkOrder.listView2load()
其他问题转化为一个文本框的整数。
如果输入不是有效的数值,你会得到一个错误,你需要一点错误处理之前 连接对象是一个稀缺资源,你应该确保完成后关闭。你的代码没有错误处理,万一错误,你的连接永远不会关闭。
+0
即使没有错误,连接也没有关闭和处置 – user1937198 2013-03-17 14:19:48
你为什么不处理你的连接? – user1937198 2013-03-16 12:17:05