10 Replies - 681 Views - Last Post: 10 March 2015 - 05:51 AM Rate Topic: -----

#1 dme12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 15

how to change button back color if column in database is 1.

Posted 10 March 2015 - 03:56 AM

Private Sub Load_Record()
Dim sqlstring As String = "SELECT * FROM Seats_tbl WHERE FlightID = " & FlightID
lblFlightID.Text = "Flight ID: " & FlightID


con()
Dim cmd As New OleDbCommand(sqlstring, connect)
connect.Open()

Dim dr As OleDbDataReader
Dim value As Byte = 0
dr = cmd.ExecuteReader()

If dr.HasRows Then
With Me
While dr.Read
'--i have no idea what to input here. Am having multiple buttons as seats.
End While
End With
End If
End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: how to change button back color if column in database is 1.

#2 djjeavons  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 04:13 AM

Hi

To read a value from the DataReader use dr("ColumnName").

How you will update the buttons depends on how they are added to the form and what mechanism you have of finding them and associating them to a record in your database. I assume you would have some form of SeatID that corresponds to all seats on a specific Flight?
Was This Post Helpful? 0
  • +
  • -

#3 dme12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 15

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 04:23 AM

I have dr("A1") which corresponds to seats. I would like to make the button 'A1' color red if the value in column A1 in the database is 1 which means the seat is occupied.
Was This Post Helpful? 0
  • +
  • -

#4 djjeavons  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 04:28 AM

Something like:

If dr("A1") = 1 Then buttonA1.BackColor = Color.Red



The above would do what you want, but I am curious to the design. If you have say 50 seats on a flight, are you going to have 50 statements like the above?
Was This Post Helpful? 0
  • +
  • -

#5 dme12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 15

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 04:52 AM

that's what i was trying to accomplish. It seems that 50 statements like the above is not an efficient way to do it.
Was This Post Helpful? 0
  • +
  • -

#6 djjeavons  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 04:55 AM

View Postdme12, on 10 March 2015 - 04:52 AM, said:

It seems that 50 statements like the above is not an efficient way to do it.

I agree.

How are you currently laying out this program? Have you simply added lots of buttons to the form or are you creating them dynamically? Also, what data do you get back from your database. Maybe armed with this information, we can suggest a more suitable approach to implementing this.
Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 592
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 05:04 AM

is that column just 1's and 0's?

if so you can do a case statement


make a list with the buttons and pass the list, cycle the list and assign the button
foreach (int b from buttons)
case 1
    do something 
case 0 
    do something

Was This Post Helpful? 0
  • +
  • -

#8 dme12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 15

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 05:05 AM

View Postdjjeavons, on 10 March 2015 - 04:55 AM, said:

View Postdme12, on 10 March 2015 - 04:52 AM, said:

It seems that 50 statements like the above is not an efficient way to do it.

I agree.

How are you currently laying out this program? Have you simply added lots of buttons to the form or are you creating them dynamically? Also, what data do you get back from your database. Maybe armed with this information, we can suggest a more suitable approach to implementing this.



i simply added my buttons manually in the designer. If the value in database is 1 my button would be color red, if value is 0 then it remains as white.
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 05:09 AM

What does your table looks like? I would expect to see a column that holds seat# (ie, A1) and a isOccupied column to show occupancy of said seat.

Then you could read these two columns and put them into either an object that you create or a dictionary of (key,value) where key would be your seat (unique) and value would be your bit (1 or 0)

Then you could use a for each statement to color all the seats in a few lines of code.
Was This Post Helpful? 0
  • +
  • -

#10 dme12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-March 15

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 05:41 AM

View PostCharlieMay, on 10 March 2015 - 05:09 AM, said:

What does your table looks like? I would expect to see a column that holds seat# (ie, A1) and a isOccupied column to show occupancy of said seat.

Then you could read these two columns and put them into either an object that you create or a dictionary of (key,value) where key would be your seat (unique) and value would be your bit (1 or 0)

Then you could use a for each statement to color all the seats in a few lines of code.


Just 1 row. I have set up my column name as the seat#.
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: how to change button back color if column in database is 1.

Posted 10 March 2015 - 05:51 AM

That's really kind of backwards. You want your data to be dynamic. What would happen if the place expanded and added another 50 seats? You would have to change your table to add 50 more columns. Where if you were to create a table and then fix all your sql statement to ensure those new columns were included.
[ID]  [SeatNum] [isOccupied]
  1     A1          0
  2     A2          1


this would allow you to add more seats without changing the table

I'm assuming you are never going to have more than one row and that row is altered via an UPDATE statement each time.

If that's the case, why not just use a delimited text file to maintain occupancy?

This post has been edited by CharlieMay: 10 March 2015 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1