Click here to Skip to main content
16,013,465 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello

I have an error in my database command of query
when I run the program , an error appear on message as follow

incorrect syntax near the keyword "WHERE

this is the code
C#
public static Boolean AddTodo(string name, string email, string password)
       {
           Boolean c = false;

           try
           {

               SqlConnection connection = new SqlConnection();
               connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Documents\WinDB.mdf;Connect Timeout=30;User Instance=True;Integrated Security=SSPI";

               connection.Open();


               SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName", connection);
               command1.Parameters.Add(new SqlParameter("@UserName", name));
               command1.ExecuteNonQuery();

               SqlDataReader reader = command1.ExecuteReader();
               {

                   if (!reader.HasRows)
                   {
                       reader.Close();

                       SqlCommand command = new SqlCommand("insert into UserInfo. (UserName, UserEmail, Password) values (@UserName, @UserEmail, @Password)", connection);
                       command.Parameters.Add(new SqlParameter("@UserName", name));
                       command.Parameters.Add(new SqlParameter("@UserEmail", email));
                       command.Parameters.Add(new SqlParameter("@Password", password));

                       command.ExecuteNonQuery();
                       c = true;
                   }
                   else
                   {
                       MessageBox.Show("UserName is thaken");

                   }

               }


               connection.Close();

           }
           catch (SqlException e)
           {
               MessageBox.Show(e.Message);
           }

           return c ;

       }


why is there error ?
what is the wrong !! :(


thanks

PS: UserName key world is as in the database
i.e the same name
Posted

your code :
SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName", connection);


try this
SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo  WHERE UserName= @UserName", connection);


i hope this work :)
 
Share this answer
 
Comments
maxpower12345 9-May-12 15:51pm    
yup its work :)
thanks
whats this dot next your table name

C#
new SqlCommand("SELECT * FROM UserInfo. 
 
Share this answer
 
Comments
maxpower12345 9-May-12 15:51pm    
thanks :) its work
You seem to have an extra dot after the table name. Try:
C#
SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo WHERE UserName= @UserName", connection);


Note that the same applies to your INSERT statement
 
Share this answer
 
v2
Comments
maxpower12345 9-May-12 15:51pm    
thanks :) its work
Wendelius 9-May-12 15:53pm    
Glad it helped :)

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