Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data Adapters / Datasets and Datagrid views

Posted on 2006-07-04
4
Medium Priority
?
224 Views
Last Modified: 2012-06-21
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
...Steve
0
Comment
Question by:swtiley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17040696
Hi

Please check out the following Link

http://www.vb-helper.com/howto_net_datagrid.html

Or

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, _
        CONNECT_STRING)

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

    ' Fill the DataSet.
    m_DataSet = New DataSet()
    data_adapter.Fill(m_DataSet)

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

Expert Comment

by:kaliyugkaarjun
ID: 17040755
Hi

Also check out the following Link

http://www.codeproject.com/aspnet/datagriddemo.asp

Hope this  will help u  : )

Cheers!!!

0
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 17040849
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 http://www.connectionstrings.com/
   '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
   da.Fill(dt)
   '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.

Roger
0
 
LVL 1

Author Comment

by:swtiley
ID: 17043898
Thank you Roger,

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

Cheers
...Steve
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question