Question hard questions

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
hi
i have two hard questions
1-i have 40 labels on a form i want when i click any label it turned to another color.
2-i want when the form load it searsh in adatabase table if any label from the 40 label values in the database it turned to another colour
can any one help me in detail ?
 
1. Select all the Labels in the designer, open the Properties window, click the Events window and then double-click the Click event to generate a single event handler for all of them. In the event handler, use the 'sender' parameter to get access to the Label that was clicked. You can then change its BackColor as desired.

2. How much do you know about .NET data access already?
 
1-how can i use the sender parameter you mentioned
2-i think that my second question is clear i can do it with one label but with the 40 labels i cannot
 
1. The 'sender' is the Label that was clicked.

2. If your second question was clear then I would have answered it. You now tell us that you can do it with one Label so you obviously know the basic principles of ADO.NET. We didn't know that from your first post, so we now know that we don't have to cover that. All we now have to cover is the difference between doing it with one condition in your WHERE clause and multiple. That's all you have to do: add multiple conditions to your WHERE clause. Presumably you can compare a column value to a single Label value. You just have to add more such conditions to your WHERE clause and put OR operators between them. You should also make sure that you use parameters to insert all the values. For an example of how to do that, follow the Blog link in my signature and check out my post on ADO.NET Parameters.
 
hi my dear
how can use "or" in where condition with 40 labels its to much and opposite to programming principles i need a simple way and i trust you just think again

DS = New DataSet
        Dim cmd As String
        cmd = ("select * from item where name = " & label1.Text & " ")
        DS.Clear()
    
        Try



            CON.Open()
        Catch ex As Exception
            MessageBox.Show("connection error")
            Exit Sub

        End Try
        Dim DataAdapter1 As New OleDbDataAdapter(cmd, CON)
        DataAdapter1.Fill(DS, "item")
        CON.Close()
        If Me.BindingContext(DS, "item").Count <> 0 Then

         label1.backcolor=green
            Exit Sub

        End If



thats my code for one label i need it for 40 label if any of them in the data base the label color must change to green in the formload
 
If you're testing different columns then you need to use multiple '=' conditions with OR between them. If you're testing the same column each time then you can use one IN condition with the list of values in parentheses.
 
how can i make a 40 query iam sure that there is a programing solution ,its not easy to use 40 query

How do you take 40 steps? You take 1 step 40 times. If you can execute a single query then you can execute 40 queries because it's just the same thing 40 times. In fact, it's easier than that because all 40 commands can use the same connection.
 
I generally avoid posting code when people are not really willing to make much effort on their own behalf. You already have all the information you need yet we are yet to see any effort to make use of it. To prevent this dragging on I will post a code solution, but I for one would expect that, in future, an effort be made to use information when it's provided.
Using connection As New SqlConnection("connection string here"),
      command As New SqlCommand("SELECT COUNT(*) FROM MyTable WHERE MyColumn = @MyColumn", connection)
    Dim prm = command.Parameters.Add("@MyColumn", SqlDbType.VarChar)

    connection.Open()

    For i = 1 To 40
        Dim lbl = DirectCast(Controls("Label" & i), Label)

        prm.Value = lbl.Text

        If CInt(command.ExecuteScalar()) > 0 Then
            lbl.BackColor = Color.Red
        End If
    Next
End Using
As you can see, there is one connection and 40 queries are executed, with a parameter used to insert the appropriate value each time. The only thing in there that has not been mentioned is the use of COUNT and ExecuteScalar but it could still be done with that. You could just as easily use a DataAdapter to populate a DataTable each time, which you obviously already know how to do, or call ExecuteReader on a command each time. The end result would be the same but it would be less efficient, given that you don't actually need the data but just to know whether it exists, so COUNT and ExecuteScalar are the best choice.
 
You could, of course retrieve one value of e.g. 40 characters:
VB.NET:
'obviously, get this value from db rather than writing it in your code
Dim str as String = "00100000000000000000000000000000000000000001"
For i as Integer = 0 to str.Length - 1
  If str.ChatAt(i) = "1"c Then
    labelsOnMyFormArray(i).BackCOlor = Color.Red
  End If
Next i
 
Back
Top