Rajesh’s Weblog

Just another WordPress.com weblog

Ado.Net Exception: Wrong SQL command exception

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim SqlConnection1 As New SqlConnection(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;database=MyDatabase”)

Dim thisCommand As SqlCommand = SqlConnection1.CreateCommand()
thisCommand.CommandText = “Select nonExistColumn from Employee”

Try
‘ Open connection
SqlConnection1.Open()

‘ Run stored procedure
thisCommand.ExecuteNonQuery()

Catch ex As System.Data.SqlClient.SqlException
Dim str As String
str = “Source : ” & ex.Source & ControlChars.NewLine
str &= “Number : ” & ex.Number & ControlChars.NewLine
str &= “Message : ” & ex.Message & ControlChars.NewLine
str &= “Class : ” & ex.Class.ToString() & ControlChars.NewLine
str &= “Procedure : ” & ex.Procedure & ControlChars.NewLine
str &= “Line number : ” & ex.LineNumber.ToString() & ControlChars.NewLine
str &= “Server : ” & ex.Server
Console.WriteLine(“Database Exception” & str)

Catch ex As System.Exception
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
Console.WriteLine(“General Exception” & str)

Finally
If SqlConnection1.State = ConnectionState.Open Then
Console.WriteLine(“Finally block closing the connection”)
SqlConnection1.Close()
End If
End Try
End Sub
End Class

Posted in dotNet | Leave a Comment »

Use Alias to reference data

Posted by arajesh on April 4, 2008

Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data.OleDb
Imports System.Collections
Imports System.Data

Public Class MainClass

Shared Sub Main()
Try
‘ Define a connection object
Dim dbConn As New OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Password=;User ID=Admin;Data Source=Employee.mdb”)

‘ Create a data adapter to retrieve records from db
Dim strSELECT As String = “SELECT ID AS EmployeeID, FirstName AS FirstName, LastName AS LastName FROM Employee”
Dim daUsers As New OleDbDataAdapter(strSELECT, dbConn)
Dim dsUsers As New DataSet(“Employee”)

‘ Fill the dataset
daUsers.Fill(dsUsers)

‘ Go through the records and print them using the mapped names
Dim r As DataRow
For Each r In dsUsers.Tables(0).Rows
Console.WriteLine(“ID: {0}, FirstName: {1}, LastName: {2}”, r(“EmployeeID”), r(“FirstName”), r(“LastName”))
Next
Catch ex As Exception

‘ An error occurred. Show the error message
Console.WriteLine(ex.Message)
End Try
End Sub
End Class

Posted in dotNet | Leave a Comment »

Use Data Grid to update Data

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class

Public Class Form1
Inherits System.Windows.Forms.Form

Private cb As SqlCommandBuilder
Private da As SqlDataAdapter

#Region ” Windows Form Designer generated code “

Public Sub New()
MyBase.New()

‘This call is required by the Windows Form Designer.
InitializeComponent()

‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents buttonUpdate As System.Windows.Forms.Button
Friend WithEvents DataSet1 As System.Data.DataSet
Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.buttonUpdate = New System.Windows.Forms.Button
Me.DataSet1 = New System.Data.DataSet
Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()

‘DataGrid1

Me.DataGrid1.DataMember = “”
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
Me.DataGrid1.Name = “DataGrid1″
Me.DataGrid1.Size = New System.Drawing.Size(400, 192)
Me.DataGrid1.TabIndex = 0

‘buttonUpdate

Me.buttonUpdate.Location = New System.Drawing.Point(171, 208)
Me.buttonUpdate.Name = “buttonUpdate”
Me.buttonUpdate.TabIndex = 1
Me.buttonUpdate.Text = “Update”

‘DataSet1

Me.DataSet1.DataSetName = “NewDataSet”
Me.DataSet1.Locale = New System.Globalization.CultureInfo(“en-GB”)

‘Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(416, 245)
Me.Controls.Add(Me.buttonUpdate)
Me.Controls.Add(Me.DataGrid1)
Me.Name = “Form1″
Me.Text = “Form1″
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Create Connection object
Dim thisConnection As New SqlConnection _
(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;” & _
“database=MyDatabase”)

‘ Sql Query
Dim sql As String = _
“SELECT * FROM Employee”

‘ Create a Command
SqlCommand1 = New SqlCommand(sql, thisConnection)

‘ Create SqlDataAdapter
da = New SqlDataAdapter
da.SelectCommand = SqlCommand1

‘ Create SqlCommandBuilder object
cb = New SqlCommandBuilder(da)

‘ Fill Dataset
da.Fill(DataSet1, “Employee”)

‘ Bind the data to the grid at runtime
DataGrid1.SetDataBinding(DataSet1, “Employee”)
End Sub

Private Sub buttonUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonUpdate.Click
da.Update(DataSet1, “Employee”)
End Sub
End Class

Posted in dotNet | Leave a Comment »

Bind data to ListBox

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code “

Public Sub New()
MyBase.New()

‘This call is required by the Windows Form Designer.
InitializeComponent()

‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents DataSet1 As System.Data.DataSet
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.DataSet1 = New System.Data.DataSet
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()

‘ListBox1

Me.ListBox1.Location = New System.Drawing.Point(8, 8)
Me.ListBox1.Name = “ListBox1″
Me.ListBox1.Size = New System.Drawing.Size(272, 95)
Me.ListBox1.TabIndex = 0

‘TextBox1

Me.TextBox1.Location = New System.Drawing.Point(8, 112)
Me.TextBox1.Name = “TextBox1″
Me.TextBox1.Size = New System.Drawing.Size(128, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = “TextBox1″

‘TextBox2

Me.TextBox2.Location = New System.Drawing.Point(152, 112)
Me.TextBox2.Name = “TextBox2″
Me.TextBox2.Size = New System.Drawing.Size(128, 20)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = “TextBox2″

‘DataSet1

Me.DataSet1.DataSetName = “NewDataSet”
Me.DataSet1.Locale = New System.Globalization.CultureInfo(“en-GB”)

‘Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 149)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.ListBox1)
Me.Name = “Form1″
Me.Text = “Form1″
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Create Connection object
Dim thisConnection As New SqlConnection _
(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;” & _
“database=MyDatabase”)

‘ Sql Query
Dim sql As String = _
“SELECT FirstName, LastName FROM Employee”

‘ Create Data Adapter
Dim da As New SqlDataAdapter(sql, thisConnection)

‘ Fill Dataset and Create DataTable
da.Fill(DataSet1, “Employee”)
Dim dt As DataTable = DataSet1.Tables(“Employee”)

ListBox1.DataSource = dt
ListBox1.DisplayMember = “FirstName”

‘ Bind to firstname column of the employees table
TextBox1.DataBindings.Add(“text”, dt, “FirstName”)

‘ Bind to lastname column of the employees table
TextBox2.DataBindings.Add(“text”, dt, “LastName”)
End Sub
End Class

Posted in dotNet | Leave a Comment »

Data set Bind to Label

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code “

Public Sub New()
MyBase.New()

‘This call is required by the Windows Form Designer.
InitializeComponent()

‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents DataSet1 As System.Data.DataSet
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.DataSet1 = New System.Data.DataSet
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()

‘Label1

Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = “Label1″
Me.Label1.TabIndex = 0
Me.Label1.Text = “Label1″

‘Label2

Me.Label2.Location = New System.Drawing.Point(16, 56)
Me.Label2.Name = “Label2″
Me.Label2.TabIndex = 1
Me.Label2.Text = “Label2″

‘DataSet1

Me.DataSet1.DataSetName = “NewDataSet”
Me.DataSet1.Locale = New System.Globalization.CultureInfo(“en-GB”)

‘Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(128, 85)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = “Form1″
Me.Text = “Form1″
CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Create Connection object
Dim thisConnection As New SqlConnection _
(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;” & _
“database=MyDatabase”)

‘ Sql Query
Dim sql As String = _
“SELECT * FROM Employee”

‘ Create Data Adapter
Dim da As New SqlDataAdapter(sql, thisConnection)

‘ Fill Dataset
da.Fill(DataSet1, “Employee”)

‘ Bind Label1 to ProductName column of the Products table
Label1.DataBindings.Add(“text”, DataSet1, “Employee.FirstName”)

‘ Bind Label2 to UnitPrice column of the Products table
Label2.DataBindings.Add(“text”, DataSet1, “Employee.LastName”)
End Sub
End Class

Posted in dotNet | Leave a Comment »

Data binding to Data Grid

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources

Public Class MainClass

Shared Sub Main()
Dim form1 As Form = new FrmTableDisplay()
Application.Run(form1)
End Sub
End Class

Public Class FrmTableDisplay
Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code “

Public Sub New()
MyBase.New()

‘ This call is required by the Windows Form Designer.
InitializeComponent()

‘ Add any initialization after the
‘ InitializeComponent() call

‘ fill DataSet1 with data
OleDbDataAdapter1.Fill(DataSet1, “Employee”)

‘ bind data in Users table in DataSet1 to dataGrid1
dgdEmployee.SetDataBinding(DataSet1, “Employee”)
End Sub ‘ New

‘ Form overrides Dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose( _
ByVal disposing As Boolean)

If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub ‘ Dispose

Friend WithEvents dgdEmployee As System.Windows.Forms.DataGrid
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand

Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand

Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand

Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand

Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection

Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter

Friend WithEvents DataSet1 As System.Data.DataSet

‘ Required by the Windows Form Designer
Private components As System.ComponentModel.Container

‘ NOTE: The following procedure is required by the
‘ Windows Form Designer
‘ It can be modified using the Windows Form Designer.
‘ Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()

Me.dgdEmployee = New System.Windows.Forms.DataGrid()
Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand()

Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand()

Me.OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand()

Me.OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand()

Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection()

Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter()

Me.DataSet1 = New System.Data.DataSet()
CType(Me.dgdEmployee,System.ComponentModel.ISupportInitialize).BeginInit()

CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()


‘ dgdEmployee

Me.dgdEmployee.DataMember = “”
Me.dgdEmployee.Location = New System.Drawing.Point(8, 8)
Me.dgdEmployee.Name = “dgdEmployee”
Me.dgdEmployee.Size = New System.Drawing.Size(304, 256)
Me.dgdEmployee.TabIndex = 0


‘ OleDbSelectCommand1

Me.OleDbSelectCommand1.CommandText = “SELECT ID, FirstName, LastName FROM Employee”

Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1


‘ OleDbInsertCommand1

Me.OleDbInsertCommand1.CommandText = “INSERT INTO Employee(ID, FirstName, LastName)” & _
“VALUES (?, ?, ?)”

Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1

Me.OleDbInsertCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“ID”, _
System.Data.OleDb.OleDbType.Numeric, 0, _
System.Data.ParameterDirection.Input, False, _
CType(10, Byte), CType(0, Byte), “ID”, _
System.Data.DataRowVersion.Current, Nothing))

Me.OleDbInsertCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“FirstName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “FirstName”, _
System.Data.DataRowVersion.Current, Nothing))

Me.OleDbInsertCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“LastName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “LastName”, _
System.Data.DataRowVersion.Current, Nothing))


‘ OleDbUpdateCommand1

Me.OleDbUpdateCommand1.CommandText = _
“UPDATE Employee SET ID = ?, FirstName = ?, ” & _
“LastName = ? WHERE (ID = ?)” & _
” AND (FirstName = ?) AND (LastName = ?)”

Me.OleDbUpdateCommand1.Connection = Me.OleDbConnection1
Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“ID”, _
System.Data.OleDb.OleDbType.Numeric, 0, _
System.Data.ParameterDirection.Input, False, _
CType(10, Byte), CType(0, Byte), “ID”, _
System.Data.DataRowVersion.Current, Nothing))

Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“FirstName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “FirstName”, _
System.Data.DataRowVersion.Current, Nothing))

Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“LastName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “LastName”, _
System.Data.DataRowVersion.Current, Nothing))

Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter _
(“Original_ID”, _
System.Data.OleDb.OleDbType.Numeric, 0, _
System.Data.ParameterDirection.Input, False, _
CType(10, Byte), CType(0, Byte), “ID”, _
System.Data.DataRowVersion.Original, Nothing))

Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter _
(“Original_FirstName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “FirstName”, _
System.Data.DataRowVersion.Original, Nothing))

Me.OleDbUpdateCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter _
(“Original_LastName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “LastName”, _
System.Data.DataRowVersion.Original, Nothing))


‘ OleDbDeleteCommand1

Me.OleDbDeleteCommand1.CommandText = _
“DELETE FROM Employee WHERE (ID = ?) AND ” & _
“(FirstName = ?) AND (LastName = ?)”

Me.OleDbDeleteCommand1.Connection = Me.OleDbConnection1
Me.OleDbDeleteCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“ID”, _
System.Data.OleDb.OleDbType.Numeric, 0, _
System.Data.ParameterDirection.Input, False, _
CType(10, Byte), CType(0, Byte), “ID”, _
System.Data.DataRowVersion.Original, Nothing))

Me.OleDbDeleteCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“FirstName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “FirstName”, _
System.Data.DataRowVersion.Original, Nothing))

Me.OleDbDeleteCommand1.Parameters.Add _
(New System.Data.OleDb.OleDbParameter(“LastName”, _
System.Data.OleDb.OleDbType.Char, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), “LastName”, _
System.Data.DataRowVersion.Original, Nothing))


‘OleDbConnection1

Me.OleDbConnection1.ConnectionString = _
“Provider=Microsoft.Jet.OLEDB.4.0;Password=”"”";” & _
“User ID=Admin;Data Source=Employee.mdb;Mode=Sha” & _
“re Deny None;Extended Properties=”"”";” & _
“Jet OLEDB:System database=”"”";Jet OLEDB:Regis” & _
“try Path=”"”";Jet OLEDB:Database Password=”"”";” & _
“Jet OLEDB:Engine Type=5;Jet OLEDB:Dat” & _
“abase Locking Mode=1;Jet OLEDB:Global Partial ” & _
“Bulk Ops=2;Jet OLEDB:Global Bulk T” & _
“ransactions=1;Jet OLEDB:New Database ” & _
“Password=”"”";Jet OLEDB:Create System Databas” & _
“e=False;Jet OLEDB:Encrypt Database=False;” & _
“Jet OLEDB:Don’t Copy Locale on Compact=” & _
“False;Jet OLEDB:Compact Without Replica ” & _
“Repair=False;Jet OLEDB:SFP=False”


‘ OleDbDataAdapter1

Me.OleDbDataAdapter1.DeleteCommand = _
Me.OleDbDeleteCommand1

Me.OleDbDataAdapter1.InsertCommand = _
Me.OleDbInsertCommand1

Me.OleDbDataAdapter1.SelectCommand = _
Me.OleDbSelectCommand1

Me.OleDbDataAdapter1.TableMappings.AddRange _
(New System.Data.Common.DataTableMapping() _
{New System.Data.Common.DataTableMapping(“Table”, _
“Employee”, New System.Data.Common.DataColumnMapping() _
{New System.Data.Common.DataColumnMapping(“ID”, _
“ID”), New System.Data.Common.DataColumnMapping _
(“FirstName”, “FirstName”), _
New System.Data.Common.DataColumnMapping(“LastName”, _
“LastName”)})})

Me.OleDbDataAdapter1.UpdateCommand = _
Me.OleDbUpdateCommand1


‘ DataSet1

Me.DataSet1.DataSetName = “NewDataSet”
Me.DataSet1.Locale = _
New System.Globalization.CultureInfo(“en-US”)


‘ FrmTableDisplay

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() _
{Me.dgdEmployee})

Me.Name = “FrmTableDisplay”
Me.Text = “Table Display”
CType(Me.dgdEmployee, System.ComponentModel. _
ISupportInitialize).EndInit()

CType(Me.DataSet1, System.ComponentModel. _
ISupportInitialize).EndInit()

Me.ResumeLayout(False)

End Sub ‘ InitializeComponent

#End Region

End Class

Posted in dotNet | Leave a Comment »

Database Data Binding: ComboBox

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources

Public Class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1()
Application.Run(form1)

End Sub
End Class

Public Class Form1
‘Form level variables
Private strConnectionString As String = _
“Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=Employee.mdb;”

Private objConnection As OleDbConnection
Private objCommand As OleDbCommand
Private objDataAdapter As OleDbDataAdapter
Private objDataTable As DataTable

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load

‘Initialize the Connection object
objConnection = New OleDbConnection(strConnectionString)

‘Initialize the Command object
objCommand = New OleDbCommand(“SELECT ID, FirstName ” & _
“FROM Employee”, objConnection)

‘Initialize the DataAdapter object and set the SelectCommand property
objDataAdapter = New OleDbDataAdapter
objDataAdapter.SelectCommand = objCommand

‘Initialize the DataTable object
objDataTable = New DataTable

‘Populate the DataTable
objDataAdapter.Fill(objDataTable)

‘Bind the DataTable to the ComboBox
ComboBox1.DataSource = objDataTable
ComboBox1.DisplayMember = “FirstName”
ComboBox1.ValueMember = “ID”

‘Clean up
objDataAdapter.Dispose()
objDataAdapter = Nothing
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing
End Sub
End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form

‘Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()

‘ComboBox1

Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Location = New System.Drawing.Point(76, 13)
Me.ComboBox1.Name = “ComboBox1″
Me.ComboBox1.Size = New System.Drawing.Size(205, 21)
Me.ComboBox1.TabIndex = 3

‘Label1

Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(13, 16)
Me.Label1.Name = “Label1″
Me.Label1.Size = New System.Drawing.Size(41, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = “Name”

‘Form1

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 76)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.Label1)
Me.Name = “Form1″
Me.Text = “Form1″
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Label1 As System.Windows.Forms.Label

End Class

Posted in dotNet | Leave a Comment »

Data binding to a DataGrid

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class

Public Class Form1
Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code “

Public Sub New()
MyBase.New()

‘This call is required by the Windows Form Designer.
InitializeComponent()

‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()

‘DataGrid1

Me.DataGrid1.DataMember = “”
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(8, 0)
Me.DataGrid1.Name = “DataGrid1″
Me.DataGrid1.Size = New System.Drawing.Size(552, 280)
Me.DataGrid1.TabIndex = 0

‘Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(568, 285)
Me.Controls.Add(Me.DataGrid1)
Me.Name = “Form1″
Me.Text = “Form1″
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Create Connection object
Dim thisConnection As New SqlConnection _
(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;” & _
“database=MyDatabase”)

‘ Sql Query
Dim sql As String = _
“SELECT * FROM Employee”

‘ Create Data Adapter
Dim da As New SqlDataAdapter(sql, thisConnection)

‘ Create and fill Dataset
Dim ds As New DataSet
da.Fill(ds, “Employee”)

‘ Bind the data table to the data grid
DataGrid1.SetDataBinding(ds, “Employee”)

End Sub
End Class

Posted in dotNet | Leave a Comment »

Ado.Net Exception: No Store Procedure Exception

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim SqlConnection1 As New SqlConnection(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;database=MyDatabase”)

Dim thisCommand As SqlCommand = SqlConnection1.CreateCommand()

thisCommand.CommandType = CommandType.StoredProcedure

thisCommand.CommandText = “SelectAllEmployees”

Try
SqlConnection1.Open()

Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()

thisReader.Close()
Catch ex As System.Data.SqlClient.SqlException
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
Console.WriteLine(“Database Exception” & str)
Catch ex As System.Exception
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
Console.WriteLine(“Non-Database Exception ” & str)
Finally
If SqlConnection1.State = ConnectionState.Open Then
Console.WriteLine(“Finally block closing the connection”)
SqlConnection1.Close()
End If
End Try
End Sub
End Class

Posted in dotNet | Leave a Comment »

Ado.Net Exception: access non-exist column exception

Posted by arajesh on April 4, 2008

Imports System
Imports System.Data
Imports System.Data.SqlClient

public class MainClass
Shared Sub Main()
Dim SqlConnection1 As New SqlConnection(“server=(local)\SQLEXPRESS;” & _
“integrated security=sspi;database=MyDatabase”)

Dim thisCommand As SqlCommand = SqlConnection1.CreateCommand()
thisCommand.CommandText = “Select * from Employee”

Try
‘ Open connection
SqlConnection1.Open()

‘ Run command and get data as a reader
Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()

‘ Access non-existent column
Dim str As String = thisReader.GetValue(20).ToString()

‘ Close data reader
thisReader.Close()

Catch ex As System.InvalidOperationException
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
str &= ControlChars.NewLine
str &= “Stack Trace : ” & ex.StackTrace
Console.WriteLine(“Specific Exception:” & str)
Catch ex As System.Data.SqlClient.SqlException
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
Console.WriteLine(“Database Exception” & str)

Catch ex As System.Exception
Dim str As String
str = “Source : ” & ex.Source
str &= ControlChars.NewLine
str &= “Exception Message : ” & ex.Message
Console.WriteLine(“Generic Exception” & str )

Finally
If SqlConnection1.State = ConnectionState.Open Then
Console.WriteLine(“Finally block closing the connection”)
SqlConnection1.Close()
End If
End Try
End Sub
End Class

Posted in dotNet | Leave a Comment »