0

Here is a code :

Private Sub ComboBoxQC1L1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxQC1L1.SelectedIndexChanged

        Dim conn As New SqlConnection
        If conn.State = ConnectionState.Closed Then
            conn.ConnectionString = ("Data Source=.\SQLEXPRESS;Initial Catalog=OST;Integrated Security=True")

        End If

        Try
            conn.Open()
            Dim sqlquery As String = "SELECT * FROM liste_unités where nom_unité = " & ComboBoxC1L1.Text & ""

            Dim data As SqlDataReader
            Dim adapter As New SqlDataAdapter
            Dim command As SqlCommand = New SqlCommand(sqlquery, conn)

            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader()
            While data.Read

                If ComboBoxQC1L1.Text = "ordinaire" Then
                    LabelPointsC1L1.Text = XXX
                ElseIf ComboBoxQC1L1.Text = "élite" Then
                    LabelPointsC1L1.Text = XXX
                ElseIf ComboBoxQC1L1.Text = "médiocre" Then
                    LabelPointsC1L1.Text = XXX
                End If

            End While
        Catch ex As Exception

        End Try

    End Sub

Here is the app overview

Here is the SQL table overview

Into the ComboBoxC1L1, all the "nom_unité" are populated and the LabelC1L1 is changing with the corresponding "cout_unité"

Ex : When ComboBoxC1L1.text = "Arbalétrier", LabelC1L1.text = "7". If the ComboBoxC1L1.SelectedIndex is changing, the LabelC1L1.text is changing to.

Now introducing the ComboBoxQC1L1 with 3 possible values : "ordinaire", "médiocre" and "élite".

I want the LabelPointsC1L1.text to adapt to the correct values (see table overview) while the ComboBoxQC1L1 is changing.

Ex : When ComboBoxC1L1.text = "Arbalétrier", LabelC1L1.text = "7". If ComboBoxQC1L1.text is changing from "ordinaire" to "élite", LabelC1L1.text should become "9". If ComboBoxQC1L1.text is changing from "ordinaire" to "médiocre", LabelC1L1.text should become "5". At last, if ComboBoxQC1L1.text is changing to "ordinaire", LabelC1L1.text should become "7" like initially.

I'm trying to figure by what code should I replace the "XXX" in the code to do this ?

Many thanks :-)

Coucouyou
  • 19
  • 5
  • Few things to consider off the bat. 1) Wrap you connection/command handelling in a "Using" block which will manage that for you. 2) use SQL Parameters rather than string concatinations when building SQL commands. 3) Read your query results into a in-memory collection, then bind that in-memory collection to your Combo (Read up on TableAdapters) which makes dealing with the label much simpler – Hursey Jan 09 '22 at 21:40
  • Thanks for your time Hursey. Would you have some examples of 2 and 3 ? :) – Coucouyou Jan 09 '22 at 21:56
  • Table adapters - https://learn.microsoft.com/en-us/visualstudio/data-tools/fill-datasets-by-using-tableadapters?view=vs-2022. SQL Parameters https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparameter?view=dotnet-plat-ext-6.0 – Hursey Jan 09 '22 at 22:05
  • Ok well, thanks for this. Would you be kind and help to figure out the VB syntax ? :D – Coucouyou Jan 10 '22 at 20:40
  • Why not have a go yourself? When you run into issues ask new questions specific to the issue you encounter. Along with the the two links already supplied there are literally thousands of examples on line of this sort of thing, many on this very site with very clear how-to. Sorry if this sounds rude but nobody is here to do your work for you – Hursey Jan 10 '22 at 20:44
  • I understand. Thanks anyway – Coucouyou Jan 10 '22 at 20:46

0 Answers0