Data Adapters / Datasets and Datagrid views

Hi I am new to this area of programming - so please go easy with me :-) can someone in simple terms please give me an example of how to programme at runtime and then link together, a datadapter, a dataset and then show me how to display the results in a datagrid.

To make it easier for me to follow can the example use the following criteria : a SQL database called "LiveDBF" with one table called "Stores". The stores table has three string fields (strName, strNumber, StrPhone).

Many thanks
Who is Participating?
SanclerConnect With a Mentor Commented:
Assuming that you have dragged a datagrid onto a form, all you need is seven steps.  Those can be accomplished in seven lines of code or, if you combine some of the steps, even less.  These are the steps.
1)  Make a connection string
2)  Make a connection, using (1)
3)  Make an SQL Select string
4)  Make a dataadapter using with (2) and (3)
5)  Make a blank, empty datatable
6)  Structure and fill (5) using (4)
7)  Set (5) as the .DataSource of your datagrid.

This is example code.  You would need Imports System.Data.SqlClient
 at the start of your form
   'Step 1: you can get variations from
   'for the example you give, '<yourDataBase>' would be replaced by 'LiveDDF' - no quotes
   Dim strCon As String = "Data Source=<yourDataSource>;Initial Catalog=<yourDataBase>;Integrated Security=SSPI;"
   'Step 2
   Dim Con As New SqlConnection(strCon)
   'Step 3
   'for the example you give, '<yourTable>' would be replaced by 'Stores' - no quotes
   Dim strSQL As String = "SELECT * FROM <yourTable>"
   'Step 4
   Dim da As New SQLDataAdapter(strSQL, Con)
   'Step 5
   Dim dt As New DataTable
   'Step 6
   'Step 7
   myDataGrid.DataSource = dt

That's it.  No bells or whistles: everything would be "default".  You might want to alter some of those.  You might want to do some of the declarations at the form start, to give the objects scope for all procedures within the form.

I see that, while typing the above, kaliyugkaarjun has given you an answer.  I agree with everything s/he says but still decided to post this as it is slightly simpler.  E.g., to make things work you don't actually need either a dataset or the table mappings, although there may be good reasons why - in a particular context - that more detailed approach should should be preferred.


Please check out the following Link


Private Const SELECT_STRING As String = _
    "SELECT * FROM Stores"

Private Const CONNECT_STRING As String = _
    "Data Source=Bender\NETSDK;Initial " & _
        "Catalog=Contacts;User Id=sa"
Change this line with ur connection string

' The DataSet that holds the data.
Private m_DataSet As DataSet

' Load the data.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    Dim data_adapter As SqlDataAdapter

    ' Create the SqlDataAdapter.
    data_adapter = New SqlDataAdapter(SELECT_STRING, _

    ' Map Table to Contacts.
    data_adapter.TableMappings.Add("Table", "Stores")

    ' Fill the DataSet.
    m_DataSet = New DataSet()

    ' Bind the DataGrid control to the Stores DataTable.
    dgStores.SetDataBinding(m_DataSet, "Stores")
End Sub

Also check out the following Link

Hope this  will help u  : )


swtileyAuthor Commented:
Thank you Roger,

This is exactly what I needed to get me going again, Thanks for the very clear step by step instructions.

All Courses

From novice to tech pro — start learning today.