Buwaneka Sumanasekara Buwaneka Sumanasekara - 6 months ago 19
MySQL Question

Retrieve DataRow after get sql

I`m new in VB.net and i need to create common function to retrieve data row. I tried this way. But this is not successful.

Function GetRecord(ByVal sql As String) As DataRow
Dim conn As MySqlConnection = connect()
Dim objDataSet As New DataSet
Dim dt As DataTable
Dim dr As DataRow
Try
Dim cmd As New MySqlCommand()
conn.Open()
cmd.Connection = conn
cmd.CommandText = sql
Dim da As New MySqlDataAdapter
da.SelectCommand = cmd
Dim ds As New DataSet
da.Fill(ds, "T")
dt = ds.Tables("T")
dr = dt.Rows.Item(0)
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dr
End Function


remark: Connect() function returns mysql connection object

Answer

Not exactly sure what are you trying to do but change your code to be like

    da.Fill(dt, "T")
    Catch e As Exception
        Throw e
    Finally
        conn.Close()
    End Try
    GetRecord = dt.Rows.Item(0)