Clicky

I am new to C# and I have developed a windows form that can input information to sql  from the texboxes that user fills out and then it can be viewed in  data grid view.
Now I am trying to have a Login Windows for the users to put their login information like username and password in. And the code looks at a table in sql call IDs and if the appropriate username and pass matches the data in sql then it lets the user to login. Please let me know if you have a sample code or directions on how to do this.

Thank you all,
Paul

asked 12/13/2011 01:08

pauledwardian's gravatar image

pauledwardian ♦♦


4 Answers:
Are you expecting something like this:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
private Boolean isValidUser(string username, string pwd)
{
        SqlConnection con = new SqlConnection("DataBase=Northwind;Server=(local);User ID=sa;Password=test");
        SqlCommand command = new SqlCommand("select ID from users where userid='" + username + "' and pwd='" + pwd + "'");
        command.Connection = con;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(command);
        con.Open();
        da.Fill(dt);
        con.Close();
        if (dt.Rows.Count == 0)
        {
            //strLoginErrorMsg = "Invalid UserName";
            dt.Dispose();
            return false;
        }
        else
        {
	   dt.Dispose();
	   //strId = dt.Rows[0]["Id"].toString();
	   return true;
        }
}


-Rahul
link

answered

Rahul_Gade's gravatar image

Rahul_Gade

Thanks Rahul. If the password is wrong then it determines it here--> if (dt.Rows.Count == 0) and if the password is correct it is in the else part?
Do you know how can I have this Username and Password form in the beginning of the application when the users click on the application icon, this would be the first window that pups up and hows their username and password?
Also, do you know if it is possible for the code to have come conditions such as if the username is equal to user then button1.enable = false and if username is equal to Admin then the button1.enable=True.
Where can you put these conditions in the code you provided above?

Paul
link

answered 2011-12-13 at 21:28:09

pauledwardian's gravatar image

pauledwardian


In Program.cs of your .Net project, you will find main from where you instantiate your first form, so in below code, LoginForm is the place where you should instantiate your login form.

1:
2:
3:
4:
5:
6:
7:
[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoginForm());
        }


if you mean, these "Admin" and "user" are fixed user name you can just hard code them in the condition like below in textbox's TextChanged event :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.ToLower() == "admin")
            {
                button1.Enabled = true;
            }
            else if(textBox1.Text.ToLower() == "user")
            {
                button1.Enabled = false;
            }
        }


-Rahul Gade
link

answered 2011-12-13 at 21:54:34

Rahul_Gade's gravatar image

Rahul_Gade

Thanks
link

answered 2011-12-15 at 21:09:11

pauledwardian's gravatar image

pauledwardian

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×156

Asked: 12/13/2011 01:08

Seen: 270 times

Last updated: 12/16/2011 12:54

Related questions