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

Request.Querystring NULL Value Help!

Hello Experts,

I have the following Code attached below. When a user tries to navigate to this page and if the value for ghaocp_id is NULL then I get an error message on the line below:

I need a way to handle this issue so if the value for ghaocp_id is NULL then assign the value NULL to ghaocp_id rather than have an error thrown. This value ghaocp_id may not contain always contain a value and if it does not contain a value then assign NULL to it.

LINE OF ERROR MESSAGE:
int ghaocp_id = Convert.ToInt32(Request.QueryString["ghaocp_id"].ToString());

ERROR MESSAGE:
Input string was not in a correct format.

public partial class programinfo_ghap_secure_register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.QueryString["ghaoc_id"] == null))
        {
            Response.Redirect("../one/index.aspx");
        }
        else
        {
            RetrieveUsersFirstLastName();
            RetrieveRegisterCourseInfo();
        }
    }

    protected void RetrieveUsersFirstLastName()
    {       
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            string FirstLastName = Convert.ToString(Session["UserNameSessionID"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveUsersFLName";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@users_username", SqlDbType.VarChar).Value = FirstLastName;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    lblFLName.Text = rdr["users_flname"].ToString();
                    hf_users_id.Value = rdr["users_id"].ToString();
                }
            }

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

    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();
                    lblCourseProgram.Text = "<b>Program:</b> " + rdr["ghaocp_name"].ToString();
                }
            }

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

    protected void btn_RegisterUser_Click(object sender, EventArgs e)
    {
        hf_ghaoc_id.Value = Request.QueryString["ghaoc_id"];
        hf_ghaocp_id.Value = Request.QueryString["ghaocp_id"];

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

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

            try
            {
                conn.Open();

                cmd.ExecuteNonQuery();

                Response.Redirect("register_success.aspx");
            }

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

Open in new window

0
asp_net2
Asked:
asp_net2
1 Solution
 
käµfm³d 👽Commented:
Try this:


protected void RetrieveRegisterCourseInfo()
{
	int ghaoc_id;
	int ghaocp_id;

	if (int.TryParse(Request.QueryString["ghaoc_id"], out ghaoc_id) && int.TryParse(Request.QueryString["ghaocp_id"], out ghaocp_id))
	{
		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();
					lblCourseProgram.Text = "<b>Program:</b> " + rdr["ghaocp_name"].ToString();
				}
			}

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

Open in new window

0
 
asp_net2Author Commented:
Hi kaufmed,

Yes, that worked. Is there a way to make the value ghaocp_id = NULL if ghaocp_id has NO value instead of 0?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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