jump to navigation

Using a DataGridView October 19, 2008

Posted by tuse in : ASP.NET, Databases, VB.NET , 1 comment so far

The DataGridView Control Control can be used to display the contents of a table from the database as per an SQL query of user’s choice.

Let us see an example, add a DataGridView from the ToolBox to a new Windows Form.

When the Form Loads, we will look to fill the DGV with the following code-


Dim cmd As New Odbc.OdbcCommand("Select number as 'Book Number',title as 'Book Name', author as 'Author', publisher as 'Publisher', no_of_copies as 'No. of Copies', number_issued as 'No Issued', sub_category as 'Sub Category',edition as 'Edition' from book_details", cn)

'cn is the ODBC Connection Object defined in earlier posts

        Dim adp As Odbc.OdbcDataAdapter
        adp = New Odbc.OdbcDataAdapter(cmd)
        Dim ds As New DataSet
        adp.Fill(ds, "book_details")
        Me.DataGridView1.DataSource = ds
        Me.DataGridView1.DataMember = "book_details"
        Me.DataGridView1.AutoResizeColumns()

We are using a DataSet-the core of the ADO.NET disconnected architecture and is used to store data in a disconnected state. In accordance to the SQL query, the DataSet is filled using the DataAdapter (adp).

The DataGridView comes with several options- enabling the user to edit records, add records, delete records.

In order to retrive data displayed in the DataGridView we can use the following code- this piece of code is very useful and I have used it on several occasions:


Dim i As Integer
        i = DataGridView1.CurrentRow.Index
        Dim book_number as Integer  = DataGridView1.Item(0, i).Value
        

What we are doing in this piece of code is that we are are holding the current row’s index number in the variable i and thereafter the item in the first column in the variable book_number

Using XML in .NET August 2, 2008

Posted by tuse in : ASP.NET, Tools, VB.NET , 2 comments

We will see how we can use XML files in a VB.NET application. XML is used in many aspects of web development, often to simplify data storage.

In a Windows form, add a DataGridView and a Button. Change the text of this Button as ‘Save’.

The XML file that I am interested in-

    


  
    John Smith
    Computers
  

  
    Matthew Max
    Electrical
  

  
    Jay Yan
    Mechanical
  


The XML file that I have holds freshman records studying in an engineering school. We will look to display the fields ‘name’ and ‘branch’ in the DataGridView.

The ‘Save’ button should be able to save the changes made to the records in the DataGridView in the XML file.

We shall use a DataSet for reading the XML file. The Save Button will involve the Write function to the XML file invoked using the dataset.

The code-

Public Class Form1

    Dim ds As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds = New DataSet
        ds.ReadXml("C:\myxml\file1.xml") 'Path where your XML file exists on the system
        Me.DataGridView1.DataSource = ds
        Me.DataGridView1.DataMember = "freshman" 'Records that you wish to display. Look at the XML file for reference
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ds.WriteXml("C:\myxml\file1.xml") 'Save the changes made in the DataGridView to be reflected in the XML file
    End Sub
End Class

This is an illustration of the usage of XML in VB.NET. You may use this in ASP.NET too. In order to specify the source for the file on the server, you may use ‘Server.MapPath()’ to specify the path for your XML file.


Warning: stristr() [function.stristr]: Empty delimiter in /home/tekyt17/public_html/dotnet/wp-content/plugins/wassup/wassup.php on line 2093