Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am making a login form and it attached with database when i retrieve data it show this sort of error:



Line 23: string query = "select count(*) from userlogin where username ='" + txtBox1.Text + "'and pass '" + txtBox2.Text + "'";
Line 24: SqlCommand cmd = new SqlCommand(query, con);
Line 25: string output = cmd.ExecuteScalar().ToString();
Line 26: if (output == "1")
Line 27: {

Source File: c:\Users\Atta\Documents\Visual Studio 2012\WebSites\WebSite3\Default2.aspx.cs Line: 25

Stack Trace:

the is showing on line number 25. while i am using such type of code behind login form:

What I have tried:

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


public partial class Default2 : 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["connect"].ToString());
        con.Open();
        string query = "select count(*) from userlogin where username ='" + txtBox1.Text + "'and pass '" + txtBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        string output = cmd.ExecuteScalar().ToString();
        if (output == "1")
        {
            Session["user"] = txtBox1.Text;
            Response.Redirect("Login Successful");
        }
        else
        {
            Response.Write("Login Faild");
        }
    }
}
Posted
Updated 9-Jul-17 23:41pm
Comments
F-ES Sitecore 10-Jul-17 5:30am    
And the error is?

BTW you should google how to use parameterised queries as your code is vulnerable to SQL injection attacks and also issues where people have apostrophes in their usernames or passwords.
StM0n 10-Jul-17 5:30am    
Which error... there're some that could occur.

Sorry to ask, but are you aware of the circumstance, that your login functionality is open for sql-injection?!
Richard Deeming 10-Jul-17 8:39am    

Without looking much into your code and details, I could say the error is because of the missing equals operator in the sql query.

Replace line no. 23 with following and see if that helps-
C#
string query = "select count(*) from userlogin where username ='" + txtBox1.Text + "'and pass ='" + txtBox2.Text + "'";

But wait, that's not all.
Your code is vulnerable to SQL Injection[^].
With little more effort, you can prevent SQL injection via Stored Procedure or parameterised query. Please follow below reference for further guidance-
How To: Protect From SQL Injection in ASP.NET[^]
[^]

Hope, it helps :)
 
Share this answer
 
 
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