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
JustinAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan OcasioApplication DeveloperCommented:
How about String.IsNullOrEmpty ?
0
Chris StanyonWebDevCommented:
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

1
JustinAuthor Commented:
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"])
                            });
0
Chris StanyonWebDevCommented:
OK. That's indicating that sdr["DescA"] is an object. You will want to call ToString() on it:

Desc_a = String.IsNullOrEmpty( sdr["DescA"]?.ToString() ) ? "no value" : sdr["DescA"].ToString()

Notice we're using the Null-Conditional checker (?.) before calling ToString()

If you only want to check for NULL and aren't bothered about an empty string (""), then you could use:

Desc_a = sdr["DescA"]?.ToString() ?? "no value";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JustinAuthor Commented:
Thank you very much, this helped me quite a bit.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.