Exforsys

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor

Ads


Home arrow Technical Training arrow VB.NET 2005

Working with DataSets

Page 1 of 2
Author : Exforsys Inc.     Published on: 14th Jul 2005    |   Last Updated on: 15th Mar 2011

Access and Manipulate Data - Using DataSets

In this tutorial you will learn about Using DataSets, Populating a DataSet From a Database, Moving Around in DataSets and Retrieving Data, Using Strongly Typed DataSets, DataSets With Multiple Tables.

Ads

Populating a DataSet from a Database

As already discussed DataSets do not contain any data when they are created. The user must fill the data in to the DataSet separately. We have already seen that there are several methods of filling a DataSet with data. DataSets can be created using the Visual Studio Design in which case TableAdapters are also created.

Filling a DataSet using a TableAdapter

  1. Create a new project in Visual Basic IDE. 
  2. On the Database Explorer click the icon for creating new data connection and 
  3. Choose the SQL Server file.
  4. Establish the connection and you should be seeing the database objects on the window.
  5. In the solution Explorer click on the project name and
  6. Choose add an item option.
  7. In the dialog box that opens choose DataSet item and
  8. Name it as ds and click ok.
  9. You will see the DataSet item added to the solution and also
  10. A blank screen will be seen.
  11. From the Database explorer drag and drop the table ProductCategory.
  12. Choose ‘not’ in the message box that asks your permission to add the datafile as a project data.
  13. Now right click on the Form1 and
  14. Choose the option to see the code window.
  15. Type the following codes to fill the DataSet

Sample Code
  1. Dim ProductCategoryTableAdapter As New dsTableAdapters.ProductCategoryTableAdapter()
  2. Dim ProductCategoryDataSet As New ds ProductCategoryTableAdapter.Fill(ProductCategoryDataSet.ProductCategory)
Copyright exforsys.com



You can also populate a DataSet using a SqlDataAdapter or an OleDbDataAdapter. The method of doing this is same in both the cases. We shall now see how a DataSet is filled by using a SqlDataAdapter. You have to

  1. Create a SqlConnection object. SqlDatAdapter object.
  2. The SqlConnection object needs connection string as an argument and the SqlDataAdapter requires the SQL Statement and Connection Object as an argument.
  3. The ConnectString gives details about the Database Server, Initial Catalogue, connection type, userid and password.
  4. A typical connection string could look like this:

Sample Code
  1. data source=sql.domain.no;
  2. initial catalog=xxxxx;
  3. User ID=xxxxx;pwd=xxxxx;
  4. Integrated Security=SSPI
Copyright exforsys.com


Code for filling the DataSet is given below:

Sample Code
  1. Dim SQLStr As String
  2. Dim ConStr As String
  3. SQLStr = "SELECT Name FROM production.ProductCategory"
  4. ConStr = "data source=sql.domain.no; initial catalog=xxxxx; User ID=xxxxx;pwd=xxxxx; Integrated Security=SSPI"
  5. Dim sqlConn As New System.Data.SqlClient.SqlConnection(ConStr)
  6. Dim ds As New DataSet
  7. Dim SQLAdapter As New System.Data.SqlClient.SqlDataAdapter(SQLStr, ConStr)
  8. SQLAdapter.Fill(ds)
Copyright exforsys.com



New rows can be added manually to the data set as in the case of a data-entry. In this case the user has to first create a DataSet, a DataTabale, and a DataRow. Then he must populate the DataRow manually by supplying the value for each row and then add these rows to the DataSet. It should be remembered that there is no underlying data store that supplies data in this case. The code for the activity is given below:

Sample Code
  1. Dim dsNew As New DataSet
  2. Dim t As New DataTable
  3. Dim tr As DataRow = dsNew.Tables("T").NewRow
  4. tr("Name") = "Aviation Gears"
  5. dsNew.Tables("T").Rows.Add(tr)
Copyright exforsys.com


You can also populate a DataSet by reading from an WML file. The code listing is given below:

Sample Code
  1. Dim dsXML As New DataSet()
  2. dsXML.ReadXml("XmlFilePath and Name")
Copyright exforsys.com


The user can also create a new DataSet and merge it with any existing DataSet. This opetaion is illustrated by the code given below:

Sample Code
  1. Dim dsXML As New DataSet()
  2. Dim dsCopy As New DataSet
  3. dsXML.ReadXml("XmlFilePath and Name")
  4. dsCopy.Merge(dsXML, True, MissingSchemaAction.AddWithKey)
Copyright exforsys.com


Ads

Moving Around in DataSet and Retrieving Data

In the above sections we have seen how to create a DataSet, how to populate it etc. In the following sections we shall see how to retrieve data from a DataSet. Remember, we stated that there is no current row in a DataSet? So any row can be accessed directly by just mentioning its position? Let us add to this the fact that a DataSet can contain as many tables as required and the user can also create objects that show the relationships that exist and the constraints that are imposed on them. Thus a DataSet can be a very complex data store. Let us understand the process of navigating the DataSet by the following example.

You can write code to go to the first record of the table, go one record forward or backward and also go to the last record. We shall use the BindingContext to do this. Look at the following code:

Click here to view sample code

Retrieving data from DataSets is easy. The data in the DataSet can be displayed in a grid and a script can be written to enable the user browse through the data. The sample code illustrates this.

Sample Code
  1. Imports system.Data
  2. Public Class Form1
  3. Dim ProductDataSet As ds
  4. Private Sub DataLoad()
  5. Dim ProductTableAdapter As New dsTableAdapters.ProductTableAdapter()
  6. ProductDataSet = New ds ProductTableAdapter.Fill(ProductDataSet.Product)
  7. End Sub
Copyright exforsys.com


The code that is given below will make navigation within the data set.

Click here to view sample code



 
This tutorial is part of a VB.NET 2005 tutorial series. Read it from the beginning and learn yourself.

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor
 

Comments