Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to initialize an object with data from database return in a dataset as below:

Example Dataset

Class Pdf
    Public FileId As Integer
    Public AccountNumber As Integer
    Public DateSaved As DateTime
    Public FileName As String
    Public DateImported As DateTime

Scenerio 1 I can intialize the object like this:

Dim pdf = New Pdf With {.FileId = ds.Tables(0).Rows(i)("fileid"),
                        .AccountNumber = ds.Tables(0).Rows(i)("accountnumber"),
                        .DateSaved = ds.Tables(0).Rows(i)("datesaved"),
                        .FileName = ds.Tables(0).Rows(i)("filename"),
                        .DateImported = ds.Tables(0).Rows(i)("dateimported")
                        }

But this is not working, because column data can be null and I am not if how to do a db null check in this approach.

Then I have scenerio 2:

Dim pdf As New pdf
If Not IsDBNull(ds.Tables(0).Rows(i)("fileid")) Then
    PdfFileId = ds.Tables(0).Rows(i)("fileid")
Else
    PdfFileId = 0
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("accountnumber")) Then
    pdf.AccountNumber = ds.Tables(0).Rows(i)("accountnumber")
Else
    pdf.AccountNumber = 0
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("datesaved")) Then
    pdf.DateSaved = Format(ds.Tables(0).Rows(i)("datesaved"), "yyyy-MM-dd")
Else
    pdf.DateSaved = Nothing
End If

If Not IsDBNull(ds.Tables(0).Rows(i)("dateimported")) Then
    pdf.DateImported= Format(ds.Tables(0).Rows(i)("dateimported"), "yyyy-MM-dd")
Else
    pdf.DateImported= Nothing
End If

How can I do this to avoid doing so many If statements below. This way seems inefficient to me, can anyone suggest an better approach to initializing the object in scenario one or two? If the question is unclear, please do let me know, I will try and explain.

Please note this is sample data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

From reading T Field<T>(this DataRow row, string columnName), I believe that there is a check for the DBNull.Value for both reference and value types, returning a default value if DBNull.Value is passed.

So you can use it instead of checking for DBNull.Value each time:

.FileName = ds.Tables(0).Rows(i).Field(Of String)("FileName")

If the value of the specified DataColumn is null and T is a reference type or nullable type, the return type will be null. The Field method will not return Value.

DataRowExtensions.Field

Since you cant use this then @TimSchmelter provided an answer which you could build upon:

.FileId = If(row.IsNull("fileid"), 0, Convert.ToInt32(row("fileid"))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...