edgarmtze
edgarmtze

Reputation: 25058

Get result of MS Access query into textbox in VB.NET

I have a table in MS Access which has a colum named NameC (using ODBC to connect to MS Access)

I want the result of the following query to be saved in a txtField

Dim query = "SELECT NameC FROM Table WHERE ClientID = " & Integer.Parse(clientID) 

How to do that in VB.NET?

I have a txtNameC.Text field

I currently was reviewing some sample code and they do:

  Dim _consultationTable As DataTable
  Public Sub Load()
    Dim query = "SELECT * FROM Table WHERE ClientID = " & Integer.Parse(clientID)
    Me._consultationTable = DatabaseFunctions.GetDataTable(query)
    dvgInfo.Rows.Clear()
    For Each  dtRow In Me._consultationTable.Rows
        dvgInfo.Rows.Add()
        dvgInfo.Rows.Add(dvgInfo.RowCount-1).Cells("ColClientID").Value = dtRow("ClientId").ToString()
   Next

but I do not want to fill a table I just want to get the result of a query into a text box How can I do this?

I want to do something like this but just return a value and save it into a textbox

Protected Sub BindData()  
   strSQL = "SELECT * FROM customer"  

   Dim dtReader As OdbcDataReader  
   objCmd = New OdbcCommand(strSQL, objConn)  
   dtReader = objCmd.ExecuteReader()  

   '*** BindData to GridView ***'  
   myGridView.DataSource = dtReader  
   myGridView.DataBind()  

   dtReader.Close()  
   dtReader = Nothing  
End Sub  


Protected Sub BindData()  
   strSQL = "SELECT SpecificValue FROM customer where x = y..."  

   Dim dtReader As OdbcDataReader  
   objCmd = New OdbcCommand(strSQL, objConn)  
   dtReader = objCmd.ExecuteReader()  

   '*** BindData to GridView ***'  
   myGridView.DataSource = dtReader  
   myGridView.DataBind()  

   dtReader.Close()  
   dtReader = Nothing  
End Sub  

Upvotes: 1

Views: 10136

Answers (1)

Ravindra Bagale
Ravindra Bagale

Reputation: 17675

use DataReader.populate ur data from database in the datareader & from the the datareader u can use perticular values.
i don't know your code,that's why i m giving one simple example.
here is one example.

imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connetionString As String
        Dim oledbCnn As OleDbConnection
        Dim oledbCmd As OleDbCommand
        Dim sql As String

        connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;"
        sql = "Your SQL Statement Here like Select * from product"

        oledbCnn = New OleDbConnection(connetionString)
        Try
            oledbCnn.Open()
            oledbCmd = New OleDbCommand(sql, oledbCnn)
            Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            While oledbReader.Read
                MsgBox(oledbReader.Item(0) & "  -  " & oledbReader.Item(1) & "  -  " & oledbReader.Item(2))
            End While
            oledbReader.Close()
            oledbCmd.Dispose()
            oledbCnn.Close()
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
    End Sub
End Class

Upvotes: 2

Related Questions