Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'condition is expected' in query

Tags:

c#

asp.net

SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Projects\\csharp\\Login1\\App_Data\\Login.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd;
SqlDataReader dr;   

protected void  LinkButton1_Click(object sender, EventArgs e)
{
 conn.Open();
 cmd=new SqlCommand("Select * from LoginTable where User Name='"+TextBox1.Text+"'",conn);
 dr=cmd.ExecuteReader();   //  <---error here

 if(dr.Read())
 {
   Label1.Text="User name already exist";
   this.Label1.ForeColor=Color.Red;
 }
 else
 {
   Label1.Text="Name available";
 }
}

It shows the following error near dr=cmd.ExecuteReader();:

An expression of non-boolean type specified in a context where a condition is expected, near 'Name'.

Whats happening here

like image 816
pratt Avatar asked Feb 23 '26 12:02

pratt


2 Answers

Replace the User Name in where clause with [User Name]

like image 60
Jahan Zinedine Avatar answered Feb 26 '26 01:02

Jahan Zinedine


The user name must be specified as [User Name] or UserName so please check column name once

like image 26
Randhi Rupesh Avatar answered Feb 26 '26 02:02

Randhi Rupesh