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.



1> I want to display the list of table names from database MS Access to vb.net.

2>Also I want to know that under the metadata of MS Access , what other things can i display in vb.net

Can any one will help me to sort out this problem....



wi As System.Security.Principal.WindowsIdentity =  _System.Security.Principal.WindowsIdentity.GetCurrent()



Dim a AsString() = HttpContext.Current.User.Identity.Name.Split("\"c)

Dim ADEntry AsNew System.DirectoryServices.DirectoryEntry _

(("WinNT://" & a(0) & "/") + a(1))

Dim Name AsString = ADEntry.Properties("FullName").Value.ToString()

this code works fine when it is running in my local machine. when i try to open the sam eapplication from remote system its giving error.

even i am getting the erron after changed the web.config file with

 <authentication mode="Windows"/>
<identity impersonate="true"/>
         <deny users="?"/><allow users="*"/>

The network path was not found.



Please help me on this isseue its urgent.

Using SQL Server Compact 3.5 SP1 under VS2008 in VB, I am trying to copy records from a table in one database to a table with identical schema in another database.  Is this the most appropriate approach? (It is not working)

    Public Sub TransferDataInSqlServerCe()

        Dim conStringImport As String = "Data Source='" & myConfig.dbImport & "'"
        Dim conStringExport As String = "Data Source='" & myConfig.dbExport & "'"

        Dim conImport As New SqlCeConnection(conStringImport)
        Dim conExport As New SqlCeConnection(conStringExport)

        Dim comImport As New SqlCeCommand()
        comImport.Connection = conImport
        comImport.CommandText = "select * from rentcomps"
        Dim Importresultset As SqlCeResultSet
        Importresultset = comImport.ExecuteResultSet(ResultSetOptions.Scrollable Or ResultSetOptions.Updatable)

        Dim comExport As New SqlCeCommand()
        comExport.Connection = conExport
        comExport.CommandText = "select * from rentcomps"
        Dim Exportresultset As SqlCeResultSet
        Exportresultset = comExport.ExecuteResultSet(ResultSetOptions.Scrollable Or ResultSetOptions.Updatable)

        For Each row In Importresultset

        DataGridView1.DataSource = Exportresultset


    End Sub

Thanks, -BGood



I have Mysql database.In that some records status is New.now,I bind database table to Listview(asp) control.

What's my problem is,the New status records(from Mysql database) text in Listview(after binding) is visible in bold format.

how to do this??

Thanks in advance.


sometimes learning asp.net feels so heavy handed compared to some of the general purpose scripting languages like php.  for examples, all i want to do is create a sql connection that pulls a row count and displays that number.  I don't need a grid, list, detail page, etc., nothing this heavy... a simply query that will display a simple bit of text.  for the life of me I can't figure out how to do it.  Using these controls, I'm not in the code behind with the query, so I wouldn't attach this to a label (or would i), and there's nothing i see in the toolbox that does what i want.  Perhaps i'm too accustomed to php but it feels like with asp.net, major things are simple, and simple things are hard.  any help would be appreciated.  i'm trying to convert my career to c#.net

I am new to programming and have just finished one of my first projects. I would like to possibly sell this application, but I need to figure out how I'm going to get the app. protected.

I plan on selling the program as a digital download and was wondering what the steps would be to issue serial licenses to the people who will purchase it.

Can anyone please direct the first few steps? It would be greatly appreciated.

Aaron C.
I am a new programming grad and am writing a vb program to access an access database.   I have a screen called Invoices that has a ComboBox that is data bound to the invoice table in the database.  My issue is when I set the SelectedValue to the binding source my other controls do not work.  When SelectedValue is not set the other controls work just fine however the first value in the ComboBox will change to the selected data and not chang back to the original value.  Any suggestions

Hello i want to create a Phone book

i made on my parent window a datagridview that connect to a database

i have also a menu i have added a button


<pre lang="x-vbnet">'my code for opening the dialog'

in the dialog i have 7 label's and 7 textbox's

-Name + textbox

-Sur name + textbox

-phone name + Textbox


Now i have a button on the bottom of the dialog


i want when i press the button it will save it to the database and also refresh the datagridview


but how do i do that




Arno (proudtobemexp)


hi all,

Can any body tell me how to display error message in asp.net when duplicate user is inserted.


Thanks in advance!



i'm newbie to asp.net mvc. i use mvc 1.0 since my host supports that.

i have a piece of code in my view like this and i want to change it in order to be able to show records in 4 columns.

What should i do?


    <% foreach (var item in Model) { %>
    <%=Html.ActionLink(item.CategoryName + "(" + item.NumberOfWebsitesInCategory + ")", "SubCategory", new { CatID = item.CategoryID }, new {style="text-decoration:none;" })%>

    <% } %>



how can i display the last record of the table in asp.net. i have button(Cparts) on status webform when you click this two events takes place

1.A pop-up window will appearon on the screen which has 4 textfield,pop-up window comming with blank fields i want to fill all the 4 fields with current/last record of the corresponding table.

2.Also tranfering the primary key value (WONumber in status) of status webform into textbox of the pop-up window(CParts.aspx) which is a hidden field.here i am inserting values into different table(Cparts table) based on WONumber which is not an unique column in this table

I want when this pop-up window opened last record of the CParts table should be populated into CParts.aspx fields. WONumber is not an unique number in CParts table



I have requirement like to display the Master details records in the page, without clicking the parent record (for getting ID to retrieve the detail record) i need to display all the child records in between the Master records while page loads.

Appreciated your help.


I have requirement like to display the Master details records in the page, without clicking the parent record (for getting ID to retrieve the detail record) i need to display all the child records in between the Master records while page loads.

Appreciated your help.


Hi All.
In my company website previously working fine.after login page the user click menu,the new page will open.after opening the user enter the some values in textbox nearly 15 values after that user click the submit button on that time the page is sending records to database after 10 seconds page cannot be displayed message appeared in internet explorer.

For information:on the submit button click
(1)15 records insert in to sql server database (insert query)
(2)after inserting select query will run
(3)after select query the from the sql server selected records will goes to mail
on that time the mail user name ,password also take from sql server database for sending mail
(4)mail sending process going
(5)the above process are running user click the submit button

The asp.net website working previously fine.now one weeks are problem
what i do ,any one please help to solve this issue.
this is very urgent for me

your co-operation is highly appreciated.




How do I get this to work on the client computer in a web application :

MessageBox.Show(OnlineRepairListBox.SelectedItem.Text, "LEN Record Selected is :", _


MessageBoxButtons.OK, MessageBoxIcon.Exclamation, _


 MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)

The message box appears on the server - I understand why, but I know there is a way to get this to work on the client.

Does anyone have any code as an example?


I want to display a moving line in an x-y axis.

For example, at begining, this line is "y = x".

After 1 sencod, it will be "y = x + 1".

Then it will be "y = x + 2", and so on.

Which function should I use, thanks very much!


Hi, i have used a single picture box and i want it to display many pictures when i run it, whereby i cant navigate through it by clicking next or back. Can anyone help me out with the codes.

I used some codes, they had no error but they weren't able to display my pictures during run time. Could anyone help me out with new codes and the procedure to do it. Please...

Hi, I want to implement Pole Display in DOS Base through VB.NET. How can I align the text which is Display in the Pole Display and also clear the text when I want to clear it. I have required three type of Port(COM, LPT, USB) for Pole display.
NOTE: Don't use of OPOS driver.

i want to display indian currency symbol in textbox

The old VB6.0 TextWidth() and TextHeight() functions told you the display device's physical measurements for the width of a string as it would be displayed using the devices current font. This made it easy to size controls around their contained strings. If you know that the string is X screen units long by Y tall, you can create a button or checkbox or other control that is just big enough to encapsulate the string (setting the width or height at (X + margin) screen units.

I now have to upgrade and old VB6.0 project to .Net and cannot find a replacement for those functions. Can anybody point me in the right direction?

Thanks...... Plundered Plato 


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure