1
Reply

loop through items in a listbox and send to database

rulec444

rulec444

Jul 11 2004 12:12 PM
2.7k
I'm trying to loop through items added to a listbox (in windows form) and send them to a table in a database. The user selects items from a listbox on the Left and clicks a button to add them to a listbox on the right. When they click a save button, I'm trying to loop through and get each item and send it to the database, but for some reason, I can't retreive each item from the listbox. This is my code: public void SendSelectedState() { foreach(Object i in lbxStateChosen.Items) { int c = 0; lbxStateChosen.SelectedItem = c; SqlParameter[] parameters = { new SqlParameter("@EntryValue", SqlDbType.VarChar) }; parameters[0].Value = lbxStateChosen.SelectedValue; try { SqlHelper.ExecuteNonQuery(cnDEV, CommandType.StoredProcedure, "gsp_State_Insert", parameters); } catch(Exception err) { MessageBox.Show(err + "error in sending filter to db"); } c++; }//end of foreach loop }//end of SendSelectedState() I receive an error that object reference is not set to an instance of an object - because I think its not recognizing the value of the selected item. Any suggestions?

Answers (1)