how to retrieve data from database using select query - vb.net

I have to retrieve data in two TextBoxes but the data should belong to tokennum that I am getting from first text box. I have a total of three TextBoxes and one button. In a database called db1 I have a table named Table1 and two fields ser as serial number, tokennum for token number and name for name of employees.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim a As Integer
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\Documents and Settings\trainee-ng-it\Desktop\Subhedar Sir\New Folder (2)\db1.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
a = Val(TextBox1.Text)
sql = "SELECT Table1.ser FROM Table1 where Table1.token num=a"
da = New OleDb.OleDbDataAdapter(sql, con)
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "db1")
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
End Sub
End Class
#dhruva sir:thanks for guidance,i corrected that but now how to proceed?how to get respective data in the correspoding textboxes?

Your sql query is wrong
sql = "SELECT Table1.ser FROM Table1 where Table1.token num=a"
It should be something like this :
sql = "SELECT Table1.ser FROM Table1 where Table1.tokennum=" & a
Although the 'Table1.token num' is wrong, there cannot be a space within a column name, I hope that was just a typo and have corrected the same in the second query.

"SELECT Table1.ser FROM Table1 where Table1.tokennum= " + a
This is a start, but your question is not very clear

Related

Search MS Access and display in textboxes VB.Net

Please assist. I wish to search the ID number from MS Access table.
If that ID number exists, I must display the firstname, surname, gender and DOB of the person that has ID = TextBox.Text, as follows:
Patient_FirstName - txbFirstName
Patient_Surname - txbSurname
Patient_Sex - txbGender
Patient_DOB -txbDOB
This data is all contained in a table called Patient.
My code is as follows:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
dbProvider = "PROVIDER= Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Siyabonga Kubeka\Documents\Academic Folder\Richfield Graduate Institute Of Technology\BSC GUIDES\BSC GUIDES\Semester 2\Programming 512 Tasks\HealthServiceSystem.accdb"
conn.ConnectionString = dbProvider & dbSource
conn.Open()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("Select * from Patient where ID like '%" & txbIdNumber.Text & "%'", conn)
da.Fill(dt)
txbFirstName.DataBindings.Add("Text", dt, "Patient.Patient_First_Name")
txbSurname.DataBindings.Add("Text", dt, "Patient.Patient_Surname")
txbDOB.DataBindings.Add("Text", dt, "Patient.Patient_DOB")
txbGender.DataBindings.Add("Text", dt, "Patient.Patient_Sex")
End Sub
I get this error but I cannot figure it out why:
System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.'
I did get is right, see code below:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
dbProvider = "PROVIDER= Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Siyabonga Kubeka\Documents\Academic Folder\Richfield Graduate Institute Of Technology\BSC GUIDES\BSC GUIDES\Semester 2\Programming 512 Tasks\HealthServiceSystem.accdb"
Dim strSql As String = "Select * from Patient where Patient_ID=#PatientID"
conn.ConnectionString = dbProvider & dbSource
conn.Open()
Using cmd As New OleDbCommand(strSql, conn)
cmd.Parameters.AddWithValue("#PatientID", txbIdNumber.Text)
Dim reader As OleDbDataReader = cmd.ExecuteReader()
While reader.Read()
txbFirstName.Text += reader("Patient_First_Name").ToString()
txbSurname.Text += reader("Patient_Surname").ToString()
txbDOB.Text += reader("Patient_DOB").ToString()
txbGender.Text += reader("Patient_Sex").ToString()
End While
End Using
End Sub

Syntax error in INSERT INTO statement generated by OleDbCommandBuilder

Why does this keep telling me
Syntax error in INSERT INTO statement
I searched for more details but it keeps telling me this.
This is the code :
Imports System.Data
Imports System.Data.OleDb
Public Class f9
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim sql As String
Private Sub f9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "Provider=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = E:\21.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM snack"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "snack")
da = New OleDb.OleDbDataAdapter(sql, con)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b1.Click
Me.Close()
x = x + (5 * 1)
If d.tc.Text = f7.b1.Text Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("snack").NewRow()
dsNewRow.Item("Date") = f1.d1.Text
dsNewRow.Item("Order") = d.tc.Text
dsNewRow.Item("Number Of Items") = b1.Text
dsNewRow.Item("Price") = " 5 "
dsNewRow.Item("Total") = x
ds.Tables("snack").Rows.Add(dsNewRow)
da.Update(ds, "snack")
con.Close()
End If
End Sub
End Class
Some of your field names are reserved words in Access SQL (Date, Order) and you also have a field name with spaces in it. The default configuration of the CommandBuilder will not produce valid SQL statements in cases like this.
To fix this issue, immediately after the line...
Dim cb As New OleDb.OleDbCommandBuilder(da)
...add the following two lines:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the command builder to enclose table and field names in square brackets ([]) so instead of generating a statement like
INSERT INTO snack (Date, Order, Number Of Items) VALUES ...
it will generate a statement like
INSERT INTO [snack] ([Date], [Order], [Number Of Items]) VALUES ...
Those square brackets are required for the SQL statement to be syntactically correct.

how to look for a field in different tables in vb.net using query

I Have a query in which i want to search in all the table for a common field using query below ismy code----
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'Dim da As OleDb.OleDbDataAdapter
Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;"
Me.con = New OleDb.OleDbConnection
Dim sqlquery As String = "SELECT * FROM **mba mca mscit** WHERE urno='" & TextBox2.Text & "';"
Dim command As New OleDb.OleDbCommand(sqlquery, con)
Dim reader As OleDb.OleDbDataReader
con.ConnectionString = dbprovider
con.Open()
reader = command.ExecuteReader()
reader.Read()
TextBox1.Text = reader(1).ToString
TextBox2.Text = reader(2).ToString
TextBox3.Text = reader(3).ToString
TextBox4.Text = reader(4).ToString
TextBox5.Text = reader(5).ToString
Catch ex As Exception
MsgBox("Not Found")
con.Close()
End Try
i want to search for urno in all the tables which have the fields in them with the same name....?
Assuming the tables mba, mca and mscit have the same columns, then
SELECT * FROM mba
WHERE urno=#urno
UNION
SELECT * FROM mca
WHERE urno=#urno
UNION
SELECT * FROM mscit
WHERE urno=#urno
and supply an SQL parameter (q.v.) for #urno

vb.net load record data in a text field

im fairly new to databases in vb.net and i have just learned how to use datagridview. im gonna show some of my code for the connection and datagridview display
Public Class Form1
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet 'holds table data
Dim da As OleDb.OleDbDataAdapter 'connection to database connectionobject
Dim sql As String
Dim inc As Integer
Dim MaxRows As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:/AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
'alternative way of connection
'Dim fldr As String
'Environment is the user profile
'fldr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "/AddressBook.mdb"
'dbSource = "Data Source = " & fldr
con.Open()
MsgBox("Database is now Open")
sql = "select * from tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Addressbook")
con.Close()
MsgBox("Database is now Closed")
MaxRows = ds.Tables("AddressBook").Rows.Count
inc = -1
MsgBox(MaxRows)
TextBox1.Text = inc
DataGridView1.DataSource = ds
DataGridView1.DataMember = "AddressBook"
End Sub
End Class
i want to display in a textfield the first name based on where is the pointer is positioned after i clicked Button1, how do i do this? thank you for the replies!
You need to get that value from the data grid itself, and then show it on the form. There are other ways, but try this (and add null checks!):
Dim row as DataRow = CType(DataGridView1.CurrentRow.DataBoundItem, DataRowView).Row
myTextBox.Text = row["firstName"].ToString();
C#
var row = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
myTextBox.Text = row["firstName"].ToString();
Alternately:
If you use a DataSource, and bind the grid to that first, then fill the DataSource with the data, you can use the .Current property to get the selected row.
Edit:
Mistake in code. It should be "DataBoundItem". Not "DataItem". From memory... Also, you need to cast to string, ctype(...,string) or call .ToString().
If you bind to a list of objects, then you won't need to call the .Row, the DataBoundItem will be the actual object type, eg Customer

Visual Basic 2008 New Search Query

I'm trying to do a search through an access database I added to a project but I get this error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll"
Additional information: No value given for one or more required parameters.
The idea was to search the database for text entered into a textbox, then display the information on that row within more text boxes.
The code dr = cmd.ExecuteReader is also highlighted as an issue when debugging. I'm using visual basic 2008, and quite new to the whole coding scene so explanations as to why the issue has occurred would be appreciated!
Imports System.Windows.Forms
Imports System.Data.OleDb
Public Class frmSearch
Public con As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Dim dbProvider As String
Dim dbSource As String
Dim BillingSystemFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Private Sub frmSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BillingdatabaseDataSet.BillingInfo' table. You can move, or remove it, as needed.
Me.BillingInfoTableAdapter.Fill(Me.BillingdatabaseDataSet.BillingInfo)
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
'Setup the provider
TheDatabase = "/billingdatabase.accdb"
BillingSystemFolder = Application.StartupPath
FullDatabasePath = BillingSystemFolder & TheDatabase
'Set the database and the location of it
dbSource = "Data Source = " & FullDatabasePath
'Set the data source
con.ConnectionString = dbProvider & dbSource
'Set the connection string
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
con.Open()
txtJobNum.Clear()
txtName.Clear()
txtSurname.Clear()
Dim str As String
str = "SELECT * FROM BillingInfo WHERE (Code = " & CodeText.Text & ")"
Dim cmd As OleDbCommand = New OleDbCommand(str, con)
dr = cmd.ExecuteReader
While dr.Read()
txtSurname.Text = dr("Surname").ToString
txtName.Text = dr("First Name").ToString
txtJobID.Text = dr("Customer ID").ToString
End While
con.Close()
End Sub
End Class
Probably the field Code is a text field. In this case when you want to search using a particular value for that field you should enclose the value between single quotes.
Something like this
str = "SELECT * FROM BillingInfo WHERE (Code = '" & CodeText.Text & "')"
However this is really a bad practice because this allows to create an Sql Injection attack or it will simply fail because your value contains a single quote.
The correct method is using a parameterized query like this
str = "SELECT * FROM BillingInfo WHERE (Code = #p1)"
Dim cmd As OleDbCommand = New OleDbCommand(str, con)
cmd.Parameters.Add("#p1", OleDbType.VarWChar).Value = CodeText.Text
dr = cmd.ExecuteReader