Question

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
Était-ce utile?

La solution

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

Autres conseils

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

.Parameters.AddWithValue("@Name", If(row.IsNull("Name"), String.Empty, CStr(row("Name"))))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top