Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this is the code for login button that should check the database and then show another form.but instead its always giving me the else part here, what am i doing wrong here.
C#
{
            SqlConnection con = new SqlConnection(@"Data Source=SAJJAD-PC;Initial Catalog=hotel;Integrated Security=True;");
            
            SqlDataAdapter sda = new SqlDataAdapter("select count(*) from login where username = '" + usernametxtbox+"'and password = '" +passwordtxtbox+"'",con);
 
            DataTable dt = new DataTable();
            sda.Fill(dt);
 

 

            if (dt.Rows[0][0].ToString() == "1")
            {
 
                con.Open();
 

                this.Hide();
                Form5 form5 = new Form5();
                form5.Show();
 
            }
            else
            {
                MessageBox.Show("Please Check your username and password again !");
            }
Posted
Comments
F-ES Sitecore 3-Aug-15 6:31am    
Use the debugger and see what dt.Rows[0][0] is returning. It's probably because you're not getting the field data correctly, google for examples on how you use datatables.

http://www.dotnetperls.com/datatable
[no name] 3-Aug-15 6:48am    
There is no need to crosspost. You have already posted this in one forum. Pick a forum and delete the duplicate.
Fix your SQL injection vulnerability.
Run your code through the debugger and you would see what is going on.
Sanket Saxena 3-Aug-15 7:03am    
Atleast check your dt.Rows.Count return any rows or not.........100% not.

1 solution

Dark Commet wrote:
what am i doing wrong here.


Would you like a list? :laugh:

Being honest, the answer is "pretty much everything".
You break the two first rules of databases:
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Then, you don't check to see if there are any values in your data:
VB
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")

Then you unnecessarily convert to string, and do string comparisons,you use default names for forms, you don't dispose of objects that hold scarce resources, you don't do any error checking, you...

You get the idea. You have a long way to go.

So follow the link, have a read of the code there, and then change your db to support hashed passwords.
Return the hashed password for the user name, and compare that instead of trying to get the count. And please - for your own sake - use parameterised queries at all times!
 
Share this answer
 
Comments
Dark Commet 3-Aug-15 7:08am    
thanks
its just my 4th day with c sharp and sql server, but i am ready to learn, i tried to learn about the Parameterized queries but could not find any good source, if you have any plz share, 2nd thing that you pointed storing the passwords in simple plain text, can i ignore this until i get some more knowledge about it ?
OriginalGriff 3-Aug-15 7:19am    
I really, really wouldn't!
It's a very dangerous practice, because people tend to try to use the same password for all systems - makes it easier to remember - so if one of them is vulnerable, then there is a good chance that the same data will work for, say...your bank account...:laugh:

It's not difficult to do it properly - the code is all in the link - so even for the moment when you don't actually understand it, it's worth doing. If you get into the habit of doing things the right way, it becomes second nature and you will always do it. The one thing you won't do is "come back to it later" - you'll be busy doing more interesting new projects!

And google is an excellent source of info about parameterised queries:
https://www.google.co.uk/search?q=parameterised+queries+c%23&oq=parameterised+queries+c%23&aqs=chrome..69i57j0l5.8866j0j7&sourceid=chrome&es_sm=93&ie=UTF-8

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