-1

This is the error I'm getting:

System.Data.SqlClient.SqlException: Incorrect syntax near ','. error

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;

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


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
            string s = "select count(*) from signup where [username]='" + txtusername.Text + "' ,  [passwd]= '" + txtpasswd.Text + "'";
            SqlCommand cmd = new SqlCommand(s, con);
            con.Open();
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            if(i>0)
            {
                Response.Redirect("analysis.aspx");
            }
           else
            {
                lblDisplay.Text=("Invalid User Credentials..");
            }
            con.Close();
        }
    }
}

Could someone point me in the right direction to find what's causing this error and how to solve it?

ItamarG3
  • 3,974
  • 6
  • 29
  • 42
K.Sindhu
  • 57
  • 1
  • 10

2 Answers2

1

Update your query as below:

string s = "select count(*) from signup where [username]='" + txtusername.Text + "' and  [passwd]= '" + txtpasswd.Text + "'";

Inline query execution is not preferable to use as it cause SQL Injection, better use as below:

string myQuery = "select count(*) from signup where [username]=@userName and  [passwd]= @password";

string connectionString='';//your connection string
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand(myQuery , conn))
    {        
        connection.Open();
        cmd.Parameters.Add(new SqlParameter("userName", txtusername.Text));
        cmd.Parameters.Add(new SqlParameter("password", txtpasswd.Text));
        cmd.ExecuteNonQuery();
    }
}
0

this is the error. You should use AND between 2 where clause. use like below. Change marked in bold.

string s = "select count(*) from signup where [username]='" + txtusername.Text + "' AND [passwd]= '" + txtpasswd.Text + "'";

Also do not use inline queries as they could lead to SQL Injection. Always use Stored procedures. Read more about sqlinjection.

Pawan Kumar
  • 1,981
  • 9
  • 12