I am iterating through some rows/cells in an Excel sheet and some of the cells are blank. When I try to do the insert, if a cell contains no data, it advises me that that particular parameter has no default value. In Access, I have set the default value to “=Null” but I still receive the error. Do I have to do an If statement as shown below for every parameter in order to prevent errors? I’d like to clean up my code if possible.
If worksheet1.GetValue(i, 14) Is Nothing Then
cmd.Parameters.Add("@Param1", OleDbType.Char).Value = DBNull.Value
Else
cmd.Parameters.Add("@Param1", OleDbType.Char).Value = worksheet1.GetValue(i, 14)
End If
In my experience (albeit C#), yes you do. Interop (which is what I’m assuming you’re using although I don’t recognise the
GetValue()call) usually throws an exception when you try to get data from an empty cell without first checking. The way I’ve handled it is to wrap the code in a method and just call it each time (sorry, my VB is a little rusty):And then you can just do: