Handle the return of a line from a null Datatable

1

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

How would I do about 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
    
asked by anonymous 31.12.2014 / 03:39

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;
}
    
31.12.2014 / 05:24