How to Handle NULL Values

Hello Experts,

Please see my attached code below. I'm trying to retrieve data based on the following two paramerters getting passed in and it is possible that the value for ghaocp_id my be NULL. If so, I need to display "N/A" to the following label control lblCourseProgram.Text but if the value for ghaocp_id is NOT NULL then display the value.

protected void RetrieveRegisterCourseInfo()
    {
        int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"].ToString());
        int ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"].ToString());

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveRegisterCourseInfo";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ghaoc_id;
            cmd.Parameters.AddWithValue("@ghaocp_id", SqlDbType.Int).Value = ghaocp_id;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    lblCourseName.Text = "<b>Course Name:</b> " + rdr["ghaco_name"].ToString();

                    if (rdr["ghaocp_name"] == DBNull.Value)
                    {
                        lblCourseProgram.Text = "<b>Course Program if applicable:</b> " + "N/A";
                    }
                    else
                    {
                        lblCourseProgram.Text = "<b>Course Program if applicable:</b> " + rdr["ghaocp_name"].ToString();
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }

Open in new window

LVL 4
asp_net2Asked:
Who is Participating?
 
Jesus RodriguezIT ManagerCommented:
ghaocp_id = null;  <---  Cannot convert 'null' to int because it is a non-nullable data type
Do Not Assign Null, assing 0 to this value
 and on this line    if (rdr["ghaocp_name"] == DBNull.Value || string.IsNullOrEmpty(ghaocp_id))
 try this
if (rdr["ghaocp_name"] == 0)
0
 
strickddCommented:
if (rdr["ghaocp_name"] == DBNull.Value || String.IsNullOrEmpty(ghaocp_id))
                    {
                        lblCourseProgram.Text = "<b>Course Program if applicable:</b> " + "N/A";
                    }
0
 
Jesus RodriguezIT ManagerCommented:
What's wrong with the code??
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
asp_net2Author Commented:
@k-designers,

Sorry, forgot to post the error message that I receive when I run my original code.

Error Message:
Input string was not in a correct format.

int ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"].ToString());
0
 
asp_net2Author Commented:
@strickdd,

If I use your code I get the following redline message below.

bool string.IsNullOrEmpty(string value)
Indicates whether the specified string is null or an system.string.empty string.

Error:
the best overloaded method match for 'string.isnullorempty(string)' has some invalid arguments.

0
 
Jesus RodriguezIT ManagerCommented:
Before do the conversion chechl if the value is null
IF is null assign a 0 value if not assign the value that get form the query string and later convert this value
0
 
asp_net2Author Commented:
@k-designers,

Can you show me what I need to have with the code I supplied?
0
 
Jesus RodriguezIT ManagerCommented:
On VB will be like this, let me try to translate to C# later

DIm Var1 as String=""
DIm Var2 as string=""

IF Request.QueryString["ghaoc_id"] is dbnull.value then
 var1=0
else
 var1=Request.QueryString["ghaoc_id"]
end if

IF Request.QueryString["ghaocp_id"] is dbnull.value then
 var1=0
else
 var1=Request.QueryString["ghaocp_id"]
end if

DIm ghaoc_id  as int32= Convert.ToInt32(var1)
Dim ghaocp_id as int32= Convert.ToInt32(Var2)
0
 
Jesus RodriguezIT ManagerCommented:
Must be something like this (I don't know too much C#, sorry about that)

String Var1="";
String Var2="";
if !(Request.QueryString["ghaoc_id"]); //This line will be checking if is nyull or not this query string value
 {
   Var1=0;
 }
else
 {
   Var1=Request.QueryString["ghaocp_id"];
 }
}

if !(Request.QueryString["ghaocp_id"]);  //This line will be checking if is nyull or not this query string value
 {
   Var2=0;
 }
else
 {
   Var2=Request.QueryString["ghaocp_id"];
  }
}
       int ghaoc_id = Convert.ToInt32(Var1);
       int ghaocp_id = Convert.ToInt32(Var2);
0
 
Jesus RodriguezIT ManagerCommented:
The error that you get is because you try to convert a value that can be null from the query string to Int32 directly. Before convert you must check if the value will be null
0
 
strickddCommented:
The problem is here:
int ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"].ToString());

If you do a Convert.ToInt32 on a null or empty string it breaks. You can also use Int.TryParse() if you want, but the code below will correct for the issue.

int? ghaocp_id;

if(String.IsNullOrEmpty(Request.QueryString["ghaocp_id"]))
{
    ghaocp_id = null;
}
else
{
    ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"]);
}

if (rdr["ghaocp_name"] == DBNull.Value || String.IsNullOrEmpty(ghaocp_id))
                    {
                        lblCourseProgram.Text = "<b>Course Program if applicable:</b> " + "N/A";
                    }

Open in new window

0
 
asp_net2Author Commented:
@strickdd,

Still problems with your code. Please see below:

int ghaoc_id = Convert.ToInt32(Request.QueryString["ghaoc_id"].ToString());

        int ghaocp_id;
        if (String.IsNullOrEmpty(Request.QueryString["ghaocp_id"]))
        {
            ghaocp_id = null;  <---  Cannot convert 'null' to int because it is a non-nullable data type.
        }
        else
        {
            ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"]);
        }



if (rdr["ghaocp_name"] == DBNull.Value || string.IsNullOrEmpty(ghaocp_id))
                    {
                        lblCourseProgram.Text = "<b>Course Program if applicable:</b> " + "N/A";
                    }

I get a red line message on line of code below with the same error message as in reference to a comment in made in comment ID: 37007103
 string.IsNullOrEmpty(ghaocp_id)) syaing
0
 
asp_net2Author Commented:
@k-designers,

No luck, if I have a value for ghaocp_name and or ghaocp_id nothing is displayed.
0
 
asp_net2Author Commented:
Low grade because you never came back to help. All your code DID NOT WORK!
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.

All Courses

From novice to tech pro — start learning today.