Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ConnectionString Property not initialized

Status
Not open for further replies.

UHNSTrust

Technical User
Dec 2, 2003
262
0
0
GB
I have the following code (forgive the lack of try...finally statements as I am new to .net) that throws the ConnectionString property not initialized error.

Public Function CustomerExists(ByVal pstrSQL As String) As Boolean
On Error GoTo CustomerExists_err
'returns a true value if a customer exists

Me.OleDbConnection1.Open()
Dim myCommand As New OleDb.OleDbCommand

myCommand.Connection = Me.OleDbConnection1
myCommand.CommandText = "SELECT COUNT(*) AS Expr1 FROM(tblCustomer)" & pstrSQL

Dim rstCount As Int32
rstCount = myCommand.ExecuteScalar()
Me.OleDbConnection1.Close()

'get the value from the query
If rstCount <> 0 Then
CustomerExists = True
End If

'clean up the objects
Me.OleDbConnection1.Close()

rstCount = Nothing

CustomerExists_err:
Select Case Err.Number
Case 0
'Do nothing as this is correct
Case 3021
CustomerExists = False
'clean up the objects
Me.OleDbConnection1.Close()

rstCount = Nothing

Case Else
MsgBox(Err.Number & &quot; - &quot; & Err.Description)
End Select

End Function

The pstrSQL string is a where statement passed ito the function.

Can anybody help on where I am going wrong?

Thanks in advance

Jonathan
 
Jonathan,

not sure if this helps but encompass the select query in a string then just call the strin

lstrsql = &quot;SELECT COUNT(*) AS Expr1 FROM(tblCustomer)&quot; & pstrSQL
myCommand.CommandText = lstrsql
hth,
bueller
 
Where is your connection string. I do not see it in the code you have posted.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Before you open a connection, you must set the connection string.
OLEDB example:

Dim strConnection as string
Dim M_Server as string = &quot;Server Name&quot;
Dim M_Database as string = &quot;Database Name&quot;
Dim Password as String = &quot;USER Password&quot;
Dim UserID as string = &quot;User Login ID for SQL Server&quot;

strConnection = &quot;Provider=SQLOLEDB;&quot; & &quot;Data Source=&quot; & M_Server & &quot;;Initial Catalog=&quot; & M_Database & &quot;;User Id=&quot; & UserID & &quot;;Password=&quot; & p_Pwd & &quot;;&quot;

cn = New OleDb.OleDbConnection(strConnection)

cn.open()

Ken
 
Thanks for your replies.

I thought that the fact that I had a connection open (see below) would mean that it would take the string from that.

Me.OleDbConnection1.Open()
Dim myCommand As New OleDb.OleDbCommand

myCommand.Connection = Me.OleDbConnection1


Is this not the case?

Jonathan
 
Sorted!!!

My oledbconnection was not set up right.

Sorry for wasting your time. Apart from that the code is correct.

Thanks again

Jonathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top