-1

I want to set status equal to the status present in the information in my database, a person can be either an admin or a user, based on if the person is an admin or user a different form would be loaded.

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.SqlClient;

namespace CSCI_610_Northwind
{
    public partial class formLogin : Form
    {
        public formLogin()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-KEVKC22\SQLEXPRESS;Initial Catalog=northwind;Integrated Security=True");
        private void formLogin_Load(object sender, EventArgs e)
        {

        }
        
        private void butLogin_Click(object sender, EventArgs e)
        {
            String username, password, status;

            username = txtUsername.Text;
            password = txtPassword.Text;
           

            try
            {
                String query = "SELECT * FROM Information WHERE username = '" + txtUsername.Text + "' AND password = '" + txtPassword.Text + "'";
                SqlDataAdapter sda = new SqlDataAdapter(query, conn);

                DataTable dtable = new DataTable();
                sda.Fill(dtable);



                if (dtable.Rows.Count > 0)
                {
                    username = txtUsername.Text;
                    password = txtPassword.Text;
                    status = ""; 
             
                    if (status == "Admin")
                    {
                        Form2 frm2 = new Form2();
                        frm2.Show();
                        this.Hide();
                    }
                    else
                    {
                        Form1 frm1 = new Form1();
                        frm1.Show();
                        this.Hide();
                    }
                }
                else
                {
                    MessageBox.Show("Invalid Login", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.Clear();
                    txtPassword.Clear();
                }
            }
            catch
            {
                MessageBox.Show("Error");
            }
            finally
            {
                conn.Close();
            }
        }

        private void butClear_Click(object sender, EventArgs e)
        {
            txtUsername.Clear();
            txtPassword.Clear();
        }
    }
}

Data Screenshot

I'm not sure as to how to set status equal to the status present in the database.

I tried setting status = "SELECT status FROM Information WHERE username = '" + txtUsername.Text + "' AND password = '" + txtPassword.Text + "'";, but all it was doing was making it equal to the statement and not the actual value I wanted.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Jason_27
  • 21
  • 2

1 Answers1

0

You already pulled the data into dtable.

You could just

status = dtable .Rows[0][2].ToString();
Mauricio
  • 3
  • 3