Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i'm trying to match the users name, surname and password input and match against the database where they registered but getting the following when i press the login button:

"Syntax error (missing operator) in query expression '[name]','jordan''."

i just need a little help as I'm new to this and need a push towards the right direction any books or articles will also help. Thanks in advance

The Code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class login : System.Web.UI.Page
{
    \\ create connection object
    private static OleDbConnection GetConnection()
    {
        String connString;
        connString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\Wisal\Documents\Visual Studio 2012\WebSites\WebSite3\registration-Db.mdb";

        return new OleDbConnection(connString);

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void loginButtn_Click(object sender, EventArgs e)
    {
        OleDbConnection myConnection = GetConnection();


        try
        {
            myConnection.Open();
            Console.WriteLine("Connection Opened");
            String checkUser = "select count(*) from client where [name]" + "','" + nameloginBox.Text + "'";

            OleDbCommand myCommand = new OleDbCommand(checkUser, myConnection);
            myCommand.ExecuteNonQuery();     \\ error occurs here when login button is pressed Syntax error (missing operator) in query expression '[name]','jordan''."
            myConnection.Close();
            {

                myConnection.Open();
                String checkname = "select count(*) from client where surname" + snameloginBox.Text + "'";

                OleDbCommand checkSname = new OleDbCommand(checkUser, myConnection);
                checkSname.ExecuteNonQuery();
                myConnection.Close();
                {
                    myConnection.Open();
                    String checkPassword = "select count(*) from client where [password]" + passwrdloginBox + "'";

                    OleDbCommand passComm = new OleDbCommand(checkPassword, myConnection);
                    String password = passComm.ExecuteNonQuery().ToString().Replace(" ","");
                    if (password == passwrdloginBox.Text)
                    {
                        Session[""] = nameloginBox.Text;
                        Response.Write("Password Correct");
                    }
                    else
                    {
                        Response.Write("Password Incorrect");
                    } 
                    

                }
            }
        }


        finally
        {
            myConnection.Close();
        }
    }
}
Posted
Comments
Richard Deeming 26-Nov-15 13:21pm    
Also, you're storing passwords in plain-text. That's an extremely bad idea. You should only ever store a salted hash of the user's password.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Our password hashing has no clothes - Troy Hunt[^]
Member 12169192 26-Nov-15 13:29pm    
that's brilliantI'll have a read! thank you

Firstly sql query syntax was incorrect - that is why you were getting error.
As you don't need column value from query, it is better to use ExecuteScalar()

Secondly it is recommended to use make query safe otherwise it violates SQL injection.

C#
OleDbCommand myCommand = new OleDbCommand(checkUser, myConnection);
myConnection.Open();
String checkPassword = "select count(*) from client where [password] = ?";

OleDbCommand passComm = new OleDbCommand(checkPassword, myConnection);
OleDbParameter p1 = new OleDbParameter();
p1.Value = passwrdloginBox.Text;
passComm.Parameters.Add(p1);

int rowsAffected = (int)passComm.ExecuteScalar();

if (rowsAffected > 0)
{
	Session[""] = nameloginBox.Text;
	Response.Write("Password Correct");
}
else
{
	Response.Write("Password Incorrect");
} 
 
Share this answer
 
v2
Comments
Member 12169192 26-Nov-15 13:27pm    
@Manas_Kumar just applied the changes to the passwordcheck and I'm still getting the same error.
the error get caught in the myCommand.ExecuteNonQuery(); under OleDbCommand myCommand = new OleDbCommand(checkUser, myConnection); Thanks
[no name] 26-Nov-15 13:35pm    
Just try partially copy/paste my code. Because checkUser and myConnection is already defined in your code.
There is a typo; comma should be an equal sign. The error message tells you exactly what is the problem (missing operator) and where it occurs.
 
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