Get values Vb.Net

Ernesto
Ernesto used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
>>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.
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial