Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is my code for login button in c# windows forms application


private void button1_Click_1(object sender, EventArgs e)
      {
          string cs = @"Data Source=.;Database=master;Integrated Security=True;";


          if (textBox1.Text == "" || textBox2.Text == "")
          {
              MessageBox.Show("Please provide UserName and Password");
              return;
          }
          try
          {
              //Create SqlConnection
              SqlConnection con = new SqlConnection(cs);
              SqlCommand cmd = new SqlCommand("Select * from Register where UserNmae=@username and Password=@password", con);
              cmd.Parameters.AddWithValue("@username", textBox1.Text);
              cmd.Parameters.AddWithValue("@password", textBox2.Text);
              con.Open();
              SqlDataAdapter adapt = new SqlDataAdapter(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!");
                  this.Hide();
                  Dashboard dsb = new Dashboard();
              }
              else
              {
                  MessageBox.Show("Login Failed!");
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }


What I have tried:

I have tried to connect it with sql server to connect with the table have the fields.
its showing error after entering the correct username and password.
Posted
Updated 17-Sep-16 10:35am
v2
Comments
OriginalGriff 17-Sep-16 3:31am    
What error? Is there a message?
Patrice T 17-Sep-16 8:57am    
Which error message and where ?
[no name] 17-Sep-16 12:28pm    
Learn how to ask questions without posting the same thing over and over.

check this spell UserNmae
 
Share this answer
 
As already pointed out, you probably have a misspelling in the column name, but...

Much more important is that you seem to handle passwords in plain text so anyone who has access to the database can see users passwords. This should never be allowed. No-one, not even the database administrator should be able to see what is the password for an individual.

If you think about it, you only need to check if the password matches to the one the user has provided when registered or starting to use the system. This means that you don't need to know what the password is, only if it's the exact same. The way to do this is to use one-way hashing. In the beginning, store the hash for the password and later check if the hash matches when trying to log in.

For more information, go through Password Storage: How to do it.[^]

Another important thing is that you use master database for your application's tables. You should never put any of your tables in master or any other system databases. As the name says, they should be reserved for system use only. Common practice is to create a separate database for your own system.
 
Share this answer
 
v2
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

If Karthik Bangalore correctly spotted the problem, you have an error message on SQL server telling you that the query uses an unknown column.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900