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