vallete7
vallete7

Reputation: 123

Operator '&' is not defined for types 'String' and 'Control'

i'm building a VB.Net app to check in real time the state of machines in a production area. I want to display in a monitor the area layout and if the state of the machine is 1 put it with green colour, if it's 2 with red, and if it's something else put orange. I have the following code but it's not working because it says that the operator & is not defined for types Control, wich i use to declare my array of labels. Can someone tell me if i'm doing anything wrong? (I'm a begginer in VB.Net)

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
                Dim labels() As Control = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10, Label11, Label12, Label13, Label14, Label15, Label16, Label17, Label18, Label19, Label20, Label21, Label22,
                    Label23, Label24, Label25, Label26, Label27, Label28, Label29, Label30, Label31, Label32, Label33, Label34, Label35, Label36, Label37, Label38, Label39, Label40, Label41, Label42, Label43, Label44, Label45, Label46,
                    Label47, Label48, Label49, Label50, Label51, Label52, Label53, Label54, Label55, Label56, Label57, Label58, Label59, Label60, Label61, Label62, Label63, Label64, Label65, Label66, Label67, Label68, Label69, Label70,
                    Label71, Label72}
                Dim estado As Integer

                Try
                    con.Open()
                    For i = 0 To 71
                        Console.WriteLine(labels(i))
                        Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i) & ""
                        Dim myCommand As New MySqlCommand()
                        myCommand.Connection = con
                        myCommand.CommandText = sqlquery
                        Dim objReader As MySqlDataReader = myCommand.ExecuteReader
                        If objReader.Read = True Then
                            estado = objReader("IDEstado")

                            If estado = 1 Then
                                labels(i).BackColor = System.Drawing.Color.Green
                            ElseIf estado = 2 Then
                                labels(i).BackColor = System.Drawing.Color.Red
                            Else
                                labels(i).BackColor = System.Drawing.Color.DarkOrange
                            End If
                        End If
                        objReader.Close()
                    Next
                    con.Close()
                Finally
                End Try

            End Sub

Upvotes: 2

Views: 675

Answers (2)

bolt19
bolt19

Reputation: 2063

You are trying to join a Label control with a string in your sqlquery, you need to get the text from the label, see below;

Change this:

Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i) & ""

to this:

Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i).Text & ""

Upvotes: 1

Rahul
Rahul

Reputation: 77934

You need to use the Text property of the control while concatenating it like below else per your posted code you are actually trying to concatenate string with a control and so the error.

Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i).Text & ""

Upvotes: 1

Related Questions