i want to change the backcolor for buttons depends on information from database, i have form has multiple buttons as the picture bellow.
I have a database table for shops (stores),There is a column in this table that contains the shop number and a column for its status : (if rented or empty)
in the form shape Each shop has a button and this button named as the number of the store number in the database How can I do for loop to Retrieve store data from the database and changes the color of the button depends on the data from the database if it is empty or rented.
this is what i tried but i'm getting this error :
Object reference not set to an instance of an object. ( on change color code ) -> btn.BackColor = Color.LightPink
this is my code :
Dim pagingAdapter As SqlDataAdapter
Dim pagingDS As DataSet
Dim dt As New DataTable
Dim sqlstring As String = "Select * FROM shops where location='G2'"
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
pagingAdapter = New SqlDataAdapter(sqlstring, SQL.conn)
pagingDS = New DataSet()
pagingAdapter.Fill(pagingDS, "shops")
dt = pagingDS.Tables("shops")
MsgBox(dt.Rows.Count)
For Each row As DataRow In dt.Rows
Dim id As Integer = row("shop_id")
Dim state As String = row("status")
Dim btn As Button = TryCast(Me.Controls("Button" & id), Button)
If state = "rented" Then
btn.BackColor = Color.LightPink
ElseIf state = "empty" Then
btn.BackColor = Color.LightGreen
End If
Next
SQL.conn.Close()