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

  • SEARCH
  • Home
  • 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 8179087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:55:31+00:00 2026-06-06T23:55:31+00:00

I have created the following WebMethod in the back end of my application where

  • 0

I have created the following WebMethod in the back end of my application where the users login through the front end.

[WebMethod]
    public String Login(String userName, String password)
    {

            OleDbConnection connect = new OleDbConnection(connection);
            connect.Open();
            OleDbCommand command = new OleDbCommand("Select * from login where userName='" + userName + "'  and password ='" + password + "'", connect);
            command.CommandType = CommandType.Text;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            DataSet NSNSet = new DataSet();
            adapter.Fill(NSNSet);

            string username = NSNSet.Tables[0].Rows[0]["firstName"].ToString() + NSNSet.Tables[0].Rows[0]["lastName"].ToString();

            int userID = System.Convert.ToInt16(NSNSet.Tables[0].Rows[0]["UID"].ToString());

            return username + "," + userID;


    }

Currently, I have error handling in place which states –

catch(Exception ex)
            {
                string error = System.Convert.ToString(ex);
                if (error.Contains("There is no row at position 0"))
                {
                    status.Text = "Incorrect Username/Password combination";
                }
            }

This works fine, however how could I aulter my code so that it brings back a more specific error, i.e. states if the userName or password specifically are incorrect?

  • 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-06-06T23:55:32+00:00Added an answer on June 6, 2026 at 11:55 pm

    You should do like this:

    public String Login(String userName, String password)
        {
            OleDbConnection connect = new OleDbConnection(connection);
            connect.Open();
    
            OleDbCommand command = new OleDbCommand("Select UID, firstName, lastName from login where userName=?  and password =?", connect);
            command.CommandType = CommandType.Text;
    
            //to avoid sql injection
            command.Parameters.Add(userName);
            command.Parameters.Add(password);
    
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            DataSet NSNSet = new DataSet();
            adapter.Fill(NSNSet);
    
            if (NSNSet.Tables[0].Rows.Count == 0)
                return "Access denied";
    
            string username = NSNSet.Tables[0].Rows[0]["firstName"].ToString() + NSNSet.Tables[0].Rows[0]["lastName"].ToString();
            int userID = int.Parse(NSNSet.Tables[0].Rows[0]["UID"].ToString());
            return username + "," + userID;
        }
    

    Or a better way, using DataReader for performance:

    public String Login(String userName, String password)
        {
    
            OleDbConnection connect = new OleDbConnection(connection);
            connect.Open();
    
            OleDbCommand command = new OleDbCommand("Select UID, firstName, lastName from login where userName=?  and password =?", connect);
            command.CommandType = CommandType.Text;
    
            //to avoid sql injection
            command.Parameters.Add(userName);
            command.Parameters.Add(password);
    
            OleDbDataReader reader=command.ExecuteReader();
            if (reader.Read())
            {
                //that means there's at least one row
                string username = reader["firstName"] + " " + reader["lastName"];
                int userID = int.Parse(reader["UID"].ToString());
                return username + "," + userID;
            }
            else
            {
                //no combination username-password found
                return "Access denied";
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following WebMethod [WebMethod] public static string InnerGetURL(string accessCode) { string newURL;
I have created the following database layout, and started coding the application. The more
I have created the following view for users to create new items. Ive tried
I have created a .NET WebService. There I have implemented the following WebMethod: [WebMethod]
I have the following endpoint interface: @WebService public interface SEIWebService { @WebMethod @WebResult(name=CreateWorkOrderItemResponse) CreateWorkOrderItemResponse
I have created a following model public class myModel { public int ID; public
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I need to have the URL like photos/13/rate => I have created following routing
I have created the following class class Contact def initialize(id, name, phone) @id =

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.