Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Handle NULL Values

Posted on 2011-10-21
14
Medium Priority
?
264 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:asp_net2
  • 6
  • 6
  • 2
14 Comments
 
LVL 28

Expert Comment

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

Expert Comment

by:Jesus Rodriguez
ID: 37007004
What's wrong with the code??
0
 
LVL 4

Author Comment

by:asp_net2
ID: 37007057
@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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:asp_net2
ID: 37007103
@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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 37007176
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
 
LVL 4

Author Comment

by:asp_net2
ID: 37007203
@k-designers,

Can you show me what I need to have with the code I supplied?
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 37007246
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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 37007272
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
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 37007281
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
 
LVL 28

Expert Comment

by:strickdd
ID: 37007417
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
 
LVL 4

Author Comment

by:asp_net2
ID: 37007613
@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
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 1000 total points
ID: 37007807
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
 
LVL 4

Author Comment

by:asp_net2
ID: 37008000
@k-designers,

No luck, if I have a value for ghaocp_name and or ghaocp_id nothing is displayed.
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 37057033
Low grade because you never came back to help. All your code DID NOT WORK!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question