我创建了学生考勤管理数据库。我使用了sql server
和VB12
。我的问题是我从我的表中创建了2个数据集。一个数据集没有注册号和名称。和其他人没有注册,姓名,出勤率,总数和百分比。我成功地使用第一个数据集添加了reg no和name。使用第二个数据集我必须输入出勤率并找到新创建的寄存器号的总数和百分比。当我输入出勤并单击我的添加按钮时,我收到错误消息msg tat + operator未定义为db null。
我有两个按钮连接和添加。我的按钮代码是,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
connetionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ADMIN\Documents\Visual Studio 2012\Projects\studentattendance\studentattendance\attnd\details.mdf;Integrated Security=True"
connection = New SqlConnection(connetionString)
sql = "select * from entry"
Try
connection.Open()
adapter = New SqlDataAdapter(sql, connection)
adapter.Fill(ds)
connection.Close()
EntryDataGridView.DataSource = ds.Tables(0)
MsgBox("connected")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
connetionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ADMIN\Documents\Visual Studio 2012\Projects\studentattendance\studentattendance\attnd\details.mdf;Integrated Security=True"
connection = New SqlConnection(connetionString)
sql = "select * from entry"
Try
connection.Open()
adapter = New SqlDataAdapter(sql, connection)
cmdBuilder = New SqlCommandBuilder(adapter)
adapter.Fill(ds)
For i = 0 To ds.Tables(0).Rows.Count - 1
ds.Tables(0).Rows(i).Item(9) = ds.Tables(0).Rows(i).Item(2) + ds.Tables(0).Rows(i).Item(3) + ds.Tables(0).Rows(i).Item(4) + ds.Tables(0).Rows(i).Item(5) + ds.Tables(0).Rows(i).Item(6) + ds.Tables(0).Rows(i).Item(7) + ds.Tables(0).Rows(i).Item(8)
ds.Tables(0).Rows(i).Item(10) = ds.Tables(0).Rows(i).Item(9) / 7
Next
adapter.Update(ds.Tables(0))
connection.Close()
MsgBox("Data updated ! ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
请帮帮我......
答案 0 :(得分:2)
" +运算符未定义为db null。"
确实没有定义。
您需要检查该字段是否包含DBNull值。
IsDBNull(ds.Tables(n).Rows(n).Item(n))
例如,如果第3列和第4列的数据类型设置为Integer:
Dim item2 As Object = ds.Tables(0).Rows(i).Item(2)
Dim item3 As Object = ds.Tables(0).Rows(i).Item(3)
Dim result As Integer = (
If(IsDBNull(item2), 0I, CInt(item2)) +
If(IsDBNull(item3), 0I, CInt(item3))
)
你应该尽快转向Option Strict ON
,因为这样可以防止你将来犯这种错误。