Я пытаюсь создать экран входа в систему, в котором пользователь вводит свой адрес электронной почты и пароль, а оператор SQL находит идентификатор, связанный с его учетной записью. Я использую ExecuteScalar, чтобы назначить вывод из оператора переменной loggedID, но он вызывает исключение InvalidOperationException.
Points.userLogin.Parameters.Add("@email", SqlDbType.NVarChar).Value = txtEmail.Text;
Points.userLogin.Parameters.Add("@pass", SqlDbType.NVarChar).Value = txtPass.Text;
Points.userLogin.CommandText=
"SELECT CustomerID FROM tblCustomers WHERE CustomerEmail = @email AND CustomerPassword = @pass";
Points.pointsCon.Open();
Console.WriteLine("Opening Connection");
try
{
int loggedID = (int)Points.userLogin.ExecuteScalar(); // Line in question
Console.WriteLine("Assigning ID to variable");
// -------------------TEMP---------------------
MessageBox.Show("insert login page");
// -------------------TEMP---------------------
}
catch
{
MessageBox.Show("You have entered your email or password incorrectly. Please try again.",
"Incorrect Credentials",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
Points.pointsCon.Close();
Console.WriteLine("Closing Connection");
Правка: точное полученное сообщение: Exception thrown: 'System.InvalidOperationException' in System.Data.dll
Правка : это мой файл points.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace PenPoints
{
class Points
{
// Connects the class to the database via the connection string
public static SqlConnection pointsCon = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename='C:\\Users\\matth\\Documents\\Lessons\\Project\\The Actual Program\\PenPoints\\PointsDB.mdf';Integrated Security=True;Connect Timeout=30");
// Command to create account
public static SqlCommand createAccount = new SqlCommand();
// Command to log in
public static SqlCommand userLogin = new SqlCommand();
}
}