I have a list box called degree programme, once a selection is made in this listbox, I want the modules associated with selection to appear in my modules listbox. I'm doing this through an access database. I'll post the code below which i've pieced together from things ive read, but I dont know how right/wrong it is.
private void lstboxDegreeProgramme_SelectedIndexChanged(object sender, EventArgs e)
{
String strItem;
foreach (Object selecteditem in lstboxDegreeProgramme.SelectedItems)
{
strItem = Convert.ToString(lstboxDegreeProgramme.SelectedItem);
string connectionString =
"provider=Microsoft.ACE.Oledb.12.0;" +
"data source=C:\\Users\Documents\\StudentRegistrationDatabase.accdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbConnection.Open();
myOleDbCommand.CommandText = "SELECT * FROM Modules WHERE CourseID = '" + strItem + "''";
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
while (myOleDbDataReader.Read())
{
lstboxModules.Items.Add(myOleDbDataReader["ModuleID"].ToString() + myOleDbDataReader["ModuleName"].ToString());
myOleDbDataReader.Read();
}
I keep getting an error on the highlighted line saying Syntax error in string in query expression 'CourseID. But when i've put breaks in it runs up until that and i've been re-writing the code for the best part of today. Any help (as normal) is appreciated :D
I keep getting an error on the highlighted line saying Syntax error in string in query expression 'CourseID. But when i've put breaks in it runs up until that and i've been re-writing the code for the best part of today. Any help (as normal) is appreciated :D
Javeed M ShaikhPosted Apr 15, 2013, 5:09 PM
string[] marray =null;
strItem = marray[0]
....
....
.... your remaining code
Katie BPosted Apr 16, 2013, 4:36 PM
Javeed M ShaikhPosted Apr 16, 2013, 1:36 PM
for example:
string mvar = "I,am,Katie"; // store some text with comma seperate
string[] marray = mvar.split(','); // now we create a string array and specify the delimeter in this case it is a comma
// below is how the array is formed after the split command
marray[0] will contain "I"
marray[1] will contain "am"
marray[2] will contain "Katie"
Katie BPosted Apr 16, 2013, 1:29 PM
Thanks Kate
Katie BPosted Apr 15, 2013, 5:00 PM
The strItem is suppose to get the value "CourseID" which is displayed in the listbox above.
Once the strItem has the "CourseID", I would like it to then compare it with a value in another table in the database, and if the "CourseID" is matched with a value in the other table.
I'm Not sure how I get just the CourseID from the bit of code above.
Any help would be nice.
k
Javeed M ShaikhPosted Apr 15, 2013, 4:36 PM
what is the value you are getting in variable strItem ?
check is myOleDbDataReader.HasRows is return true or false ?
remove the " myOleDbDataReader.Read();" from inside the while loop
Katie BPosted Apr 15, 2013, 4:32 PM
It runs now, but when I double click on an item in the listbox it doesn't display anything in the next listbox.
updated code:
Javeed M ShaikhPosted Apr 15, 2013, 4:27 PM
replace this:
myOleDbCommand.CommandText = "SELECT * FROM Modules WHERE CourseID = '" + strItem + "''";
with
myOleDbCommand.CommandText = "SELECT * FROM Modules WHERE CourseID = '" + strItem + "'";