在 VB.net 2012 中,这是我的代码:
For Each row As DataGridViewRow In DataGridViewAddPlanning.Rows
If Not IsDBNull(row.Cells(0).Value) Then
Dim sqlcmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
sqlcmd.Connection = sqlConn
sqlcmd.CommandText = sqlQuery
sqlcmd.Connection = sqlConn
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = sqlQuery
sqlcmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = LabelUserName.Text
sqlcmd.Parameters.Add("@ActiveDate", SqlDbType.Date).Value = Now.ToShortDateString
sqlcmd.Parameters.Add("@Cat", SqlDbType.VarChar).Value = "User's Planning"
sqlcmd.Parameters.Add("@Task", SqlDbType.VarChar).Value = row.Cells(0).Value.ToString
sqlcmd.Parameters.Add("@DateStarted", SqlDbType.Date).Value = Now.ToShortDateString
Try
sqlConn.Open()
sqlcmd.ExecuteNonQuery()
Catch ex As SqlException
MessageBox.Show("The task from line " & row.Index.ToString & " could not be added. Error Message:", ex.Message.ToString())
End Try
End If
Next
我在第二行收到错误消息:“发生'System.NullReferenceException' 类型的未处理异常”。我看不出这是什么原因..有什么想法吗?