tags:

views:

131

answers:

4

hi, i am trying to retrieve some data from mysql(c#,mono), the problem is that if the returned value is null the program crashes, i know it has to do something with the returned null value, because if i want to retrieve something that is in the database it works,can anyone help me with this?

The code:

MySqlConnection dbcon;
dbcon = new MySqlConnection(conString);
try
{
    dbcon.Open();
}
catch (Exception)
{
    Console.WriteLine("MySQL Database Connection Problem !!!");

}

//reading data from mysql   
MySqlCommand dbcmd = new MySqlCommand(sql, dbcon);

MySqlDataReader reader = dbcmd.ExecuteReader();

while(reader.Read()){

    txtFirstname.Text = reader["first_name"].ToString();
    txtLastname.Text  = reader["last_name"].ToString();
    imgUser.File      = path+reader["photo"].ToString();
    expDate           = reader["expiration_datetime"].ToString();
    usrName           = reader["username"].ToString();

}

dbcon.Close();
+1  A: 

For instance, let's imagine your "first_name" is null at database; reader["first_name"] will return null and it doesn't have a .ToString() method, so your code fails.

Below, I changed way you get that files; I try to cast it to string and, if I got a null value, I use that ?? (null-coalescing operator) to return an empty string.

txtFirstname.Text = reader["first_name"  ] as string ?? "";
txtLastname.Text  = reader["last_name"   ] as string ?? "";
imgUser.File      = path + reader["photo"] as string ?? "";
expDate           = reader["expiration_datetime"] as string ?? "";
usrName           = reader["username"] as string ?? "";

HTH

Rubens Farias
tanx for the answers, but still no difference. I am using this in a "FocusOutEvent" so, when i want to retrieve someones information(i use barcode scanner) that is in the mysql, it returns with information, but when the person is not in the database, it crashes. simply after scanning the barcode i hit tab, thats when it crashes.
deewangan
what line exactly that problem occurs?
Rubens Farias
+2  A: 

You are calling the ToString method on the objects returned by MySQL.

If MySQL returns null, you'll call the ToString method on a null object, giving a NullReferenceException.

Assuming that the SQL is actually returning strings (not numbers or dates), you can simply cast to a string, like this: (string)reader["username"].
If the SQL is returning non-string datatypes, you can call Convert.ToString(reader["username"]).

Both of these will result in null if MySQL returns null.

If you want MySQL nulls to result in something other than null, use the null coalescing operator, like this: Convert.ToString(reader["username"]) ?? "It's Null!".

SLaks
`reader["columnName"]` should never return null, it should return `DBNull.Value`. Perhaps it's a bug in the MySQL provider...
Thomas Levesque
It's not the only adapter that does that unfortunately. Some versions of the Oracle adapter (the one by Oracle, not the in-built Microsoft one) do that as well.
DrJokepu
A: 

Based on the exception in your comment, it looks like you have a completely different problem somewhere else.

I would guess that you're reading a .resources file and generating a filename from the SQL query. If so, nulls might cause you to generate an incorrect filename.

You need to debug your code.

SLaks
A: 
deewangan