Forums - C# Corner

Forum guidelines
Meena S

Meena S

  • 1.8k
  • 33
  • 532

Must declare scalar variable in login form

May 3 2019 12:19 AM
I am getting error "Must declare scalar variable  @uname and @pw". I have tried many methods but its not working. please guide me to find the issue in my belo code.
 
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3. OleDbConnection con = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=DESKTOP\SQLEXPRESS;User ID=sa; Password = pwd; Initial Catalog=database; Integrated secutity=SSPI");  
  4. con.Open();  
  5. OleDbCommand cmd = new OleDbCommand("select Username,password from login where Username=@uname and password=@pw", con);  
  6. OleDbDataAdapter da = new OleDbDataAdapter(cmd);  
  7. cmd.Parameters.Clear();  
  8. //cmd.Parameters.AddWithValue("@uname", textBox1.Text);  
  9. //cmd.Parameters.AddWithValue("@pw", textBox1.Text);  
  10. DataTable dt = new DataTable();  
  11. da.Fill(dt);  
  12. if (dt.Rows.Count > 0)  
  13. {  
  14. MessageBox.Show("success");  
  15. }  
  16. else  
  17. {  
  18. MessageBox.Show("incorrect");  
  19. }  
  20. con.Close();}  

Answers (3)