I'm automating student information system in C # programming language and visual studio. I am using MSSQL as a database. At first I had created a lot of different tables such as students, teachers, parents, lectures and admin. But then I decided to collect them in one table. That's my question.
I've created a variable named userType in the sql query. When the user in the table with UserType 1 logs into the system, for example, go to the admin panel. I want the user to go to the student table when the person who has 2 has logged in. I'm a student at first, if you're a teacher, if you're admin, I would open three label labels separately and process each label individually. But now I want the user to go to the appropriate form based on the type when the user logs in. So I said the performance would be better.
The Sql query is here;
declare @userType SMALLINT
if (@userType = 1)
"Select * from singleTable where UserType = 1 and UserName = '" + userName.Text + "'"
else if (@userType = 2) Select * from singleTable where UserType = 2 and UserName = '" + userName.Text + "'
else if (@userType = 3) select * from singleTable where UserType = 3 and UserName = '" + userName.Text + "'
It's okay up here. But I couldn't figure out how to send the form to the appropriate form.
query = new SqlCommand ("declare @userType SMALLINT if @userType = 1" +
"Select * from singleTable where UserType = 1 and UserName = '" + userName.Text + "'" +
"else if @userType = 2 Select * from singleTable where UserType = 2 and UserName = '" + userName.Text + "'" +
"else if @userType = 3 select * from singleTable where UserType = 3 and UserName = '" + userName.Text + "'", conn);
dr = query.ExecuteReader ();
if (dr.Read ())
{
MessageBox.Show ("Login is successful. Welcome" "+ userName.Text +" '");
studentPanel form = new studentPanel ();
form.userName = userName.Text;
Form.ShowDialog ();
this.Hide ();
}
The above is the code you provide if you're seeing access to a single form. I want to connect with the @userType variable above.
So I want to open new if blocks in if (dr.Read).
if (@userType = 1) {
adminPanel form = new adminPanel ();
}
else if (@userType = 2) {
teacherPanel form = new teacherPanel ();
}
else if (@userType = 3) {
studentPanel form = new studentPanel ();
}
Like ... Waiting for your help. Respects.
Hey. SqlConnection is have null value.
private void loginButton_Click(object sender, EventArgs e)
{
string connection = @"Data Source=DESKTOP-AG9TT68;Initial Catalog=studentInformation;Integrated Security=True";
SqlConnection conn = new SqlConnection(connection);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
var query = new SqlCommand(@"IF @UserType = 'admin'
BEGIN
Select * from singleTable where UserType = 'admin' and UserName = @Username;
END
IF @UserType = 'teacher'
BEGIN
Select * from singleTable where UserType = 'teacher' and UserName = @Username;
END
IF @UserType = 'student'
BEGIN
select * from singleTable where UserType = 'student' and UserName = @Username;
END", conn);
//You should pass parameters to avoid SQL injection
var userType = "@UserType";
query.Parameters.AddWithValue("@UserType", userType);
query.Parameters.AddWithValue("@Username", userName.Text);
var dr = query.ExecuteReader();
if (dr.Read())
{
if (userType == "'admin'")
{
MessageBox.Show("Login is successful. Welcome '" + userName.Text + "'");
adminPanel form = new adminPanel();
form.ShowDialog();
this.Hide();
}
else
{
MessageBox.Show("Your username or password is wrong!");
}
if (userType == "'teacher'")
{
MessageBox.Show("Login is successful. Welcome '" + userName.Text + "'");
teacherPanel form = new teacherPanel();
form.ShowDialog();
this.Hide();
}
else
{
MessageBox.Show("Your username or password is wrong!");
}
if (userType == "'student'")
{
MessageBox.Show("Login is successful. Welcome '" + userName.Text + "'");
studentPanel form = new studentPanel();
form.ShowDialog();
this.Hide();
}
else
{
MessageBox.Show("Your username or password is wrong!");
}
}
}
}