C
C#5w ago
Shogunex

✅ Getting a single value in sql db

I am currently making a information system for our project in school and I am stuck at the login part wherein I need to get the role value of the username and password so they'll go to their respective panel once they've clicked logged in. I've been searching online and couldn't find an answer. Currently this is my code for my login button that i've followed in Youtube.
private void btnClick(object sender, RoutedEventArgs e)
{
/* String UN = tbGetter(tbUsernameLogin);
String PW = tbGetter(tbPasswordLogin);

panelMaster(validLogin(UN, PW));*/

var Username = tbUsernameLogin.Text;
var Password = passwordBox_password.Password;

using (UserDataContext context = new UserDataContext())
{
bool userFound = context.Users.Any(user => user.Name == Username && user.Password == Password);


if (userFound)
{


}
else
{
MessageBox.Show("User Not Found");
}

}



}
private void btnClick(object sender, RoutedEventArgs e)
{
/* String UN = tbGetter(tbUsernameLogin);
String PW = tbGetter(tbPasswordLogin);

panelMaster(validLogin(UN, PW));*/

var Username = tbUsernameLogin.Text;
var Password = passwordBox_password.Password;

using (UserDataContext context = new UserDataContext())
{
bool userFound = context.Users.Any(user => user.Name == Username && user.Password == Password);


if (userFound)
{


}
else
{
MessageBox.Show("User Not Found");
}

}



}
The database currently has ID, Name, Password, and Role. I need to get the Role of the current Name and Password that was used to login so i can put it in the panelMaster() so it'll go to their own panel.
29 Replies
Kringe
Kringe5w ago
is the role just a string in the user entity or is it a seperate entity
Shogunex
Shogunex5w ago
it's a string
public class User
{

[Key]

public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public string Role { get; set; }




}
public class User
{

[Key]

public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public string Role { get; set; }




}
Kringe
Kringe5w ago
thanks 1 sec
Shogunex
Shogunex5w ago
and this is for how my db was created and checked if there's a db
public class UserDataContext: DbContext
{

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source = DataFile.db");
}

public DbSet<User> Users { get; set; }

}
public class UserDataContext: DbContext
{

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source = DataFile.db");
}

public DbSet<User> Users { get; set; }

}
Kringe
Kringe5w ago
using (UserDataContext context = new UserDataContext())
{
var user = Context.Users.FirstOrDefault(u => u.Name == Username && u.Password == Password);


if (user != null)
{

MessageBox.Show(user.Role);
}
else
{
MessageBox.Show("User Not Found");
}
}
using (UserDataContext context = new UserDataContext())
{
var user = Context.Users.FirstOrDefault(u => u.Name == Username && u.Password == Password);


if (user != null)
{

MessageBox.Show(user.Role);
}
else
{
MessageBox.Show("User Not Found");
}
}
something like this I have written it in notepad so could be some syntax errors
Shogunex
Shogunex5w ago
No description
Kringe
Kringe5w ago
im confused why is ur user a list<string> ❓ can you post a screenshot of the full code
Shogunex
Shogunex5w ago
ah sorry, it's because of the code of my groupmate made, since he was testing things out without using any database
Shogunex
Shogunex5w ago
that's the login code
Kringe
Kringe5w ago
ow you still using any
Shogunex
Shogunex5w ago
i've sent all the codes that was used for the login it was part of the tutorial that i saw
Kringe
Kringe5w ago
what you are doing is also fine but to get the role you would need to make a second query
Shogunex
Shogunex5w ago
i haven't gotten the chance to play around yet since our professor wants it working
Shogunex
Shogunex5w ago
like this again?
No description
Shogunex
Shogunex5w ago
i was thinking of changing any, but idk what to change it to get the role of the current username and password
Shogunex
Shogunex5w ago
this is what's inside the db
No description
Kringe
Kringe5w ago
but if you do this you call the db twice so I reccomend this var user = Context.Users.FirstOrDefault(u => u.Name == Username && u.Password == Password); so you can check if the user exists and then get the role
Shogunex
Shogunex5w ago
Ok, I'll test it out
Kringe
Kringe5w ago
the second one I hope 😄
Hass
Hass5w ago
Why does it makes two calls? Asking bc it seems that it does one Is it because you chain two operations?
Kringe
Kringe5w ago
the code also doesn't work made a mistake but I meant that he first checks if the user exists and then with a separate call gets the role which I recommend doing in one call
Hass
Hass5w ago
I see, for some reason I thought EF optimized that
Shogunex
Shogunex5w ago
Thank you so much! it works
Kringe
Kringe5w ago
nice! I mean you can chain commands I meant var userExist = context.any var role = context.select are 2queries
Shogunex
Shogunex5w ago
I'll now be able to continue with adding in the db and searching them thank you so much again!
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5w ago
Use the /close command to mark a forum thread as answered
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View