0

I populate my Venue combobox and I use a selectionchangecommitted on that combobox, I bind my Event listbox. This all works perfectly, except when I use a selectedindexchange, to change the price textbox and quantity textbox, having to bind the same data again I keep getting an error. I have been stuck on this for a day now and can't seem to figure this out. Any help would be appreciated!

 private void cboVenues_SelectionChangeCommitted(object sender, EventArgs e)
    {
        try
        {
            string sql = string.Format("SELECT EventName, TicketPrice, QtyTicketsAvailable " +
             "From Event WHERE VenueId = {0}", cboVenues.SelectedValue).ToString();

            DataTable dt = GetData(sql);

            lstEvents.DisplayMember = "EventName";
            lstEvents.ValueMember = "VenueId";
            lstEvents.DataSource = dt;

            txtPrice.Text = dt.Rows[0]["TicketPrice"].ToString();
            txtQty.Text = dt.Rows[0]["QtyTicketsAvailable"].ToString(); 
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message, GetType().ToString());
        }
    }



    private void lstEvents_SelectedIndexChanged(object sender, EventArgs e)
    {
        //int value = Convert.ToInt32(lstEvents.SelectedValue);

        string sql = string.Format("SELECT TicketPrice, QtyTicketsAvailable, EventId FROM Event WHERE EventId = {0}",
            lstEvents.SelectedValue);

        DataTable dt = GetData(sql);

        txtPrice.Text = dt.Rows[0]["TicketPrice"].ToString();
        txtQty.Text = dt.Rows[0]["QtyTicketsAvailable"].ToString();
    }
  • [Use your debugger](http://idownvotedbecau.se/nodebugging/) to look at the value of `sql`. Then [use parameters](https://stackoverflow.com/questions/7505808/) to put the value contained in `SelectedValue`, not the entire class, into your SQL. – Dour High Arch Feb 25 '19 at 18:54

1 Answers1

0

Some things to try:

Debug to figure out what the parameters are.

Run the SQL (with parameters you just found) directly against your DBMS.

Does the query return data when run directly in your DBMS?

If it does, then debug to find out what the value of dt is?

If dt has the expected values, does dt.Rows[0]["TicketPrice"].ToString(); have the expected value?

That should give you a pretty good idea hopefully :)

Andrew Meservy
  • 103
  • 2
  • 9