• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15
  • Last Modified:

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
0
Justin Hull
Asked:
Justin Hull
  • 2
  • 2
1 Solution
 
Juan OcasioApplication DeveloperCommented:
How about String.IsNullOrEmpty ?
0
 
Chris StanyonCommented:
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
 
Justin HullAuthor 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 StanyonCommented:
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
 
Justin HullAuthor Commented:
Thank you very much, this helped me quite a bit.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now