Free Blog Counter

Ranjiths Dot Net Blog

Friday, 3 October 2008

Create and Remove Controls at Run Time

It is always been very interesting to work with Visual Studio 2005 and 2008.  I love programming with VB.Net and it is somewhat amazing.  

When I started developing applications with Visual Studio 2008, I faced some difficulties in handling the controls which are created at runtime.  

The code below shows the creation of the label at runtime and I believe it is easy to follow without any explanation:

 

Imports System.Windows.Forms

Public Class Form1

    Dim i As Integer

‘This part of the code is to create label at runtime

‘---------------------------------------------------------------------

 Private Sub Button1_Click(ByVal sender As System.Object, 

                           ByVal e As System.EventArgs) Handles Button1.Click

        Dim alabel As New Label

        Me.Controls.Add(alabel)

        alabel.Top = Me.Controls.Count * 25

        alabel.BackColor = Color.CornflowerBlue

        alabel.Width = 100

        i += 1

        alabel.Text = "Label" & i

        alabel.Tag = "Label" & i

 End Sub

 

‘---------------------------------------------------------------------

‘This part of the code is to remove the label

    Private Sub Button2_Click(ByVal sender As System.Object, 

                              ByVal e As System.EventArgs) Handles Button2.Click

        Dim ctrl As Control

        For Each ctrl In Controls

            If TypeOf ctrl Is Label Then

                If ctrl.Tag = "Label" & i Then

                    Me.Controls.Remove(ctrl)

                    i = i - 1

                End If

            End If

        Next

    End Sub

End Class

Friday, 22 August 2008

Connecting to a Database

The first step to using ADO.NET is to connect to a data source, such as a database. Using the Connection object, you tell ADO.NET which database you want to connect. The Connection object is your gateway to the database, and all the operations you perform against the database must go through this gateway.

Now lets try by a creating a connection to the Northwind database.

‘Namespace

Imports system.data

Imports system.data.sqlclient

Dim connNorthwind as new SqlConnection

connNorthwind.ConnectionString=“data source = (local) ; initial catalog=Northwind; integrated security=SSPI;”

‘Some of the names in the connection string also go by aliases. You can use Server instead of data source to specify your SQL Server. Instead of initial catalog, you can specify database.

The Open() Method

After you have specified the ConnectionString property of the Connection object, you must call the Open() method to establish a connection to the database.

connNorthwind.Open()

The Close() Method

Use the Connection object’s Close() method to close an open connection.

connNorthwind.Close()

Alternatively, you could call the Dispose() method, which also closes the connection:

ConnNorthwind.Dispose()


The final code for the Connection will look like this:

Imports system.data

Imports system.data.sqlclient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Button1.Click

Dim connNorthwind As New SqlConnection

connNorthwind.ConnectionString = "data source=(local); initial

catalog=Northwind; integrated security=SSPI;"

connNorthwind.Open()

'some code here…….

connNorthwind.close()

End Sub

End Class

Monday, 18 August 2008

How ADO.Net works?

The main aim of this blog is to give a better coding experience to those programmers who are good in VB6 but know nothing about VB.Net 2005. This blog will provide hard coding experience in VB.Net 2005 and Dot Net Framework 2.0.

ADO.Net opens up a whole new world of data access, giving you the power to control the changes you make to your data. Each ADO.Net component has its own specialty, unlike the Recordset, which is a jack-of-all-trades. In ADO.NET, you always know what to expect from your data access objects, and this lets you streamline your code with specific functionality and greater control.

To comfortably use the ADO.NET objects in an application, you should use the IMPORTS statement. By doing so, you can declare ADO.NET variables without having to fully qualify them. You could type the following IMPORTS statement at the top of your solution:

Imports System.Data.SqlClient

The above statement is called Namespace. So, what is a Namespace and what is the importance of them?

A Namespace is a collection of different classes which are built in to .Net. You need to import theses namespaces to work with any .Net language. All the functionality of .Net is within these namespaces.

System.Data: Include classes that make up ADO.Net and allow building data-handling components.

So, before proceeding with the coding we have to use this statement:

Imports System.Data.SqlClient

After this, you can work with the SqlClient ADO.NET objects without having to fully qualify the class names.

The five core objects form the foundation of the ADO.Net object model is: Connection, Command, DataReader, DataSet and DataAdapter. The Connection,Command,DataReader and DataAdapter belong to the .NET data provider, whereas the DataSet is part of the disconnected data storage mechanism.

Four core objects belong to .NET data providers, within the ADO.NET managed provider architecture is: the Connection, Command, DataReader and DataAdapter objects.

The Connection object is the simplest one, because it establishes a connection to the database.

The Command object exposes a Parameters collection, which contains information about the parameters of the command to be executed.

The DataReader object provides fast access to read-only, forward-only data, which is identical to read-only, forward-only ADO Recordset in VB6.0.

The DataAdapter object contains Command objects that enable you to map specific actions to your data source. The DataAdapter is a mechanism for bridging the managed providers with the disconnected DataSets.

The DataSet object is not part of the ADO.NET managed provider architecture. The DataSet exposes a collection of DataTables, which in turn contain both DataColumn and DataRow collections. The DataTables collection can be used in conjunction with the DataRelation collection to create relational data structures.


Friday, 15 August 2008

How to clear text boxes of a GroupBox in VB.Net

Everybody has to start somewhere. Specially, programming in VB.Net is somewhat interesting thing to do.

The code below will help a novice software developer to clear the text boxes of Groupbox in VB.Net:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ctl As Control


For Each ctl In GroupBox1.Controls
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next
End Sub