C
C#8mo 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
Kringe8mo ago
is the role just a string in the user entity or is it a seperate entity
Shogunex
ShogunexOP8mo 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
Kringe8mo ago
thanks 1 sec
Shogunex
ShogunexOP8mo 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
Kringe8mo 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
ShogunexOP8mo ago
No description
Kringe
Kringe8mo ago
im confused why is ur user a list<string> ❓ can you post a screenshot of the full code
Shogunex
ShogunexOP8mo ago
ah sorry, it's because of the code of my groupmate made, since he was testing things out without using any database
Shogunex
ShogunexOP8mo ago
that's the login code
Kringe
Kringe8mo ago
ow you still using any
Shogunex
ShogunexOP8mo ago
i've sent all the codes that was used for the login it was part of the tutorial that i saw
Kringe
Kringe8mo ago
what you are doing is also fine but to get the role you would need to make a second query
Shogunex
ShogunexOP8mo ago
i haven't gotten the chance to play around yet since our professor wants it working
Shogunex
ShogunexOP8mo ago
like this again?
No description
Shogunex
ShogunexOP8mo ago
i was thinking of changing any, but idk what to change it to get the role of the current username and password
Shogunex
ShogunexOP8mo ago
this is what's inside the db
No description
Kringe
Kringe8mo 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
ShogunexOP8mo ago
Ok, I'll test it out
Kringe
Kringe8mo ago
the second one I hope 😄
Hass
Hass8mo ago
Why does it makes two calls? Asking bc it seems that it does one Is it because you chain two operations?
Kringe
Kringe8mo 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
Hass8mo ago
I see, for some reason I thought EF optimized that
Shogunex
ShogunexOP8mo ago
Thank you so much! it works
Kringe
Kringe8mo ago
nice! I mean you can chain commands I meant var userExist = context.any var role = context.select are 2queries
Shogunex
ShogunexOP8mo ago
I'll now be able to continue with adding in the db and searching them thank you so much again!
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server