indofraiser
indofraiser

Reputation: 1024

IsDBNull in Parameter AddWithValue

Aim: I want to check if the value is Null and it is then add blank otherwise add data

Issue: I am unsure on what I need to put after the first comma to change the Null to "" and also then if it actually has data to import that instead

    With commandSQL
    .Connection = connection
    .CommandText = "spAddCSVDataLine"  'Stored procedure here
    .CommandType = CommandType.StoredProcedure
    .Parameters.AddWithValue("Name", (IsDBNull(ds.Tables("dataExcel").Rows(j)("Name"))),"",Trim(ds.Tables("dataExcel").Rows(j)("Name"))))

I can do the following but I would like to tighten the code up onto one line if possible:

If IsDBNull(ds.Tables("dataExcel").Rows(j)("Name")) Then
.Parameters.AddWithValue("Name", "")
Else
.Parameters.AddWithValue("Name", Trim(ds.Tables("dataExcel").Rows(j)("Name")))
End If

Upvotes: 1

Views: 202

Answers (2)

Aaron Palmer
Aaron Palmer

Reputation: 8992

I think you're looking for the If operator:

With commandSQL
    .Connection = connection
    .CommandText = "spAddCSVDataLine"  'Stored procedure here
    .CommandType = CommandType.StoredProcedure
    .Parameters.AddWithValue("Name", If(IsDBNull(ds.Tables("dataExcel").Rows(j)("Name")), "", Trim(ds.Tables("dataExcel").Rows(j)("Name"))))
End With

Upvotes: 3

jmcilhinney
jmcilhinney

Reputation: 54417

Dim row = ds.Tables("dataExcel").Rows(j)

.Parameters.AddWithValue("@Name", If(row.IsNull("Name"), String.Empty, CStr(row("Name"))))

Upvotes: 3

Related Questions