The ConnectionString property has not been initialized

  • Thread starter Thread starter Joshua Campbell
  • Start date Start date
J

Joshua Campbell

I just wrote a piece of code to insert into a SQL database. Sometimes it
works, sometimes it errors out with "The ConnectionString property has not
been initialized". I can not figure out why it is diong this. Can someone
please help me figure out what is wrong? Here is the code:
==================
Dim objAck As DataView = MyView1
Dim AckView As DataRowView
Dim strRev, strWLMOrderNum, strCustOrderNum, strSoldTo, strShipTo,
strOrderDate As String

Try
For Each AckView In objAck
strWLMOrderNum = AckView("OurOrderNo")
strRev = AckView("Rev")
strCustOrderNum = AckView("CustOrderNo")
strOrderDate = AckView("OrderDate")
strOrderDate = strOrderDate.Replace("-", "/")
strSoldTo = AckView("SoldToNo")
strShipTo = AckView("ShipToNo")
Next
If strRev = "" Then
Dim queryCommand As New SqlClient.SqlCommand
queryCommand.CommandType = CommandType.Text
queryCommand.Connection = SqlConnection1
queryCommand.Connection.Open()

queryCommand.CommandText = "INSERT INTO OrderAck (SoldTo,
ShipTo, OrderDate, WLMOrderNum, CustOrderNum) VALUES ('" _
& strSoldTo & "', '" & strShipTo & "', '" & strOrderDate & "', '" &
strWLMOrderNum & "', '" & strCustOrderNum & "')"

queryCommand.ExecuteNonQuery()

queryCommand.Connection.Close()
queryCommand.Connection.Dispose()
queryCommand = Nothing
End If
Catch x As Exception
EventLog.WriteEntry("OrderAck to PDF Application", "There was an
error in UploadToSQL." & vbCrLf & x.Source & vbCrLf & x.Message,
EventLogEntryType.Error, 1, 1)
End Try
==================

x.Source is coming back with "System.Data" and x.Message is stating "The
ConnectionString property has not been initialized.".

Please help me. Could it be that during the first pass, this works, but the
dispose gets rid of the values in my SqlConnection1 for the other passes?

Thanks.
Joshua
 
Check the value of SqlConnection1.ConnectionString when it errors out. That
is where the problem is. Just watch that value for when it get cleared out.

Chris
 
Hi

I think you may try to comment out the code line below if that works for
you.
queryCommand.Connection.Dispose()

Because the queryCommand need the connection object, if we have disposed it
and did not recreate it, the queryCommand will not execute.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top