简体   繁体   中英

Getting last row of a datatable in vb.net

i simply want to get data from database where a condition is met in a particular column and at the same time get the last value of a particular column(which is a member of the set of database row that met the condition). i have this code

    connection.Open()
    command = New MySqlCommand("SELECT * FROM test.solemn WHERE pump = '" + PumpComboBox.SelectedValue.ToString + "'", connection)
    da.SelectCommand = command
    da.Fill(dt)


    Dim omr = dt.Rows(dt.Rows.Count - 1)("meter_reading")

    With Me.TextBox3
        .Text = omr
    End With
    connection.Close()

the code above is meant to get all columns from a database table where the column pump is equal to the value selected by the user in a combobox"PumpComboBox" then fill the data in a datatable then this line of code

 Dim omr = dt.Rows(dt.Rows.Count - 1)("meter_reading")

is meant to find the last value on the column Meter_reading but i get an exception "There is no row at position -1" even user hasn't made a selection at PumpCombobox though .selectedIndex of PumpCombobox is set to -1 . the code is in the pumpcombobox_change event. Thanks

Instead of doing the work in the program, it would be better to get the database to do the data selection.

LarsTech covered other important points in a comment : you should use SQL parameters any time you are passing data to a database query - it makes things work smoothly and reduces strange problems. And you should handle the case where there isn't a valid selected value.

Note that if you do not specify an order then the database is free to return result rows in any order, and that order might not even be the same from exactly the same query executed twice, so you could get what appears to be the wrong result. I have assumed that there is a column named "reading_date" and that you want the latest reading - adjust as required.

If PumpComboBox.SelectedIndex >= 0 Then
    Dim pump = CStr(PumpComboBox.SelectedValue)
    Dim result As Object = Nothing
    Using cmd As New MySqlCommand("SELECT meter_reading FROM test.solemn WHERE pump = @pump ORDER BY reading_date DESC LIMIT 1", connection)
        cmd.Parameters.Add(New MySqlParameter With {.ParameterName = "@pump", .DbType = DbType.String, .Value = pump})
        connection.Open()
        result = cmd.ExecuteScalar()
        connection.Close()
    End Using

    If result IsNot Nothing Then
        TextBox3.Text = CStr(result)
    Else
        ' there are no readings for the selected pump
    End If

End If

(You may need to change the .DbType = DbType.String - I don't have MySQL installed to check.)


If it has problems with the SelectedIndexChanged event firing when you don't want it to, then you can remove the Handles clause from the event handler:

Private Sub PumpComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
    ' code here...
End Sub

... and add the handler programmatically after the ComboBox is set up:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' other code....
    SetupPumpComboBox()
    AddHandler PumpComboBox.SelectedIndexChanged, AddressOf PumpComboBox_SelectedIndexChanged
    ' other code....
End Sub

It is also possible to use the ComboBox.SelectionChangeCommitted Event , although the remarks for that say "However, depending on how the ComboBox is configured, and how the user changes the selected item, the SelectionChangeCommitted event may not be raised."

Not really sure here but will have a go. As LarsTech has explained you have no rows hench why you are getting the error. I may be completely off the mark here but as we cannot see all your code, have you tried casing the Page load event with the following

Protected Sub Page_Load
   If Not Page.IsPostBack Then
     ' Do something here
   End If
End Sub

Basically it could be that when the page is postingback it could be firing your code, which you only want it to fire when you select your combo box or a button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM