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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:48:45+00:00 2026-05-20T09:48:45+00:00

Well I am working on a simple login screen for a game and it

  • 0

Well I am working on a simple login screen for a game and it uses username and password authentication. It connects to the database checks to see if username and password are there and then sees if it matches the data. If you insert the right username and password it works fine, but if you do one that is not in the database it fails and crashes. I was wondering am I doing this right? code below.

private void loginButton_Click(object sender, EventArgs e)
{
   string connectionString = "datasource=STUFFZ;database=users";
   string select = "SELECT Username, Password FROM RegularUsers WHERE Username = '" + usernameBox.Text + "' AND Password = '" + passwordBox.Text + "'";

   MySqlConnection my = new MySqlConnection(connectionString);

   MySqlCommand command = new MySqlCommand(select, my);
   my.Open();

   //String strResult = String.Empty;
   //strResult = (String)command.ExecuteScalar();
   string[] bba = new string[2];
   bba[1] = (String)command.ExecuteScalar();
   my.Close();

   if (bba[1].Equals(usernameBox.Text))
   {
      AdminPanel bb = new AdminPanel();
      bb.Show();
   }
   else
   {
      MessageBox.Show("INCORRECT USER/PASS!");
   }
}

The incorrect USER/PASS box never shows if you insert it wrong.

  • 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-20T09:48:45+00:00Added an answer on May 20, 2026 at 9:48 am

    Couple of comments:

    • don’t string together your SQL queries – use parametrized queries to avoid SQL injection
    • you should put your SqlConnection and SqlCommand into using(....) { ... } blocks
    • if you return two values, you should not use .ExecuteScalar() – that call only works for single row, single column returns

    So all in all, your code should be something like this:

    private void loginButton_Click(object sender, EventArgs e)
    {
       string connectionString = "datasource=STUFFZ;database=users";
       string select = "SELECT Username, Password FROM dbo.RegularUsers " + 
                       "WHERE Username = @user AND Password = @Pwd"
    
       using(MySqlConnection myConn = new MySqlConnection(connectionString))
       using(MySqlCommand command = new MySqlCommand(select, myConn))
       { 
           command.Parameters.Add("@user", SqlDbType.VarChar, 50);
           command.Parameters["@user"].Value = usernameBox.Text.Trim();
    
           command.Parameters.Add("@pwd", SqlDbType.VarChar, 50);
           command.Parameters["@pwd"].Value = passwordBox.Text.Trim();
    
           myConn.Open();
    
           using(SqlDataReader rdr = command.ExecuteReader())
           {
              if(rdr.Read())
              {
                 string userName = rdr.GetString(0);
                 string password = rdr.GetString(1);
    
                 rdr.Close();
    
                 // here compare those values and do whatever you need to do
              }
           }
    
           myConn.Close();
       }    
    }
    

    Furthermore, I think this code is a tad messy since you’re doing data access (selecting from SQL Server) and UI access (reading out textboxes, popping up dialog boxes) in the same snippet of code-

    You should strive for more separation of concerns, e.g.

    • define a method CheckUserName that takes in a user name and password as string, and returns e.g. a bool
    • from your event handler, get the information from the UI (read out the textboxes), call that separate function with these values, and then handle the returned value

    But mixing UI, logic and data access code – it gets messy and a maintenance nightmare really quickly!

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

Sidebar

Related Questions

I'm working on a simple timesheet plugin for Redmine, all was going well until
Well, simple question. I'm working with VS2008 on an ASP.NET web application which has
I'm working on a simple javascript login for a site, and have come up
I have an existing site that is working well with Authlogic login. I'm trying
I have a simple create user wizard control and It's working pretty well upto
Code below is working well as long as I have class ClassSameAssembly in same
An application that has been working well for months has stopped picking up the
reading excel files from C# working well in 32 bit version server. It is
I have a script working well for creating ad hoc iPhone builds. I can
I have a show/hide toggle working well in multiple instances (thanks to help here

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.