0

how could I select / check items in checkedListBox which exist in database ?

I INSERT value to Table (Tickets): (PK TicketID, FK ShowID, FK PriceID, Seat )

INSERT is ok, now I want to check this items which aleady exist in DB.

I have no idea to solve this problem.

EDIT: I want to select Items which exist in database, so it's not solve my problem I think.

adamo ski
  • 17
  • 7
  • Execute a select query using ExecuteScalar() and if you query returns null it means your item does not exist in database. – Muhammad Saqlain Jan 21 '17 at 13:29
  • Possible duplicate of [Check if a record exists in the database](http://stackoverflow.com/questions/21302244/check-if-a-record-exists-in-the-database) – Eugene Podskal Jan 21 '17 at 13:30
  • Could you show me an example? – adamo ski Jan 21 '17 at 13:31
  • You want to select multiple checkboxes in the checkbox list? Is it the use case of showing which tickets are booked or blocked for booking? – Chetan Jan 21 '17 at 13:49
  • `SELECT * FROM Table`. combined with the suggestion from @MuhammadSaqlain – ColinM Jan 21 '17 at 14:40
  • Yes, something like that. Example: Adds to the table: (ShowID FK, FK PriceID, Seat) (1, 10, A3). If i run program this item is check in checkedListBox. – adamo ski Jan 21 '17 at 14:43
  • You need to execute a select query and get result in a datatable. After that parse that Datatable to get your specific rows. – Muhammad Saqlain Jan 21 '17 at 14:45
  • As suggested by others, you need to create command with select query and use ExecuteScalar() method on command to determine if row exist or not. Are you going to select data based on all the column value comparison? But what is you logic to determine which item to select? You have checkboxlist bound to a datasource? What is the relation between the datasource of checkboxlist and the data you are querying ? – Chetan Jan 21 '17 at 14:57
  • Putting a little bit more details about the business use case which you are implementing would be helpful. – Chetan Jan 21 '17 at 15:02

1 Answers1

1

You can write code something like below.

using(var connection = new SqlConnection(<<connectionString>>))
{
    var sqlQuery = <<Select Query>>;
    connection.Open();
    using(var command = new SqlCommand(sqlQuery, connection))
    {
        var count = (int) comand.ExecuteScalar();

        if(count > 0)
        {
            //Logic of selecting checkbox in the checkbox list.
        }
    }
}
Chetan
  • 6,711
  • 3
  • 22
  • 32