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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:16:56+00:00 2026-06-15T09:16:56+00:00

This is my first time asking a question on StackOverflow, so I apologize in

  • 0

This is my first time asking a question on StackOverflow, so I apologize in advance if I ask someone improper. I couldn’t find anything to help me while researching this for the past few days, so thank you in advance to anyone who tries to help.

I am making a database that allows people to register and log-in. I am using C# in VS2012.
Below is my log-in code and I am running into some trouble when testing. It iterates through everyone in the database and tells me that log-in has failed till it gets to the right user.

    private void button1_Click_1(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
        }
        catch (Exception)
        {
            MessageBox.Show("Did not connect");
        }


        SqlCommand cmd = new SqlCommand("SELECT * FROM [Users]", cn);
        cmd.Connection = cn;
        SqlDataReader reader = null;
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            if (textBox1.Text == (reader["Username"].ToString()) && textBox2.Text == (reader["Password"].ToString()))
            {
                MessageBox.Show("Logged in");
            }
            else
            {
                MessageBox.Show("Login has failed. Please check your Username and Password.");
            }
        }
        cn.Close();
    }

As for my registration portion, I’m not sure if it is a VS2012 thing or what, but the information doesn’t get saved into the database after I end debug and then go back to debug again.

    private void button1_Click_1(object sender, EventArgs e)
    {
        cn.Open();
        SqlCommand cm1 = new SqlCommand("INSERT INTO Users (Username, Password) VALUES (@Username, @Password)", cn);
        SqlCommand cm2 = new SqlCommand("INSERT INTO Contact(Name, Address, City, State, PostalCode, Email, PhoneNumber) VALUES(@Name, @Address, @City, @State, @PostalCode, @Email, @PhoneNumber)", cn);



        cm1.Parameters.AddWithValue("@Username", textBox1.Text);
        cm1.Parameters.AddWithValue("@Password", textBox2.Text);
        cm2.Parameters.AddWithValue("@Name", textBox3);
        cm2.Parameters.AddWithValue("@Address", textBox4);
        cm2.Parameters.AddWithValue("@City", textBox5);
        cm2.Parameters.AddWithValue("@State", textBox6);
        cm2.Parameters.AddWithValue("@PostalCode", textBox7);
        cm2.Parameters.AddWithValue("@Email", textBox8);
        cm2.Parameters.AddWithValue("@PhoneNumber", textBox9);

        try
        {
            int affectedRows =  cm1.ExecuteNonQuery(); //+cm2.ExecuteNonQuery();

            if (affectedRows > 0)
            {
                MessageBox.Show("Insert Sucsess!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        cn.Close();
    }
  • 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-15T09:16:57+00:00Added an answer on June 15, 2026 at 9:16 am

    When you have a database file in you project and you build the project, the database file could be copied from the root project folder into the output (bin\debug or bin\release) folder.
    This behavior is controlled by the Copy To Output Directory property of the database file.

    If you have this property set to Copy Always then, every time you build your project a fresh copy of the database file is copied from the root project folder to the output directory overwriting the one already there and destroying the changes you have made in the previous debug session.

    A suggested fix is to change this property to Copy Never or Copy if Newer

    See a detailed explanation on MSDN at this page

    For the first part of your question you could avoid to loop on every user adding a WHERE clause to your sql text. Just be aware that you should never use string concatenation to build your sql strings, instead you use ALWAYS the parameters. (Why? You avoid Sql Injection and text single quote parsing/doubling)

    string sqlText = "SELECT * FROM [Users] WHERE Username = @usr AND [Password] = @psw";
    SqlCommand cmd = new SqlCommand(sqlText, cn);
    cmd.Parameters.AddWithValue("@usr", textbox1.Text);
    cmd.Parameters.AddWithValue("@psw", textbox2.Text);
    SqlDataReader reader = cmd.ExecuteReader();
    if(reader.HasRows)
        // You have found the user....
    

    Another bit of advice. Do not store the passwords in clear text inside your database. Store always an hash of this string and, on search, compute the hash value and search for it instead of a clear password.

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

Sidebar

Related Questions

this is my first time asking a question on stackoverflow. I'm working on a
This is my first time asking my question at stackoverflow. I'm working on a
This is my first time asking a question on Stackoverflow. I can't get a
this is my first time asking question in stackoverflow. I tried hard not to
this is my first time asking a question here. I tried to be well
this is my first time asking a question so bear with me. I am
I am new to Python and this is my first time asking a stackOverflow
First time asking a question here. Usually I can find my answer without having
First of all this is the first time I am asking a java question
This is my first time asking a question, please be gentle! I have a

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.