Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello sir,
"I am using window form in Visual Studio."

I had given two labels(username and password) with infornt of that ,there will be a textbox and one Login button.I created a username and password fields in MS SQL ,i had given value for username and password in the fields..ok.and when i execute that ,it will ask to enter the username and password,when i entered it will give a error like "An expression of non-boolean type specified in a context where a condition is expected, near 'Name' ".The error in this line "SqlDataReader rdr = cmd.ExecuteReader();".Can any one solve this...

If solution in the code form ,then it will be helpfull.

Thanks & Regards
Pradeep
Posted
Comments
Pete O'Hanlon 8-Mar-12 7:57am    
Come on, throw us a bone. At least let us see what the rest of this bit of code looks like.
dan!sh 8-Mar-12 8:21am    
They must enable voting on comments as well. This one's worth a five.
Pete O'Hanlon 8-Mar-12 8:49am    
Thanks. I quite like it.
ZurdoDev 8-Mar-12 8:29am    
Show the code of your SQL statement. You have an invalid SQL statement.

Try this:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, password FROM myTable WHERE userName=@UN", con))
        {
        con.Parameters.AddWithValue("@UN", userNameTextBox.Text);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            if (reader.Read())
                {
                int id = (int) reader["iD"];
                byte[] pw = (byte[]) reader["password"];
                ...
                }
            }
        }
    }
You can then check your password matches and log him in if it does.
BTW: Don't store your password in text format - it is a real security hole. Instead, use a Hashing function. Password Storage: How to do it.[^]
 
Share this answer
 
Check your SQL statements. There is something wrong with if statement or a where clause in it.
 
Share this answer
 
paste your code here...we will try to solve your problem....
 
Share this answer
 
Comments
Pradeep CBZ 9-Mar-12 1:29am    
hello sir,
This is the code ,but it is giving error in this line: "SqlDataReader rdr = cmd.ExecuteReader();" and the error is: "An expression of non-boolean type specified in a context where a condition is expected, near 'Name'."

My code is :
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=CBP\\SQLEXPRESS;Initial Catalog=ECG;Integrated Security=True;Pooling=False";
//con.ConnectionString = "Data Source=CBP\\SQLEXPRESS;Initial Catalog=hms;Integrated Security=True;Pooling=False";
// SqlConnection con = new SqlConnection(sqlString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Config Login where User Name='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'", con);


SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();

if (rdr.HasRows)
{

Config_admin_screen f = new Config_admin_screen();
f.Show();
}
}

Please do replay me me as early as possible..

Thanks
Pradeep CBZ

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