带有“Not IsDBNull”的datagridview中的空引用

时间:2013-09-13 08:08:55

标签: vb.net datagridview dbnull

在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'类型的未处理异常” 在第二行。我看不出这个的原因......有什么想法吗?

0 个答案:

没有答案