Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
abc.Open();
            com.CommandText = "select [id],[name] from testtable where user =='"textbox1.Text"' && password=='"textbox2.Text"'";
            bca = com.ExecuteReader();
            if (bca.HasRows)
            {
                while (bca.Read())
                {

                    listBox1.Items.Add(bca[0].ToString());
                    listBox2.Items.Add(bca[1].ToString());
                }
            }
            abc.Close();
        }

there is i want to select in where clause with text box1 and 2
but sql quuery mistake....
Posted

1) SQL does not accept the == operator, only the = operator.
2) Never, ever, ever use inline concatenation of parameters in sql strings. This just invites sql injection and should be avoided from the outset when learning.
3) Never, ever, ever use a plain text password
4) If you are going to do this, do it as described in this article: Password Storage: How to do it.[^]
This is how it should look...

C#
com.CommandText = "Select [id],[name] FROM testtable WHERE user=@userName AND password=@password";
com.Parameters.Add(new SqlParameter("userName", textbox1.Text));
com.Parameters.Add(new SqlParameter("password", textbox2.Text)); // Horrible idea, but just here to show you how to use a parameter.
bca = com.ExecuteReader();
 
Share this answer
 
Comments
Richard C Bishop 13-Feb-13 17:25pm    
Good call, I was not even thinking about paramterized queries when I was adding my solution. I just saw the errors with the syntax. Your solution is a much better alternative.
fjdiewornncalwe 14-Feb-13 8:59am    
Thanks, richcb
Jibesh 13-Feb-13 17:28pm    
using parameter is the right choice which eliminates the SQL Injection problem. my +5
fjdiewornncalwe 14-Feb-13 8:59am    
Thanks, jibesh
Espen Harlinn 14-Feb-13 13:20pm    
Exactly what I was thinking about :laugh:
BTW: My congratulations, I noticed that you've passed the 100K barrier, well done :-D
Change it from this:

com.CommandText = "select [id],[name] from testtable where user =='"textbox1.Text"' && password=='"textbox2.Text"'";



to this:

com.CommandText = "select [id], [name] from testtable where user ='" + textbox1.Text +"' and  password='" + textbox2.Text + "'";
 
Share this answer
 
replace your Select query with this
"select [id],[name] from testtable where user ='" + textbox1.Text + "' and password='" +textbox2.Text + "'";
 
Share this answer
 
Comments
saimm 14-Feb-13 15:05pm    
thnx muneeb i try with these query...
Sergey Alexandrovich Kryukov 12-Mar-13 1:25am    
Very, very bad answer. This is the invitation of SQL injection, should never be used.
Correct solutions use parametrized statements.
—SA

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