Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6211829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:24:22+00:00 2026-05-24T06:24:22+00:00

I have a database created in SQL Server 2008 on my machine. It contains

  • 0

I have a database created in SQL Server 2008 on my machine. It contains a table named login having two columns of username and password with certain values.

This is my web service code written in Visual Studio 2008. I need to check whether the user name and password I provide is correct or not i.e basically I want to authenticate the user via web service.

So can anyone tell me how should I can do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:24:23+00:00Added an answer on May 24, 2026 at 6:24 am
    1. I would urge you to use parameters when dealing with SQL as to not be vulnerable to SQL Injection; especially for a public facing web service.

    2. It appears from your queries you are storing passwords in plain text. Again, I would urge you to salt and hash your user’s passwords for better security.

    3. You can accomplish what you are looking for by changing your query to a COUNT(*), and executing as a scalar, and making sure the count is greater equal to one. Something like this:

      myCommand.CommandText = "select COUNT(*) from Login where Username = @Username AND Password = @HashedPassword";
      int rowCount = (int)mycommand.ExecuteScalar();
      return rowCount == 1 ? "success" : "bad username or password";
      

    The SqlDataReader should be removed from your original code.

    EDIT:
    So what is this code doing?

    We’ve changed the SQL Query slightly. I’ve also modified it to use parameters. So what this query is saying is Get me the number of users that have this username and this password as an integer.

    So if one user has that username and password, it would return 1. If no one had that username and password, it would return 0.

    ExecuteScalar is useful when you aren’t expecting any rows back; just a single value. This is true in our case; all we are expecting is a number: the number of users.

    So when you put all of this code together; it should look something like this:

    try
    {
        using(SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
        {
            myConnection.Open();
    
            using (SqlCommand myCommand = new SqlCommand())
            {
                myCommand.Connection = myConnection;
                myCommand.CommandText = "SELECT COUNT(*) FROM Login WHERE UserName = @UserName AND Password = @Password";
                myCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
                myCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
                return (int) myCommand.ExecuteScalar() == 1 ? "success" : "bad username or password";
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return "an error occurred.";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that I have created using SQL Server Developer 2008. I
I have created one user named tuser with create database rights in SQL server
I have an SQL server 2008 database instance on one machine. Now I want
I have to move an entire database from a SQL Server 2008 machine to
I have created a database test with some tables in MS SQL Server 2008
I have a database in SQL Server 2008 R2. This database contains some tables,
Tables have been created in our SQL Server 2008 R2 database on a local server,
I have designed database tables (normalised, on an MS SQL server) and created a
I have table a and table b . (SQL Server 2008) Both tables have
I have created an Amazon EC2 Instance that provides Windows Server 2008 with SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.