Null Reference in datagridview with "Not IsDBNull"

672 Views Asked by At

IN VB.net 2012 this is my code:

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

I get the error: "An unhandled exception of type 'System.NullReferenceException' occurred" at the second line. I cant see the reason for this.. Any Ideas?

0

There are 0 best solutions below