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 8723207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:34:39+00:00 2026-06-13T07:34:39+00:00

I am using: string selectString = SELECT username, password + FROM users + WHERE

  • 0

I am using:

                string selectString =
                "SELECT username, password " +
                "FROM users " +
                "WHERE username = '" + user + "' AND password = '" + password + "'";

                MySqlCommand mySqlCommand = new MySqlCommand(selectString, Program.mySqlConnection);
                Program.mySqlConnection.Open();
                String strResult = String.Empty;
                strResult = (String)mySqlCommand.ExecuteScalar();
                Program.mySqlConnection.Close();
                if (strResult.Length == 0)
                {
                    responseString = "invalid";
                    InvalidLogin = true;
                } else {
                    InvalidLogin = false;
                }

and at strResult.Length I get a NullReferenceException for some reason.

  • 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-13T07:34:41+00:00Added an answer on June 13, 2026 at 7:34 am

    This is how your code should look like:

    using(var connection = new MySQLConnection(connectionString))
    {
        using(var command = connection.CreateCommand())
        {
            command.CommandText = @"
    SELECT COUNT(*) 
    FROM users
    WHERE username = @user AND password = @password";
            command.Parameters.Add(new MySQLParameter("user", user));
            command.Parameters.Add(new MySQLParameter("password", password));
    
            var total = (int)command.ExecuteScalar();
            if(total == 0)
                InvalidLogin = true;
            else
                InvalidLogin = false;
        }
    }
    

    There are few things to notice

    • NEVER build your query string in that way you did it. Search on Google for “sql
      injection” to find more about what could happen to you. ALWAYS use
      parameters. I know, you wrote that this is console app, but good habits are important.
    • ALWAYS use using keyword when working with database
      connections and commands.
    • From your code, I have a feeling that you have global MySQLConnection object, right? NEVER do that! ADO.NET uses connection pooling, so opening new connection for your actions is not expensive operation. Make sure that you didn’t disable connection pooling in your connection string.
    • Not related to ADO.NET, but important: you should hash your password.

    To answer your question, the problem is in ExecuteScalar that you are using. It returns scalar variable (single value)…in your query, you are returning username and password, so you should use ExecuteReader instead…but I think that COUNT(*) in the query that I posted, together with ExecuteScalar might be a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: Here the goal is to do some basic commands in powershell using select-string.
I'm developing software which allows a user to select dynamically one or several users
While using string manipulation functions specificaly strcpy I did this small program. char s1[8]=Hellopo;
I am using String Tokenizer in my program to separate strings. Delimiter I am
In my code I am using String query= delete all from myTable; stmt.executeQuery(query); Ideally
Using String.Format how can i ensure all numbers have commas after every 3 digits
Friends i'm using string builder for generating Passenger List in which i have used
I am using string template to render some content, but the content may be
I have made a String[] array by using String.split(.) . I am now trying
I add GroupBoxes to an ItemsControl dynamically using: string name_ = TestName, header_ =

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.