Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i just i take 2 lables and 2 textboxes and 1 button
i just write the code in button

C#
if (textBox1.Text != "" & textBox2.Text != "")
           {
               string queryText = "SELECT Count(*) FROM Login " +
                                  "WHERE username = @User name AND password = @Password";
               using (SqlConnection cn = new SqlConnection("Data Source=MIS-PC;Initial Catalog=project;Integrated Security=True"))
               using (SqlCommand cmd = new SqlCommand(queryText, cn))
               {
                   cn.Open();
                   cmd.Parameters.AddWithValue("@User name", textBox1.Text);  // cmd is SqlCommand
                   cmd.Parameters.AddWithValue("@Password", textBox2.Text);
                   int result = (int)cmd.ExecuteScalar();
                   if (result > 0)
                       MessageBox.Show("Loggen In!");
                   else
                       MessageBox.Show("User Not Found!");
               }


but its not working

Error in this line
int result = (int)cmd.ExecuteScalar();


how to do
Posted
Updated 14-Apr-14 22:24pm
v3
Comments
Dinesh.V.Kumar 15-Apr-14 2:49am    
What is the error you are getting?
hilbert hussen 15-Apr-14 4:47am    
{"Incorrect syntax near 'nvarchar'.\r\nAn expression of non-boolean type specified in a context where a condition is expected, near 'name'."}

int result = (int)cmd.ExecuteScalar();
Dinesh.V.Kumar 15-Apr-14 4:52am    
Instead of @User name change it to @Username and check...
hilbert hussen 15-Apr-14 4:57am    
Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "@Username".

again came error
Dinesh.V.Kumar 15-Apr-14 6:17am    
did you replace @User name in both the places in your code?

you have not provided sql connection in to
sqlconnection con=new sqlconnection();

please provide connection string on that so that connect with database, if any other issue let me know about that, also please clear your question.
 
Share this answer
 
Comments
OriginalGriff 15-Apr-14 3:15am    
My down vote countered - I need more coffee...
abhishek_singh is right - you need a connection string to connect to your db, but in addition...
Please, please, don't do things like that!
There are two major mistakes you are making here:
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. As your code stands, I do not need a password to log in as anybody!
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.[^]
 
Share this answer
 
Comments
hilbert hussen 15-Apr-14 6:03am    
Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "@Username".

again came error
OriginalGriff 15-Apr-14 6:34am    
Yes - because your new code which uses parameters doesn't pass the actual parameter data.
Take the space out of the "@User Name" - it's a variable, so variable rules apply - call it "@UserName" instead in both places.
hilbert hussen 15-Apr-14 7:06am    
YES ,BUT I DID THAT ALSO..NOT COMMING
OriginalGriff 15-Apr-14 7:22am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
hilbert hussen 16-Apr-14 3:16am    
not comming
replace @User name with @Username on both place, then again run that code.
 
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