Question Session Variable

Knutsford

New member
Joined
Jan 17, 2017
Messages
4
Programming Experience
10+
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Web;

namespace test_1
{
    public partial class frmLogin : Form
    {        

        public frmLogin()
        {
            InitializeComponent();
        }
        string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\test.accdb";
        private void button1_Click(object sender, EventArgs e)
        {
            if (Username.Text == "" ||Password.Text == "")
            {
                MessageBox.Show("Please provide UserName and Password");
                return;
            }
            try
            {
                OleDbConnection con = new OleDbConnection(cs);
                
                OleDbCommand cmd = new OleDbCommand("Select * from Admin where UserName=@username and Password=@password", con);

                cmd.Parameters.AddWithValue("@username", Username.Text);
                cmd.Parameters.AddWithValue("@password", Password.Text);
                con.Open();
                OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);
                
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;
                //If count is equal to 1, than show frmMain form
                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");
                   
                    Session["LoggedIn"] = "Yes";
                   
                    this.Hide();
                    frmMain fm = new frmMain();
                    fm.Show();
                }
                else
                {
                    MessageBox.Show("Login Failed!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}




I am new to C# and have created a login form but it doesn't like Session["LoggedIn"] = "Yes"; It doesn't recognise Session or HttpContext.Current.Session. How do I store a value in a session variable please?
Thanks
 
Session is used is ASP.Net that is a stateless environment, meaning each page shown is basically a fresh start, and most user interaction by default starts a new server request. This means ASP.Net has special needs to maintain state, and Session is one of these and is used to store values across the application but separate from different users. In Windows Forms there is for the most part no need for such special arrangements and you will find none of the ASP.Net state managements here, the most similar functionality for Session is properties in a static class (for easy access across application) or application user settings (for easy access across application and/or storage between application runs).
Static Classes and Static Class Members (C# Programming Guide)
Using Settings in C#

Often you can and should do without such storage and instead pass values between the different objects used, for example here you could pass the value "yes" to a property of the frmMain class instance.
 
It should have been a web application not a form one - oops but I am still stuck. Can someone please point me in the direction of an example login page
 

Latest posts

Back
Top Bottom