Skip to content
Advertisement

How can I update my database with a loop by checking whether a column name corresponds with a button name?

So I’m making a seat reservation system for a school project and I’m stuck. I’ll explain exactly what I’m trying to do. I created this form:

Seat Reservation Layout

As you can see, I created a seat layout. All the seats are buttons. I also created a local SQL database with SeatID (which matches the names of the buttons exactly) and Availability (either True or False).

What I wanted to do is let the user choose seat(s) (which becomes green after clicking on it) and whenever the user clicks on the “OK” button, the seats get reserved, meaning that I want the Availability in my database corresponding with the buttonname and thus, the SeatID, to become False (meaning the seat got reserved). After running the program again, the reserved seats need to be red, on the start, meaning they can not get reserved.

So basically I managed to do it all; My database works, reserved seats are red whenever I run the program etc.

The only thing I can not get working is updating the database with reserved seats.

private void OK_Click(object sender, EventArgs e)
        {
            con.Open();

            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();
            String sql = "";

            command = new SqlCommand(sql, con);

            foreach (Button button in buttons)
            {

                if (button.BackColor == Color.Green)
                {
                    sql = "UPDATE Seats SET Availability='" + "False" + "'WHERE SeatID=" + button.Name;
                    adapter.UpdateCommand = new SqlCommand(sql, con);
                    adapter.UpdateCommand.ExecuteNonQuery();

                }
            }

            command.Dispose();
            con.Close();

        }

Above you see my code where I try to update the databse. I made the list of all buttons called “buttons” and the code above looks through all the buttons and checks whether they’re green or not (green == clicked, so ready to be reserved). That is supposed to happen when I click on the “OK” button. When I run this, I get this error message:

Error Message

So, what am I doing wrong? How am I supposed to update the database by checking whether the SeatID corresponds with the button name?

Thanks in advance.

EDIT: Here I attach my database:

Datatable

Data

Advertisement

Answer

You should use parameters in your SQL query insead.

  sql = "UPDATE Seats SET Availability= @Availabe WHERE SeatID = @SeatID" ;
  adapter.UpdateCommand.Parameters.AddWithValue("@Available", false); //I assume that your Availibility column is a BIT type. 


  //If a varchar column for exemple, you can use 
  //adapter.UpdateCommand.Parameters.AddWithValue("@Available", "False"); //But, you should use a bit column instead.


  adapter.UpdateCommand.Parameters.AddWithValue("@SeatID", button.Name);
  adapter.UpdateCommand = new SqlCommand(sql, con);
  adapter.UpdateCommand.ExecuteNonQuery();

To add parameters to your SQL Command, you can take a look at this : https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement