locked
How do I return Null value in sql query RRS feed

  • Question

  • User-218090889 posted

    I have an issue here, were I want to register user with referrer link, first it will check if the referrer link supplied by user is valid, if it is valid it will register user, if it is invalid it will

    alert user of invalid referrer link, but if the referrer link is not supplied it will tell user to use valid referrer link.

    below is my code

    A) The Default page code behind

    protected void Page_Load(object sender, EventArgs e)
            {
                var trackerRef = Request.QueryString["ref"]; // Catching the referre Link
                if (!string.IsNullOrEmpty(trackerRef))
                {
                    Session["RefID"] = trackerRef;
                    Response.Redirect("~/Account/Register.aspx?");
                    
                }
            }
    
    
    

    The Register page code behind

    protected void CreateUser_Click(object sender, EventArgs e)
            {
                
    
                if (!object.Equals(Session["RefID"], null))  // Catching the referrer Link
                {
                    string refId = Session["RefID"].ToString();
                    
    
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
                    SqlCommand cmdd = new SqlCommand("CheckRefId", con);
                    SqlDataAdapter adp = new SqlDataAdapter();
                    cmdd.CommandType = CommandType.StoredProcedure;
                    cmdd.Parameters.Add("@RefCheck", SqlDbType.NVarChar).Value = refId ;
    
    
                    try
                    {
                        cmdd.Connection = con;
                        con.Open();
    
                        adp.SelectCommand = cmdd;
                        adp.Fill(dt);
    
                    }
    
                    finally
                    {
    
                        con.Close();
    
                        adp.Dispose();
    
                        con.Dispose();
    
                    }
    
    
                    if (object.Equals(Session["RefID"], null))
                    {
                        lbl1.Text = "You need a valid Referrer Link!";
    
                    }
                    else if(dt.Rows.Count < 1)
                    {
                        lbl1.Text = "Referrer Link is not a valid Link!";
    
                    }
    
                    else if (dt.Rows.Count > 0)
                    {
                       // Register User
                    }

    with the above code when referrer link is supplied, it determines if referrer link is valid, and it works well, but when referrer link is not supplied message in lbl1 in above highlighted code does not respond.

    please how do I restructure my code so that if referrer link is not supplied the lbl1 message in highlighted part of the code will respond.

    Monday, March 4, 2019 1:08 PM

Answers

  • User475983607 posted

    The syntax is pretty simple.

    if(Session["RefID"] == null)
    {
    
    }

    If this does work then you have bugs to find.  Please use the Visual Studio debugger to check your code.

    Keep in mind that the Page_Load event always fires before the click handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 4, 2019 1:18 PM