Connection Property has not been initialised

fie

Member
Joined
Aug 30, 2005
Messages
22
Programming Experience
1-3
dear friends please help me.
I have a table called tblclass that contains 3 fields in SQL database

fclasscd int
fclassdesc nvarchar
fcono int

I created a form in sql that will link with the table
i have done all i know that could make me link the table but it is always giving the following errors when i input data and i press the add record button:

my email address is florence_ewuruje@yahoo.com

Additional information: ExecuteReader: Connection property has not been initialized.

Please fnd below the codes, is there anything i am missing out?

thank you


Imports System.Data.SqlClient
Public Class frmClass
Inherits System.Windows.Forms.Form
Protected Const strConnBranch As String = "workstation id=OGUNDIPE;packet size=4096;integrated security=SSPI;data source=OGUNDIPE;persist security info=False;initial catalog=AR"
Protected strBRANCHCMD As String = "select fcono, coname as 'Branch' from tblBranch"
Protected dvtblBranch As DataView
Protected dstblBranch As New DataSet
#
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
Dim DBConn As SqlConnection = New SqlConnection("workstation id=OGUNDIPE;packet size=4096;integrated security=SSPI;data source=OGUNDIPE;persist security info=False;initial catalog=AR")
classSqlConnection.Open()
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 LBLCLASS As System.Windows.Forms.Label
Friend WithEvents lblcode As System.Windows.Forms.Label
Friend WithEvents lblDesc As System.Windows.Forms.Label
Friend WithEvents lblBranch As System.Windows.Forms.Label
Friend WithEvents cmdAdd As System.Windows.Forms.Button
Friend WithEvents cmdDel As System.Windows.Forms.Button
Friend WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
Friend WithEvents classSqlDataAdapter As System.Data.SqlClient.SqlDataAdapter
Friend WithEvents classSqlConnection As System.Data.SqlClient.SqlConnection
Friend WithEvents classSqlCommand As System.Data.SqlClient.SqlCommand
Friend WithEvents classDataSet As System.Data.DataSet
Friend WithEvents cbobranchcode As System.Windows.Forms.ComboBox
Friend WithEvents txtclasscode As System.Windows.Forms.TextBox
Friend WithEvents txtclassDesc As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.LBLCLASS = New System.Windows.Forms.Label
Me.lblcode = New System.Windows.Forms.Label
Me.lblDesc = New System.Windows.Forms.Label
Me.lblBranch = New System.Windows.Forms.Label
Me.txtclasscode = New System.Windows.Forms.TextBox
Me.txtclassDesc = New System.Windows.Forms.TextBox
Me.cbobranchcode = New System.Windows.Forms.ComboBox
Me.cmdAdd = New System.Windows.Forms.Button
Me.cmdDel = New System.Windows.Forms.Button
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
Me.classSqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
Me.classSqlConnection = New System.Data.SqlClient.SqlConnection
Me.classSqlCommand = New System.Data.SqlClient.SqlCommand
Me.classDataSet = New System.Data.DataSet
CType(Me.classDataSet, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'LBLCLASS
'
Me.LBLCLASS.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LBLCLASS.Location = New System.Drawing.Point(136, 24)
Me.LBLCLASS.Name = "LBLCLASS"
Me.LBLCLASS.Size = New System.Drawing.Size(152, 23)
Me.LBLCLASS.TabIndex = 0
Me.LBLCLASS.Text = "CLASS"
Me.LBLCLASS.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblcode
'
Me.lblcode.Location = New System.Drawing.Point(72, 72)
Me.lblcode.Name = "lblcode"
Me.lblcode.TabIndex = 1
Me.lblcode.Text = "CODE"
'
'lblDesc
'
Me.lblDesc.Location = New System.Drawing.Point(72, 112)
Me.lblDesc.Name = "lblDesc"
Me.lblDesc.TabIndex = 2
Me.lblDesc.Text = "DESCRIPTION"
'
'lblBranch
'
Me.lblBranch.Location = New System.Drawing.Point(72, 144)
Me.lblBranch.Name = "lblBranch"
Me.lblBranch.TabIndex = 3
Me.lblBranch.Text = "BRANCH"
'
'txtclasscode
'
Me.txtclasscode.Location = New System.Drawing.Point(184, 72)
Me.txtclasscode.Name = "txtclasscode"
Me.txtclasscode.TabIndex = 4
Me.txtclasscode.Text = ""
'
'txtclassDesc
'
Me.txtclassDesc.Location = New System.Drawing.Point(184, 112)
Me.txtclassDesc.Name = "txtclassDesc"
Me.txtclassDesc.TabIndex = 5
Me.txtclassDesc.Text = ""
'
'cbobranchcode
'
Me.cbobranchcode.Location = New System.Drawing.Point(184, 144)
Me.cbobranchcode.Name = "cbobranchcode"
Me.cbobranchcode.Size = New System.Drawing.Size(121, 21)
Me.cbobranchcode.TabIndex = 6
'
'cmdAdd
'
Me.cmdAdd.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdAdd.Location = New System.Drawing.Point(80, 288)
Me.cmdAdd.Name = "cmdAdd"
Me.cmdAdd.TabIndex = 7
Me.cmdAdd.Text = "Add"
'
'cmdDel
'
Me.cmdDel.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmdDel.Location = New System.Drawing.Point(232, 288)
Me.cmdDel.Name = "cmdDel"
Me.cmdDel.TabIndex = 8
Me.cmdDel.Text = "Delete"
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.Connection = Me.classSqlConnection
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.Connection = Me.classSqlConnection
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.Connection = Me.classSqlConnection
'
'SqlDeleteCommand1
'
Me.SqlDeleteCommand1.Connection = Me.classSqlConnection
'
'classSqlDataAdapter
'
Me.classSqlDataAdapter.DeleteCommand = Me.SqlDeleteCommand1
Me.classSqlDataAdapter.InsertCommand = Me.SqlInsertCommand1
Me.classSqlDataAdapter.SelectCommand = Me.SqlSelectCommand1
Me.classSqlDataAdapter.UpdateCommand = Me.SqlUpdateCommand1
'
'classSqlConnection
'
Me.classSqlConnection.ConnectionString = "workstation id=OGUNDIPE;packet size=4096;integrated security=SSPI;data source=OGU" & _
"NDIPE;persist security info=False;initial catalog=AR"
'
'classDataSet
'
Me.classDataSet.DataSetName = "NewDataSet"
Me.classDataSet.Locale = New System.Globalization.CultureInfo("en-US")
'
'frmClass
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 366)
Me.Controls.Add(Me.cmdDel)
Me.Controls.Add(Me.cmdAdd)
Me.Controls.Add(Me.cbobranchcode)
Me.Controls.Add(Me.txtclassDesc)
Me.Controls.Add(Me.txtclasscode)
Me.Controls.Add(Me.lblBranch)
Me.Controls.Add(Me.lblDesc)
Me.Controls.Add(Me.lblcode)
Me.Controls.Add(Me.LBLCLASS)
Me.Name = "frmClass"
Me.Text = "Class "
CType(Me.classDataSet, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cbobranch_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadBranch()
End Sub
Sub LoadBranch()
Try
'Create connection objects
Dim cnBranch As SqlConnection = New SqlConnection(strConnBranch)
Dim BranchDA As New SqlDataAdapter(strBRANCHCMD, cnBranch)
BranchDA.Fill(dstblBranch, "tblBranch")
dvtblBranch =
New DataView(dstblBranch.Tables("tblBranch"), "", "fcono", DataViewRowState.OriginalRows)
With cbobranchcode
.ValueMember = "fcono"
.DisplayMember = "Branch"
.DataSource = dvtblBranch
End With
cbobranchcode.SelectedIndex = -1
cnBranch.Dispose()
BranchDA.Dispose()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information,
Me.Text)
Catch invalidOperationExceptionParameter As InvalidOperationException
'MessageBox.Show(invalidOperationExceptionParameter.Message)
End Try
End Sub ' Load Branchcode
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim dReader As System.Data.SqlClient.SqlDataReader
'ARSqlConnection.Open()
dReader = classSqlCommand.ExecuteReader
If dReader.HasRows Then
MessageBox.Show("Record already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
classSqlConnection.Close()
'create first the SQL query to insert a row
classSqlDataAdapter.InsertCommand.CommandText = _
"INSERT INTO tblclass(fclasscd, fclassdes, fcono)" & _
"VALUES('" & txtclasscode.Text & "','" & txtclassDesc.Text & "','" & cbobranchcode.Text & "'"
'send Data
classSqlDataAdapter.InsertCommand.ExecuteNonQuery()
MessageBox.Show("Adding Successful", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
classSqlConnection.Close()
' txtSNo.Clear()
' ClearTextBoxes()
MessageBox.Show("Information Ok", MessageBoxIcon.Information, MessageBoxButtons.OK)
End If
End Sub
End
Class
 
Additional information: ExecuteReader: Connection property has not been initialized.

In this case the error message tells you all you need to know. You need to give the reader a connection to work with. While i haven't trawled through all of the code you posted in looksa to be that classSqlCommand doens't actually do anything here, and even if i missed the part where it does get it's instructions you haven't supplied it with a connection.

VB.NET:
ClassSqlCommand.Connection = SomeConnection
 
Back
Top