G
Guest
I am a newbie to .Net. I run into problem when I try to assign null value
from the database to, say, a textbox.
Say if I had a simple table called "Contact" in the database and it has
three fields: ContactID, Name, Email. If I want to display the data. I would
do something like the following:
txtName = dsContact.Contact.Name;
txtEmail = dsContact.Contact.Email;
But if the Email field is null in database. I would get an error message
saying that I cannot convert "DBNull" to "string". After searching the
Internet for a little while, I found a way to get around this problem.
txtName = dsContact.Contact.IsNameNull() ? " " : dsContact.Contact.Name;
txtEmail = dsContact.Contact.IsEmailNull() ? " " : dsContact.Contact.Email;
Although this can solve the problem, it is pretty ugly and it wouldn't work
if I want to write a helper function that dynamically reads data from a
non-typed dataset and assigns the data to, say, textboxes.
Does anyone know a more graceful way to get around this problem? Thanks!
from the database to, say, a textbox.
Say if I had a simple table called "Contact" in the database and it has
three fields: ContactID, Name, Email. If I want to display the data. I would
do something like the following:
txtName = dsContact.Contact.Name;
txtEmail = dsContact.Contact.Email;
But if the Email field is null in database. I would get an error message
saying that I cannot convert "DBNull" to "string". After searching the
Internet for a little while, I found a way to get around this problem.
txtName = dsContact.Contact.IsNameNull() ? " " : dsContact.Contact.Name;
txtEmail = dsContact.Contact.IsEmailNull() ? " " : dsContact.Contact.Email;
Although this can solve the problem, it is pretty ugly and it wouldn't work
if I want to write a helper function that dynamically reads data from a
non-typed dataset and assigns the data to, say, textboxes.
Does anyone know a more graceful way to get around this problem? Thanks!