hi all,
am doing the roles management in login page using c# windows application.
actually i have done the code but which was working partially in my code,
i have two table the name is,
1.adminlogin
2.userlogin.
my problem is when i have enter username and password whether its from adminlogin or userlogin whatever it is, i need to log in but in my code has doing only one side of table. that is nothing but the else if statement.
so please have a look into it my code below and tell me where i did mistake..
my code is:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class test : Form
{
SqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = null;
DataRowCollection dr = null;
//DataRow dr1=null;
SqlConnection xconn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=sa");
public test()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtLoginName.Text == "" || pbPassword.Text == "")
{
// MessageBox.Show("Login name or password cannot be left blank", "Invalid Login Information", this.Text, MessageBoxButton.OK, MessageBoxImage.Information);
MessageBox.Show("Login name or password cannot be left blank", "Invalid Login Information",MessageBoxButtons.OK);
txtLoginName.Focus();
}
if (txtLoginName.Text == "Administrator")// checking whether the user is an administrator or not
{
string loginname = null;
string password = null;
da = new SqlDataAdapter("select * from adminlogin", xconn);
da.Fill(ds, "adminlogin");
dt = ds.Tables["adminlogin"];
dr = dt.Rows;
//dr1 = dr[0];
loginname = dr[0][0].ToString();
password = dr[0][1].ToString();
if (txtLoginName.Text == loginname && pbPassword.Text == password)
{
Form2 obj = new Form2 ();// showing the Admin form
MessageBox.Show("Welcome Administrator");
this.Hide();
obj.Show();
}
else
{
MessageBox.Show("The username and/or password entered is wrong", "Invalid admin credentials");
pbPassword.Text = "";
pbPassword.Focus();
}
}
else if (txtLoginName.Text != "Administrator")// if the user is not an administrator
{
da = new SqlDataAdapter("select * from userlogin", xconn);
ds.Clear();
da.Fill(ds, "userlogin");
dt = ds.Tables["userlogin"];
dr = dt.Rows;
string userlogin = null;
string userpassword = null;
SqlCommand comm = new SqlCommand("select count(*) from userlogin", xconn);
xconn.Open();
int value = Convert.ToInt32(comm.ExecuteScalar());
xconn.Close();
//MessageBox.Show("number =" + value);
for (int i = 0; i <= value - 1; i++)
{
userlogin = dr[i][0].ToString();
userpassword = dr[i][0].ToString();
if (txtLoginName.Text == userlogin && pbPassword.Text == userpassword)
{
Form1 obj1 = new Form1 ();// Showing the normal user form
MessageBox.Show("Welcome user:" + userlogin);
this.Hide();
obj1.Show();
}
}
}
else
{
MessageBox.Show("The username or password entered is incorrect", "Invalid user credentials");
txtLoginName.Text = "";
pbPassword.Text = "";
txtLoginName.Focus();
}
}
}
}
thanks in advance..