MariaElisabeta
Programmer
Hello everybody,
I'm new on this forum and I would really appreciate some help with my project.I bonded my database table "Courses" to a combobox. So far so good,but when i try to register a student and i click the "Register" button after i selected an item from the combobox,the program stops working and shows an error saying "ArgumentOutOfRangeException".I have tried different possibilities,but nothing is working.Maybe someone can help me figure it out.This is the code for the combobox:
private void FillDropDownList()
{
DataRow dr;
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAUniversityCollegeAttendanceApp\VIAUniversityCollegeAttendanceApp\DatabaseAttendanceStudents.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Courses ", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dr = dt.NewRow();
dr.ItemArray = new object[] { 0, "--Select Course--" };
dt.Rows.InsertAt(dr, 0);
comboBoxCourses.ValueMember = "courseID";
comboBoxCourses.DisplayMember = "coursename";
comboBoxCourses.DataSource = dt;
conn.Close();
}
The method is called after InitializeComponent() in StartUp(){}.
I'm new on this forum and I would really appreciate some help with my project.I bonded my database table "Courses" to a combobox. So far so good,but when i try to register a student and i click the "Register" button after i selected an item from the combobox,the program stops working and shows an error saying "ArgumentOutOfRangeException".I have tried different possibilities,but nothing is working.Maybe someone can help me figure it out.This is the code for the combobox:
private void FillDropDownList()
{
DataRow dr;
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAUniversityCollegeAttendanceApp\VIAUniversityCollegeAttendanceApp\DatabaseAttendanceStudents.mdf;Integrated Security=True;Connect Timeout=30");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Courses ", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dr = dt.NewRow();
dr.ItemArray = new object[] { 0, "--Select Course--" };
dt.Rows.InsertAt(dr, 0);
comboBoxCourses.ValueMember = "courseID";
comboBoxCourses.DisplayMember = "coursename";
comboBoxCourses.DataSource = dt;
conn.Close();
}
The method is called after InitializeComponent() in StartUp(){}.