none
c# SQLite Winfom Password database RRS feed

  • Question

  • I have a winform I click a menu strip to open a new form to add new user and passwords. when I type the password to open the datagridview only one record is displayed. I still have to finish writing the add new user. but until then i need to correct the datagridview issue.

    Any help would be greatly appreciated.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SQLite;
    
    
    namespace Login_Form_SQLITE
    {
        public partial class New_User : Form
        {
           
            public New_User()
            {
                InitializeComponent();
                
            }
        
            private void btnSubmit_Click(object sender, EventArgs e)
            {
                if (txtUser.Text.Trim() == "" && txtPassword.Text.Trim() == "")
                {
                    MessageBox.Show("Empty Field");
                }
                else
                {
                    string query = "SELECT * FROM people WHERE UserName= @user AND Password= @password"; // wrong statment?
                    SQLiteConnection conn = new SQLiteConnection("Data Source=Login.db;Version=3;");
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(query, conn);
                    cmd.Parameters.AddWithValue("user", txtUser.Text);
                    cmd.Parameters.AddWithValue("Password", txtPassword.Text);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);                
                    dataGridView1.DataSource = dt; // only shows one line of data
    
    
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("You're Logged in", "Login Successful");
                                                                                 
                    }
                }
            }
        
    
            private void New_User_Load(object sender, EventArgs e)
            {
                
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                if (txtNew_User.Text == string.Empty || txtPassword.Text == string.Empty)
                {
                    MessageBox.Show("Please insert name");
                }
    
                {
                    string query = "insert into people (UserName,Password) values ('" + txtUser.Text + "', '" + txtPassword.Text + "')";
                       // "values('" + txtNew_User.Text + "', '" + txtPassword.Text + "')";
                    SQLiteConnection conn = new SQLiteConnection("Data Source=Login.db;Version=3;");
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(query, conn);
                    cmd.Parameters.AddWithValue("user", txtUser.Text);
                    cmd.Parameters.AddWithValue("Password", txtPassword.Text);
                    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
    
                }
            }      
        }
    }

     

    Booney440

    Sunday, February 16, 2020 5:30 PM

Answers

  • Thanks Karen

    The first option works but anything can be enter to open the datagridview, I need it to only open if the admin login and password is entered. I dont want other users to be able to access the gird.


    Booney440

    • Marked as answer by Booney440 Sunday, February 16, 2020 7:37 PM
    Sunday, February 16, 2020 6:30 PM

All replies

  • Hello,

    Your where condition is asking for a single record which is why only one record is being returned.

    string query = "SELECT * FROM people WHERE UserName= @user AND Password= @password"; // wrong statment?

    While this is not constrained by a WHERE

    string query = "SELECT * FROM people"; 
    SQLiteConnection conn = new SQLiteConnection("Data Source=Login.db;Version=3;");
    conn.Open();
    SQLiteCommand cmd = new SQLiteCommand(query, conn);
    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);                
    dataGridView1.DataSource = dt; // only shows one line of data

    Now if you are simply looking to see if the correct user name and password are entered then simple check e.g.

    string query = "SELECT * FROM people WHERE UserName= @user AND Password= @password";
    SQLiteConnection conn = new SQLiteConnection("Data Source=Login.db;Version=3;");
    conn.Open();
    SQLiteCommand cmd = new SQLiteCommand(query, conn);
    cmd.Parameters.AddWithValue("user", txtUser.Text);
    cmd.Parameters.AddWithValue("Password", txtPassword.Text);
    var reader = cmd.ExecuteReader();
    if(reader.HasRows())

    If reader.HasRows returns true then continue with what you wanted to do in the first place like populate the DataGridView this time without a Where condition.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, February 16, 2020 5:51 PM
    Moderator
  • Thanks Karen

    The first option works but anything can be enter to open the datagridview, I need it to only open if the admin login and password is entered. I dont want other users to be able to access the gird.


    Booney440

    • Marked as answer by Booney440 Sunday, February 16, 2020 7:37 PM
    Sunday, February 16, 2020 6:30 PM
  • Thank you for the Help, this worked for me.

     private void btnLogin_Click(object sender, EventArgs e)
            {
                if (txtUser.Text == "123")
                {
                    if (txtPassword.Text == "123")
                    {
    
    
                    }
                    else
                    {
                        MessageBox.Show("Error: Invaled password or username!", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Error: invaled password or username!");
                }
    
                string query = "SELECT * FROM people";
                SQLiteConnection conn = new SQLiteConnection("Data Source=Login.db;Version=3;");
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand(query, conn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt; // only shows one line of data
            }
        }
    }
    


    Booney440

    Sunday, February 16, 2020 7:37 PM