0

I have a website with a login, from a database.

This is my code :

        protected void SignIn_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Movie;Integrated Security=True");
            con.Open();

            string cmdStr = "select count(*) from Users";

            cmdStr += "where Username='" + UsernameSignIn.Text + "'";
            cmdStr+= "AND Password='"+PasswordSignIn.Text+"'";

            SqlCommand cmd = new SqlCommand(cmdStr, con);

            int i = Convert.ToInt16(cmd.ExecuteScalar());

            if (i == 0)
            {
                ErrorSignIn.Text = "Sorry, Wrong Username or Password";
            }
            else
            {
                Response.Redirect("HomeAfter.aspx");
            }
        }

for some reason, I run into an error :

Incorrect syntax near '-'

. (for this line : int i = Convert.ToInt16(cmd.ExecuteScalar()); )

Thanks,

2 Answers 2

2

There is no spacing. Your query looks like this:

select count(*) from Userswhere Username='...'AND Password='...'

Add spaces, like so:

string cmdStr = "select count(*) from Users";
cmdStr += " where Username='" + UsernameSignIn.Text + "'";
cmdStr+= " AND Password='"+PasswordSignIn.Text+"'";
Sign up to request clarification or add additional context in comments.

Comments

1

Aside from the fact that this is particularly crude as a form of authentication (you really ought to consider using the built-in ASP.NET Membership provider(s)) you should at a minimum be using parameterized SQL queries, rather than concatenating plain text to create your SQL statement. Also, I notice that your "login" arrangement simply does a response.redirect to the HomeAfter.aspx page without storing anything to be re-used that will indicate the user has already successfully logged in, such as a cookie or a sesssion variable.

Is there any particular reason for all this, or is it because you're just starting out and you need to study up a bit?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.