Home » VB.Net

How to get records from Database and display in VB.NET

hi all

i have this problem on retrieve 3 records from database.
i did it in a way that i have a few button.
when i press on one of the button which have a value of bee then it will go into the database and search for bee.
it will return me the result on bee at the same time it will also return both top and bottom record of bee.

How do i do that in SQL or VB.NET

9 Answers Found


Answer 1

To do that you can do it in vb.net using a sql  query this might help

Answer 2

Hi h0noka,

What type of database  do you use? SQL Server or Access?
What's the struct of the table that you want to query?Please give more details about your problem.

Here is an example that you can insert, delete, update, query form an Access file.

Imports System.Data.OleDb

PublicClass Form1

Dim myDA As OleDbDataAdapter

Dim myDataSet As DataSet

PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\myDB.mdb")

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Table1", con)


myDA = New OleDbDataAdapter(cmd)

'Here one CommandBuilder object is required.

'It will automatically generate DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object

Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)

myDataSet = New DataSet()

myDA.Fill(myDataSet, "MyTable")

DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView


con = Nothing


' Save data back into database

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click






Best Regards

Yichun Feng


Answer 3


i'm using SQL Server.
I doing a program when by i got 3 images. When i click on one images, it will display  the detail of the one i click on the middle and at the same time  display the previous row and next row detail out too.


ID                Name                     Add
1                  Grace                    Holland
2                  Jack                      England
3                  Johan                    Malaysia
4                  Maureen                Thailand
5                  Jo                         China

if i click on the image 3, i will go into the database  and search  for the detail of id?3. At the same it will retrieve  the id?2 and id?4 detail together with id?3 detail?and display it out. Which means if i click on image 4 then it will retrieve the id 3, 4 and 5 details to be display out. When if i click on images 1, it will retrieve id 5, 1 and 2 details to be display out.

What do i do that?


Answer 4

Hi h0noka,

If you are using SQL Server, you just need to change the conncetiong string.
con = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True;")
If you want to query it by certain id, you can do like this:
 'set the id value as you want 
        Dim id As Integer
        da.SelectCommand = New SqlCommand("SELECT * from Persons where id = " & id, conn)

        myDataSet = New DataSet()
To get the result:
If you have any problem  about data access, you can just use search  tools and you'll find many answers.

Best Regards

Yichun Feng


Answer 5

but how do i get 3 result  back at the same time?

Answer 6

Hi h0noka,

What do you mean getting 3 result  back at the same time?
da.SelectCommand = New SqlCommand("SELECT * from Persons where id = " & id, conn)

This line can get the "name" and add by "id".
Use da.fill(ds), you can get the name and add by these:

Here is an article about ADO.NET: http://msdn.microsoft.com/en-us/library/e80y5yhx(VS.71).aspx . You can look at its sample code.
Another related article: http://www.codeguru.com/vb/gen/vb_database/adonet/article.php/c15033

Best Regards

Yichun Feng


Answer 7

because normally retrieve  data from the database  is getting back the result  you want right?

if you say SELECT * from Persons where id = " & id when the id is 2, how do i retrieve details for my id 1 and id 3 at the same time  when retrieve id 2 row detail?

because if you query in database SELECT * from Persons where id = " & 2, it will only get back the details of id 2. how can i make it when i click on id 2, it will return  me back id 2 details as well as id 1 and id 3 details?

Answer 8

Hi h0noka,

Set id as a parameter, change it every time  you change the id.
For example,

1)Sub getRecord(byval id as integer)
'put the code that you use to retrieve  the record

'use the myDataset.Tables(0).Rows(0)("Add") just in this sub
End Sub

2)If you want to use the record outside the sub.

Function getRecord(byval id as integer) as list(Of String)
'put the code that you use to retrieve the record
Dim l as New list(Of String)

return l
End Function

You can use it like this:
Dim l as New list(Of String)
l =  getRecord(1)
l(0) is the value of "Name" 
l(1) is the value of "Add"

Best Regards

Yichun Feng


Answer 9


This code is amazing and has worked for me but I am using Textboxes in my project to save data into Access database. I tried to replace DataGridView to TextBox1 but it never gave me the option to select "Datasource" after putting a dot. So, I was just wondering if you can help me out in saving, editing and deleting the records  from the database.

One more request if you can help me out. Can you also tell me the code for how to display  records from the access database? Actually, I did it by some other way and it will conflict with the code that I will get from you.

Please help.

Thanks in advance.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure