Treat the return of a null datatable line

Asked

Viewed 220 times

1

I’m used to developing in VB.net and am starting a new project in C#. net with VS2013. I realized that in many things there is an expressive difference in syntax and I came across a question. How I handle null values of a Datatable, after a query in the Database?

As I would do in relation to the example below?

If (objclidto.DtAniversario Is Nothing) Then : cmd.Parameters.Add(New SqlParameter("@DataAniversario", SqlDbType.Date)).Value = DBNull.Value
Else : cmd.Parameters.Add(New SqlParameter("@DataAniversario", SqlDbType.VarChar)).Value = objclidto.DtAniversario
End If

1 answer

1

You can use DBNull.Value in C#, just like in VB:

if (objclidto.DtAniversario == null)
{
    cmd.Parameters.Add(
        new SqlParameter("@DataAniversario", SqlDbType.Date)
        ).Value = DBNull.Value;
}
else
{
    cmd.Parameters.Add(
        new SqlParameter("@DataAniversario", SqlDbType.VarChar)
        ).Value = objclidto.DtAniversario;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.