Error when null value in database

Mad_Cat

New member
Joined
Mar 15, 2006
Messages
2
Hi, I just started to use C# and I have a problem.

Code:
C#:
myConnection.Open();

myDataReader = myCommand.ExecuteReader();

while (myDataReader.Read())
{
Label1.Text = (string)Clientes.GetValue(0);
// Error below when there is a null value in the database
Label2.Text = (string)Clientes.GetValue(1);
}

I tried a if else to teste the null, diferent casts but cant seem to find a workaround, can anyone help?

Thanks in advance,
Mad Cat
 
Last edited by a moderator:
Try something like the following
C#:
myConnection.Open();
 
myDataReader = myCommand.ExecuteReader();
 
while (myDataReader.Read())
{
Label1.Text = (string)Clientes.GetValue(0);

if (!Convert.IsDBNull(Clientes.GetValue(1)))
     Label2.Text = (string)Clientes.GetValue(1);
}
hopefully that should do the trick.
 
Back
Top