Search code examples
c#asp.netnullrequest.querystring

c# - asp.net // why does this value get NULL?


, i have this code where when i click button 4, i want to redirect to a link using a variable as a querystring! I use the variable in all of the code but suddenly when it gets to the button class it gets null!?!? so the next page results to /..blabla?Email=0. The variable is "PIDPROF"!

Help, im stuck! thanks in advance! :)

namespace DisplayingImages
 {
    public partial class PageView : System.Web.UI.Page
    {

    public string query, constr, query1, query2, query3, PIDVIEW;
    public int PIDPROF;
    public SqlConnection con;
    public void connection()
    {
        constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
        con = new SqlConnection(constr);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            PIDVIEW = Request.QueryString["Email"];
            PIDPROF = Convert.ToInt32(PIDVIEW);

            HttpContext context = HttpContext.Current;
            SearchedUser();
            imagebindGrid();
            PostSelection();
    }
}


    public void SearchedUser()
    {
        connection();
        String str = "select First_Name,Email_Account,Surname,id from ID where ( id = @search )";
        SqlCommand Srch = new SqlCommand(str, con);
        Srch.Parameters.Add("@search", SqlDbType.Int).Value = PIDPROF;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = Srch;
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            lblemail.Text = dt.Rows[0]["First_Name"].ToString();
            lblname.Text = dt.Rows[0]["Email_Account"].ToString();
        }

    }


    /* Gridview για εικονες */
    public void imagebindGrid()
    {
        connection();
        query = "Select Image from ImageToDB where user_id= " + PIDPROF;
        SqlCommand com = new SqlCommand(query, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        Image1.ImageUrl = "Handler1.ashx?id_Image=" + PIDPROF;

   }


    /* Κλασση για το Post */
    private void Txt()
    {
        try
        {

            if (PIDPROF != null)
            {
                connection();
                query1 = "Insert into  Posttext (user_id,Posttext) values (@user_id,@Your_Post)";
                SqlCommand com2 = new SqlCommand(query1, con);

                com2.Parameters.AddWithValue("@user_id", PIDPROF);
                com2.ExecuteNonQuery();

                PostSelection();
            }

        }

        catch (Exception ex)
        {

        }

    }
    /* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */
    public void PostSelection()
    {
        connection();

        query2 = "Select Posttext from Posttext where user_id= " + PIDPROF;
        SqlCommand com1 = new SqlCommand(query2, con);
        SqlDataReader Read = com1.ExecuteReader();
        grdemployee7.DataSource = Read;
        grdemployee7.DataBind();
        Read.Close();

    }


    /* --------------------Κουμπι για search PROFILE -----------------------------------*/
    public void Button3_Click1(object sender, EventArgs e)
    {
                Response.Redirect("~/WebForm7.aspx");
    }


    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PhotoView.aspx?Email=" + PIDPROF);
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx?Email=" + PID);
    }

    /*Logout Button */
    protected void Button1_Click(object sender, EventArgs e)
    {

        System.Web.Security.FormsAuthentication.SignOut();
        Session.Clear();
        Session.RemoveAll();
        Session.Abandon();
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("Expires", "0");
        FormsAuthentication.SignOut();
        HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
        Response.Redirect("~/Logout.aspx");
    }
    public string USER_PIDPROF { get; set; }
    public DateTime _myid { get; set; }
    public string SN { get; set; }
    public string PS { get; set; }
    public string EM { get; set; }
    public int PID { get; set; }
}
  }

Solution

  • When the user clicks the button, there is another request for the page, so the class is re-instantiated. In that second request, Page.IsPostBack is true so the code that populates PIDPROF is never called. If you move it outside the if block it should work for you.

    protected void Page_Load(object sender, EventArgs e)
    {
        PIDVIEW = Request.QueryString["Email"];
        PIDPROF = Convert.ToInt32(PIDVIEW);
    
        if (!IsPostBack)
        {
            HttpContext context = HttpContext.Current;
            SearchedUser();
            imagebindGrid();
            PostSelection();
    }