[ad_1]
That indicates that the Value
of the cell in that column for at least one row was Nothing
, which won’t work with ADO.NET. If you want to insert NULL then you have to use DBNull.Value
. The way you’re doing things, that means explicitly checking for Nothing
. You also should not be using AddWithValue
, but rather calling Add
and specifying the data type and, optionally, the size, e.g.
cmd.Parameters.Add("@KodeBuku", SqlDbType.VarChar, 50).Value = If(row.Cells("kode").Value, DBNull.Value)
I’m not 100% sure without testing but you may have to use CObj(DBNull.Value)
. Note that that If
operator will return the first argument if it’s not Nothing
and the second if it is.
A better option would be to not use a loop to call ExecuteNonQuery
in the first place. You should create a DataTable
with the appropriate schema and bind that to the grid. You can then use a data adapter with an appropriate InsertCommand
to save all the data with a single call to Update
. Every field in a DataTable
contains DBNull.Value
by default, so there’s no need to do any check for Nothing
.
[ad_2]