Link to home
Start Free TrialLog in
Avatar of Ernesto
ErnestoFlag for Mexico

asked on

Get values Vb.Net

Need to read 2 values from a query in vb.net

 cn.Open()
            Dim nusa15 As String = "SELECT pagadot,nombre  FROM cheque WHERE id = '" & CustomerId & "'"
            Dim cmd15 As New OleDbCommand(nusa15, cn)
            valor = (cmd15.ExecuteScalar())
            cn.Close()

Open in new window



this one get me only pagadot that i asign to valor

how to get nombre in the same

what is the way dear experts
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

ExecuteScalar will only return a single value.

I would use a DataReader:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader

You  "could" concatenate the values inside the select to return them as a single value for ExecuteScalar but I wouldn't recommend it.  I only mention it because I'm sure someone else will.
Avatar of Ernesto

ASKER

so the way for retrive that is use multiple querys?
Something like this:
OleDbDataReader reader = cmd15.ExecuteReader();

reader.Read()



reader.getInt32(0) ' pagout
reader.getString(1) ' nombre

Open in new window

>>so the way for retrive that is use multiple querys?

No.  As jcgd posted and from the example in the link I posted, use a DataReader not ExecuteScalar.
Are you using Access or SQL Server?
Sample:
 Private Sub FineData()
        Dim mySQLDataAdapter  as SQLDataAdapter = nothing
        Dim DS as dataset = nothing
        Dim sTableName  as string = "cheque "
        Dim str as string = nothing
        cn.Open()
        Try
            str = "SELECT pagadot,nombre  "
            str &= "FROM " & sTableName & " "
            str &= "WHERE id  = CustomerId  "
         
            If cn.State <> Data.ConnectionState.Open Then cn.Open()
            mySQLDataAdapter = New SqlDataAdapter(str, myConn)            'FOR SQL Server
            mySQLDataAdapter.Fill(DS)

            PopulateSupplierListFromDS(DS)

        Catch Exp As DataException
            MsgBox("FillDataSet Procedure Error", MsgBoxStyle.Critical, "Load Data Error")
        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error in Cheque Table")
        Finally
            str = Nothing
            If cn.State <> ConnectionState.Closed Then cn.Close()
        End Try
    End Sub

IF need you can add a join if you are looking at more than one table.
The SQL posted appears to be returning a single row.  Personally, I wouldn't bother with a DataAdapter, DataSet and DataTable for one row.  Too much overhead for what is needed.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.