2
Reply

Login problem

Jayita Roy

Jayita Roy

Jun 22 2015 10:49 AM
436
 SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
try
{

con.Open();

SqlCommand cmd = new SqlCommand(@"SELECT Count(*) FROM Logins
WHERE Username=@uname,
Password=@pass and Category=@ctgy", con);
cmd.Parameters.AddWithValue("@uname", textBox_usern.Text);
cmd.Parameters.AddWithValue("@pass", textBox_pwd.Text);

int result = (int)cmd.ExecuteScalar();
con.Close();
con.Open();
SqlCommand sc1 = new SqlCommand(@"Select Category from Logins
Where Username=@usrn", con);
sc1.Parameters.AddWithValue("@usrn", textBox_usern.Text);
SqlDataReader dr = sc1.ExecuteReader();
while (dr.Read())
{
string ctgy = (string)dr["Logins"];
sc1.Parameters.AddWithValue("@ctgy", ctgy);

if (result > 0)
{
if (ctgy == "Admin")
{
//this.Close();
MessageBox.Show("Welcome Admin");
Admin f1 = new Admin();
f1.Show();

}

else
{

MessageBox.Show("Welcome " + textBox_usern.Text);
FormCheck f5 = new FormCheck();
f5.Show();

}

}

else
{
MessageBox.Show("Incorrect login");

}
textBox_usern.Clear();
textBox_pwd.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show("Unexpected error:" + ex.Message);

}
 
/* I have a table "Logins" it has 3 column Username, Password and Category. Category has two types "Admin" and "User". Now I have two textbox where user will give Username and Password. I want username textbox input to check whether the given username is Admin or User and open different forms. Now it's showing an error." Please help. */

Answers (2)