Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"ArgumentOutOfRangeException" for binding combobox with datatable.

Status
Not open for further replies.

MariaElisabeta

Programmer
Feb 20, 2018
1
DK
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(){}.

 
It sounds like you're saying that the code above works fine (loading the combobox), but then breaks when you click the Register button. What's the code for that button's click event?

Katie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top