Search code examples
mysqlvb.netnavicat

how to use multiple results in VB.net


I need to show in 3 textboxes the result of 3 queries made in the same statement.

I have two tables called personal_sueldos and personal_haberes in mysql. They both have the same columns. I need to select the same information from both in two queries, and the 3rd query is the subtraction of descuentos from haberes.
So I have to show all 3 results in 3 textbox in a VB.NET form.

Code of the query in MySql:

SELECT SUM(importe) FROM personal_haberes WHERE persona_doc = 55880334 AND mes = 11 AND año = 2019;

SELECT SUM(importe) FROM personal_descuentos WHERE persona_doc = 55880334 AND mes = 11 AND año = 2019;

SELECT (SELECT SUM(importe) FROM personal_haberes WHERE persona_doc = 
55880334 AND mes = 11 AND año = 2019) -
(SELECT SUM(importe) FROM personal_descuentos WHERE persona_doc = 
55880334 AND mes = 11 AND año = 2019) as importeTotal

Code in VB.NET:

cnn.Open()
Dim Query As String
Query = "SELECT SUM(importe) as ImporteHaberes FROM personal_haberes WHERE persona_doc  = " & Module1.document & " AND mes = " & HR_DatePicker.Value.Month & " AND año = " & HR_DatePicker.Value.Month & ";" & _
"SELECT SUM(importe) as ImporteDescuentos FROM personal_descuentos WHERE persona_doc = " & Module1.document & " AND mes = " & HR_DatePicker.Value.Month & " AND año = " & HR_DatePicker.Value.Month & ";" & _
"SELECT (SELECT SUM(importe) FROM personal_haberes WHERE persona_doc    = " & Module1.document & " AND mes = " & HR_DatePicker.Value.Month & " AND año = " & HR_DatePicker.Value.Month & ";) -" & _
"(SELECT SUM(importe) FROM personal_descuentos WHERE persona_doc = " & Module1.document & " AND mes = " & HR_DatePicker.Value.Month & " AND año = " & HR_DatePicker.Value.Month & ";) AS ImporteTotal"
Dim cmd As New MySqlCommand(Query, cnn)
rdr = cmd.ExecuteReader
While rdr.Read
    Me.HR_txtHaberes.Text = rdr.Item("ImporteHaberes").ToString
    Me.HR_txtDescuentos.Text = rdr.Item("ImporteDescuentos").ToString
    Me.HR_txtTotal.Text = rdr.Item("ImporteTotal").ToString
End While
cnn.Close()

I have to show the result of each query in a textbox.

Now VB gives me the error "Could not find specified column in results"


Solution

  • There's a lot to clean up here. It's never okay to use string concatenation to build a query like that, even in learning code. This is also not the correct way to manage the connection object.

    A better pattern (with fixed code, including the next result issue), looks like this:

    'Multi-line string literals are now supported
    ' Note: there is NO CONCATENATION here. This is a constant.
    ' I also consolidated the SQL to avoid worrying about separate result sets.
    Dim Query As String = "
        SELECT SUM(importe) as Importe
         FROM personal_haberes
         WHERE persona_doc = @document AND mes = @mes AND año = @ano
        UNION ALL
        SELECT SUM(importe)
         FROM personal_descuentos
         WHERE persona_doc = @document AND mes = @mes AND año = @ano;"
    
    Using cnn As New MySqlConnection("connection string here"), _
          cmd As New MySqlCommand(query, cnn)
    
        'Guessing at types/lengths here.
        ' Use the ACTUAL columns types and lengths from your database
        cmd.Parameters.Add("@document", MySqlDbType.VarChar, 50).Value = Module1.document
        cmd.Parameters.Add("@mes", MySqlDbType.Int32).Value = HR_DatePicker.Value.Month
        cmd.Parameters.Add("@ano", MySqlDbType.Int32).Value = HR_DatePicker.Value.Year 'This was your error
    
        'Wait as long as possible to call .Open()
        cnn.Open()
        Using rdr As MySqlDataReader = cmd.ExecuteReader()
            rdr.Read()
            Dim Haberes As Integer = CInt(rdr("Importe"))
            rdr.Read()
            Dim Descuentos As Integer = CInt(rdr("Importe"))
    
            Me.HR_txtHaberes.Text = Haberes.ToString()
            Me.HR_txtDescuentos.Text = Descuentos.ToString()
            Me.HR_txtTotal.Text = (Haberes - Descuentos).ToString() 'No need to make the database do the subtraction work.
        End Using
    End Using ' This will take care of closing the connection.. even if an exception is thrown!
    'The old code might have leaked open connections whenever there was an exception.