I have a login page whenever the authentication is correct or equal to 1 I want to pass that to the next window, but my problem is I cannot store the values into the class object that I made.
The idea is, if the username and password is correct then I want to store their values into an object so that I can pass it to the next window and reuse it. A tip about separating them would also be good. sorry I'm dumb, just started C# two weeks ago.
// logging-in process codes
private void LoggingIn()
{
SqlConnection sqlCon = new SqlConnection(@"Data Source=localhost\SQLEXPRESS; Initial Catalog=SalesDB; Integrated Security=true;"); // this is the connection to the database
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
String query = "SELECT COUNT(1) FROM users_tbl WHERE Username = @Username AND Password = @Password";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.CommandType = CommandType.Text;
sqlCmd.Parameters.AddWithValue("@Username", txtUsername.Text);
sqlCmd.Parameters.AddWithValue("@Password", txtPassword.Password);
int count = Convert.ToInt32(sqlCmd.ExecuteScalar()); //if true the value will be converted to "1" in integer.
LoginInfo userInfo = new LoginInfo();
using (SqlDataReader oReader = sqlCmd.ExecuteReader())
{
while (oReader.Read())
{
userInfo.UserName = oReader["Username"].ToString();
userInfo.PassWord = oReader["Password"].ToString();
userInfo.Role = oReader["Role"].ToString();
userInfo.FirstName = oReader["First_Name"].ToString();
userInfo.LastName = oReader["Last_Name"].ToString();
}
}
if (count == 1)
{
MainWindow dashboard = new MainWindow();
dashboard.Show();
this.Close();
}
else
{
MessageBox.Show("Username or Password is incorrect. ");
txtUsername.Focus();
txtUsername.Clear();
txtPassword.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlCon.Close();
}
}
// login button code
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
LoggingIn();
}
// my LoginInfo Class
namespace TestApp
{
public class LoginInfo
{
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value; }
}
private string role;
public string Role
{
get { return role; }
set { role = value; }
}
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
}
SELECT COUNT(1) FROM users_tbl where Username=@Username AND Password=@Passwordthis will return the number of rows that match your where clause, not individual columns. Did you mean to useselect top 1 *?SELECT * FROM users_tbl where Username=@Username AND Password=@Passwordand go from there. If you need both the count and the data as separate queries, you'll need two different sql commands.