Note that there are some explanatory texts on larger screens.

plurals
  1. POsql user detection not working as it should be
    primarykey
    data
    text
    <p>Im having trouble getting my program to detect admin users. I have created a login system, but when an admin logs in, it skips past the sql query and moves on to open a user screen, not an admin. When a user registers, they are presented with a pair of radio buttons to choose their user type. Depending on which one is selected, the User type (admin or user) is written into the database in a column User. Here is my code:</p> <pre><code>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; using System.Data.SqlTypes; namespace myLoginProject { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes"); connection.Open(); string selection = "select * from Logins where Name = '" + userNameBox.Text + "' and Password = '" + passwordBox.Text + "' "; SqlCommand command = new SqlCommand(selection, connection); SqlDataAdapter da = new SqlDataAdapter(command); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; } private void registerButton_Click(object sender, EventArgs e) { adminAuthScreen aas = new adminAuthScreen(); aas.Show(); } private int myMethod(string user, string pass) { user.Trim(); pass.Trim(); SqlConnection connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes"); connection.Open(); string selection = "select * from Logins where Name = '"+user+"' and Password = '"+pass+"' "; SqlCommand command = new SqlCommand(selection, connection); if (command.ExecuteScalar() != null) return 1; else return 0; } private void loginButton_Click(object sender, EventArgs e) { if (myMethod(userNameBox.Text,passwordBox.Text)&gt;0) { MessageBox.Show("Welcome back, "+userNameBox.Text); SqlConnection myConnection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes"); try { myConnection.Open(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } string checkAdmin1 = "SELECT * FROM Logins WHERE Name = '"+userNameBox.Text+"' AND User='Admin'"; SqlCommand checkIfAdmin = new SqlCommand(checkAdmin1, myConnection); if (checkIfAdmin.ExecuteScalar() != null) { adminScreen admnscrn = new adminScreen(); admnscrn.Show(); } else { userScreen usrscrn = new userScreen(); usrscrn.Show(); } } } public SqlConnection connection { get; set; } } } </code></pre> <p>It seems like this bit is the problem (at least its the problem during debugging):</p> <pre><code> private void loginButton_Click(object sender, EventArgs e) { if (myMethod(userNameBox.Text,passwordBox.Text)&gt;0) { MessageBox.Show("Welcome back, "+userNameBox.Text); SqlConnection myConnection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginTest;Trusted_Connection=yes"); try { myConnection.Open(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } string checkAdmin1 = "SELECT * FROM Logins WHERE Name = '"+userNameBox.Text+"' AND User='Admin'"; SqlCommand checkIfAdmin = new SqlCommand(checkAdmin1, myConnection); if (checkIfAdmin.ExecuteScalar() != null) { adminScreen admnscrn = new adminScreen(); admnscrn.Show(); } else { userScreen usrscrn = new userScreen(); usrscrn.Show(); } } } </code></pre> <p>Can anyone help me find out what the problem is??? Ive tried googling, reading up, i just cant seem to find the answer anywhere... P.S. Its a winforms app, written in C# and will be run on a single computer</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload