简体   繁体   中英

Error msg “Incorrect syntax near the keyword 'from' ”

I am reciving this error message when i enter the username and password, and i dont know what excatly is the error and how i can fix it. The error message is:

Incorrect syntax near the keyword 'from'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'from'.

Source Error:

Line 29:     string checkuser = "Select count * from UserInfo where UID='" + usrnamlogintxtbx.Text + "'";

Line 30: SqlCommand cmd = new SqlCommand(checkuser, log); Line 31: int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString()); Line 32: Line 33:

the error in line 31

this is the code behind:

public partial class login : System.Web.UI.Page
{

    string sc = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Loginbtn_Click(object sender, EventArgs e)
    {
            SqlConnection log = new SqlConnection(ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString);
log.Open();
string checkuser = "Select count * from UserInfo where UID='" + usrnamlogintxtbx.Text + "'";
SqlCommand cmd = new SqlCommand(checkuser, log);
int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());


log.Close();



if (temp == 1)
{
    log.Open();
    string checkpasswordquery = "Select Password from UserInfo where  UID='" + usrnamlogintxtbx.Text + "'";
    SqlCommand passcom = new SqlCommand(checkpasswordquery, log);
    string password = passcom.ExecuteScalar().ToString().Replace(" ","");


    if (password == usrnamloginpassbx.Text)
    {
        Session["UsrNme"] = usrnamlogintxtbx.Text;
        Response.Redirect("User panel.aspx");
    }

else
{
    passwronglbl.Text = "Password is incorrect";
}
}

    else {

    wronglogusernamelbl.Text = "Invalid User Name";
    }



}

}



} 

The clue is the exception type System.Data.SqlClient. SqlException . This means that your SQL is not valid.

From looking at your code, the problem appears to be your use of Count . Count is a function so you need to use Select count(*) from .

When I get a SQLException, I take the SQL code (and no, I don't use inline SQL cos that's bad m'kay) and execute it in SQL Server Management Studio as I find this is the quickest way to figure out what's wrong with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM