PDA

Click to See Complete Forum and Search --> : ListBox Posting Problem


Phenglai
Jan 6th, 2005, 01:37 PM
I have looked all over and I can not figure this out. I am setting up a form that will allow someone to update a product group category in the database. I want them highlight the item in the listbox and then press a remove button. I can not figure out why this will not work. I have posted the code of the sub below. The code runs fine as long as I dont reference the listbox.

Any help would be appreciated.


//We need to mark the selected item as inactive
string cmdStr;
string connStr;

cmdStr = "UPDATE productGroup " +
"SET active = 0 " +
"WHERE Name = '" + lstMain.SelectedItem.Text + "'";

connStr = System.Configuration.ConfigurationSettings.AppSettings["connSql"];

// Create connection object
SqlConnection conn = new SqlConnection(connStr);
// Create command object
SqlCommand cmd = new SqlCommand(cmdStr, conn);

try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch ( Exception ex )
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}

JemimaChadwick
Jan 7th, 2005, 02:15 AM
Are you getting some error now?

Otherwise it could be no rows matched the selection. To solve this, try trimming the list box's selected item value.

Thanks,
Jemima.

Phenglai
Jan 7th, 2005, 06:55 AM
If you remove the listbox reference from the sql statement and add anything it works. Its when you reference the listbox does it fail. The sql statement is not failing.

I get NullException Object not set error on the listbox. Its like, at the point the sub runs, the listbox has been released.