1

After checking the users I received from firebase, if there is a registered user, I am trying to send the mail of this user to the other page with session. But session is always null on other page.

My login page

protected async void student_giris_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                String ogrenci_email, ogrenci_sifre;
                Object OBJogrenci_no;
                ogrenci_email = studentMail.Text;
                ogrenci_sifre = studentPassword.Text;
                Boolean kayit_varmi = false;

                FirestoreDb firestoreDb = FirestoreDb.Create("");
                Query ogrencileriCek = firestoreDb.Collection("Students");
                QuerySnapshot ogrencilerSnapshots = await ogrencileriCek.GetSnapshotAsync();


                foreach (DocumentSnapshot documentSnapshot in ogrencilerSnapshots.Documents)
                {
                    //System.Diagnostics.Debug.WriteLine("Document data for {0} document:", documentSnapshot.Id);
                    Dictionary<string, object> ogrenci = documentSnapshot.ToDictionary();
                    foreach (KeyValuePair<string, object> pair in ogrenci)
                    {

                        //System.Diagnostics.Debug.WriteLine("{0}: {1}", pair.Key, pair.Value);
                        if (ogrenci.ContainsValue(ogrenci_email) && ogrenci.ContainsValue(ogrenci_sifre))
                        {
                            OBJogrenci_no = ogrenci["number"];
                            ogrenci_no = (string)OBJogrenci_no;
                            kayit_varmi = true;
                        }
                    }
                }
                if (kayit_varmi == true)
                {
                    Session["cUser"] = studentMail.Text;
                    HttpContext.Current.Session.Timeout = 28800;
                    Response.Redirect("Student_Lessons.aspx");
                }
                else
                {
                    yanlis_girisLabel.Text = "Email ya da şifrenizi kontrol ediniz...";
                    yanlis_girisLabel.ForeColor = System.Drawing.Color.Red;
                }
            }
        }

My other page

protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["cUser"] != null)
            {
                Response.Redirect("StudentsLogin.aspx");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(Session["cUser"]);
                Label1.Text = Session["cUser"].ToString();
            }
        }

I am trying this in web form app. How can i solve this problem?

Talha Kara
  • 11
  • 3
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jazb Mar 30 '22 at 00:22
  • I took a look but unfortunately I couldn't find a solution. Actually my session is not null. I can see the session's value in the same page, but when redirect the other page, it becomes null. – Talha Kara Mar 31 '22 at 14:58

1 Answers1

1

Its because the value of kayit_varmi is always false.

remove the brace which is just about if (kayit_varmi == true) line

Try this code

protected async void student_giris_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                String ogrenci_email, ogrenci_sifre;
                Object OBJogrenci_no;
                ogrenci_email = studentMail.Text;
                ogrenci_sifre = studentPassword.Text;
                Boolean kayit_varmi = false;

                FirestoreDb firestoreDb = FirestoreDb.Create("");
                Query ogrencileriCek = firestoreDb.Collection("Students");
                QuerySnapshot ogrencilerSnapshots = await ogrencileriCek.GetSnapshotAsync();


                foreach (DocumentSnapshot documentSnapshot in ogrencilerSnapshots.Documents)
                {
                    //System.Diagnostics.Debug.WriteLine("Document data for {0} document:", documentSnapshot.Id);
                    Dictionary<string, object> ogrenci = documentSnapshot.ToDictionary();
                    foreach (KeyValuePair<string, object> pair in ogrenci)
                    {

                        //System.Diagnostics.Debug.WriteLine("{0}: {1}", pair.Key, pair.Value);
                        if (ogrenci.ContainsValue(ogrenci_email) && ogrenci.ContainsValue(ogrenci_sifre))
                        {
                            OBJogrenci_no = ogrenci["number"];
                            ogrenci_no = (string)OBJogrenci_no;
                            kayit_varmi = true;
                        }
                    }
                 
                if (kayit_varmi == true)
                {
                    Session["cUser"] = studentMail.Text;
                    HttpContext.Current.Session.Timeout = 28800;
                    Response.Redirect("Student_Lessons.aspx");
                }
                else
                {
                    yanlis_girisLabel.Text = "Email ya da şifrenizi kontrol ediniz...";
                    yanlis_girisLabel.ForeColor = System.Drawing.Color.Red;
                }
            }
        }
    }
Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30