1

What I want to achieve is that when the student logs into the system, the system should fetch his/her studentID from the database. Then in my "test form" - where the student takes a test. The system should add the test details plus the studentID who took the test into the "test" table.

PROBLEM: I do not know how to pass the studentID from the login form into the test form.

I have some code which I have tried out which is a result from the RESEARCH I took but it still will not work for me:

Log In form Code: [only relevant bit is the 'StudentID' part]

//gets the info and checks if the input is same as contents in DB
using (MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;database=project;username=root;password=***;"))
{
MySqlCommand cmd = new MySqlCommand("SELECT student.studentID, student.studentUsername, student.studentPassword, student.studentFirstName, student.studentLastName, teacher.teacherUsername, teacher.teacherPassword, teacher.teacherFirstName, teacher.teacherLastName FROM student, teacher WHERE (student.studentUsername = @userName AND student.studentPassword = @passWord) OR (teacher.teacherUsername = @teacherUser AND teacher.teacherPassword = @teacherPass);");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@userName", textboxUsername.Text);
cmd.Parameters.AddWithValue("@passWord", texBoxPassword.Text);
cmd.Parameters.AddWithValue("@teacherUser", textboxUsername.Text);
cmd.Parameters.AddWithValue("@teacherPass", texBoxPassword.Text);
MySqlDataReader DBReader;
cmd.Connection = connection;
connection.Open();
DBReader = cmd.ExecuteReader();
while (DBReader.Read())
{
var teacherFirstName = DBReader.GetString("teacherFirstName");
var teacherLastName = DBReader.GetString("teacherLastName");
var teacherLogin = DBReader.GetString("teacherUsername");
var teacherPass = DBReader.GetString("teacherPassword");
var studentFirstName = DBReader.GetString("studentFirstName");
var studentLastName = DBReader.GetString("studentLastName");
var studentLogin = DBReader.GetString("studentUsername");
var studentPass = DBReader.GetString("studentPassword");
string studentID = DBReader.GetString("studentID");
if (teacherLogin == textboxUsername.Text && teacherPass == texBoxPassword.Text)
{
 MessageBox.Show("Welcome " + teacherFirstName + " " + teacherLastName);
 this.Hide();
 TeacherHomepage Form = new TeacherHomepage();
 Form.Show();
}
else if (studentLogin == textboxUsername.Text && studentPass == texBoxPassword.Text)
{
  MessageBox.Show("Welcome " + studentFirstName + " " + studentLastName);
  this.Hide();
  Test ss = new Test(studentID);
  ss.Show();
}
  else
{
  MessageBox.Show("username or password invalid");
}

Test Form Code:

public Test(string studentID)
{
  InitializeComponent();
  int StudentID = Convert.ToInt32(studentID);
 }

NOTE: The Login stuff works: I want the same StudentID as the student who logs in to be used in the form "test". Also How can I use the "studentID" variable in the test form in my other functions when StudentID is a local variable? Also note that I am not using any classes (OOP). If there is any easier way then please let me know.

mason
  • 31,774
  • 10
  • 77
  • 121
SnapShot
  • 35
  • 5

2 Answers2

1

You can add the StudentID as an Auto-property or an field in the Test Form. So it will be --->

Public int StudentID {get; set;}

public Test(string studentID)
{
InitializeComponent();
StudentID = Convert.ToInt32(studentID);
}
Mr Tangjai
  • 153
  • 2
  • 9
-1

You can declare a public variable in class and use it to store value of student id. You can use it where you want.

Public static int student_id;
Rami Far
  • 406
  • 1
  • 10
  • 29