954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display error or success message after validating username

I know this question has been asked dozens of times, but the code I have been looking at has not helped me.

So far I have been able to connect to the string and the database from where I need to check the username, but in my page it is not checking whether the username is correct or not.

Please someone tell me what I am doing wrong and what I need to change to make this work, I have been on this for literally a week and still no success.

Thank you!

protected void Verify_Click(object sender, EventArgs e)
{
//connect to sql
string ConnectString = "Data Source=sql12;Initial Catalog=import_log;Persist Security Info=True;User ID=sa;Password=password1";
SqlConnection con = new SqlConnection(ConnectString);
con.Open();

string sql = "SELECT userID from from user_verification WHERE userID = '" + username;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (username.Text == "userID")
{
Output.Text = "Correct username";
}

con.Close();
}
}

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Are you sure that the "userID" column holds the user name?

instead of
string sql = "SELECT userID from from user_verification WHERE userID = '" + username;
do
string sql = "SELECT userID from from user_verification WHERE userID = '" + username.text;

instead of

if (username.Text == "userID")
                {
                    Output.Text = "Correct username";
                }

do:

if (username.Text == reader("userID"))
                {
                    Output.Text = "Correct username";
                }


and don't forget to close the reader after the while loop.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

Will this same code work for the code-behind in asp.net?

I tried using reader in the if statement and I get an error saying that the reader variable is a variable being used like a method.

Are you sure that the "userID" column holds the user name?

instead of string sql = "SELECT userID from from user_verification WHERE userID = '" + username; do string sql = "SELECT userID from from user_verification WHERE userID = '" + username.text;

instead of

if (username.Text == "userID")
                {
                    Output.Text = "Correct username";
                }

do:

if (username.Text == reader("userID"))
                {
                    Output.Text = "Correct username";
                }

and don't forget to close the reader after the while loop.

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: