Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want search database is user existing in the table.then i write this. but mark " SqlDataReader reder = cmd.ExecuteReader();" in yellow color and cant do my work?
is this problem abt" ExecuteReader()"???

help me soon as possible


SqlConnection con = new SqlConnection("Data Source=thushan-pc;Initial Catalog=dvd;Integrated Security=True");
bool b=false;
SqlCommand cmd=new SqlCommand("select User_Name from users where=('"+txtop.Text+"')",con);
con.Open();

SqlDataReader reder = cmd.ExecuteReader();
if (reder.Read() == true)
b = true;
reder.Close();
cmd.Dispose();

con.Close();
if (b == true)
MessageBox.Show("User here");
Posted
Updated 1-Oct-10 10:50am
v3

Firstly, don't do it like that - it leave you open to an SQL injection attack. Use parameterized queries instead (See SqlCommand.AddWithValue for details).

Secondly, the problem is your sql command. Try writing it out yourself assumeing txtop.Text is "John":
select User_Name from users where=('John')

Now try making it a legitimate Sql select statement:
SELECT User_Name FROM users WHERE User_Name='John'

(I have capitalised SQL syntax words to make it easier to decipher - worth doing as a general rule).

If you do this as a parameterized query, you won't need the quotes, either...

[edit]Finger trouble: there is no "Q" in "AddWithValue" - OriginalGriff[/edit]
 
Share this answer
 
v2
If you need to just check the count why dont you use Count?

using(SqlConnection con = new SqlConnection("Data Source=thushan-pc;Initial Catalog=dvd;Integrated Security=True"))
{
bool b=false;
using(SqlCommand cmd=new SqlCommand("select Count(User_Name) from users where UserName='"+txtop.Text+"'",con))
{
  con.Open();

   SqlDataReader reder = cmd.ExecuteReader();
  if (reder.Read())
     if(Convert.ToInt32(dr[0]) == 1)
  {
     b = true;
     break;
  }
reder.Close();
cmd.Dispose();
}
con.Close();
if (b == true)
MessageBox.Show("User here");
}

It is a good practice to use Count rather than only to rely on reder.Read(). I hope this will help you. :rose:
 
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