Link to home
Start Free TrialLog in
Avatar of Justin
JustinFlag for United States of America

asked on

Better way to check for null and dbnull values?

Im new to C# and think this is a pretty basic question but looking for a better way. Is there no better way to check for null values from a datareader then write a if..then statement for each input field?

so building on a previous example:

   using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = sqlCon;
                    sqlCon.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            someList.Add(new someModel
                            {
                                Desc_a = sdr["DescA"].ToString(),
                                Desc_b = sdr["Descb"].ToString(),
                                id_a = Convert.ToInt32(sdr["Id"]),
                                State = sdr["state"].ToString()
                            });
                        }
                        sqlCon.Close();

Do I need to write something like
if (sdr.getString(0) != DBNull || sdr.getString(0) != NULL){
Desc_a = sdr["DescA"].ToString(),
} else {
Desc_a = " no value"
}

for each input such as
                            someList.Add(new someModel
                            {
                                if (sdr.getString(0) != DBNull || sdr.getString(0) != NULL){
                                     Desc_a = sdr["DescA"].ToString(),
                                } else {
                                       Desc_a = " no value"
                                 }
                                if (sdr.getString(1) != DBNull || sdr.getString(1) != NULL){
                                     Desc_b = sdr["DescB"].ToString(),
                                } else {
                                       Desc_b = " no value"
                                 }
                                if (sdr.getString(0) != DBNull || sdr.getString(2) != NULL){
                                      id =  Convert.ToInt32(sdr["Id"]),
                                } else {
                                       id= "0"
                                 }
                                if (sdr.getString(0) != DBNull || sdr.getString(3) != NULL){
                                     State= sdr["state"].ToString(),
                                } else {
                                       State= " no value"
                                 }

                            });

Seems like this is common work so i was thinking there should be a better way to check for a null then if.. else for each input
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

How about String.IsNullOrEmpty ?
Should be able to run a ternary operator with the IsNullOrEmpty:

Desc_a = String.IsNullOrEmpty(sdr["DescA"]) ? "no value" : sdr["DescA"].ToString();
id = String.IsNullOrEmpty(sdr["Id"]) ? 0 : Convert.ToInt32(sdr["Id"]);

Open in new window

Avatar of Justin

ASKER

Is my syntax correct here? I get an error can not convert object to string, on, "string.IsNullOrEmpty(sdr["DescA"]) "

someList.Add(new someModel
                            {
                                Desc_a = String.IsNullOrEmpty(sdr["DescA"]) ? "no value" : sdr["DescA"].ToString(),
                                id= String.IsNullOrEmpty(sdr["Id"]) ? 0 : Convert.ToInt32(sdr["Id"])
                            });
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Justin

ASKER

Thank you very much, this helped me quite a bit.