Ancient programmer here - new to VB. I'm using the Foxall book "Teach Yourself Visual Basic 2010 in 24 Hours" and have a problem in the hour 21 project "Database Example". Hopefully, I'll describe everything with correct terms here: Using ADO.net and have Microsoft SQL Server 2008 installed on my 64 bit Win 7 system. The database file is installed in my c:\temp directory with the name of test.mdf. I've verified it's there and opened it with Wordpad and see some data that's expected to display, so assume the database is at least present and in the right place. When I run the code (example to follow), I get this error: "A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll" . The error message appears after the amount of time I specified in the Connect Timeout and debug shows the error to occur on the database Open. Here's the portion of the code that's failing:
Code:
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
m_cn.ConnectionString = "Data Source=.\SQLEXPRESS; AttachDbFilename = " & _
"c:\Temp\Test.mdf;Integrated Security=True;Connect Timeout=10;" & _
"User Instance=True"
m_cn.Open()
m_DA = New SqlDataAdapter("select * from contacts", m_cn)
m_CB = New SqlCommandBuilder(m_DA)
m_DA.Fill(m_DataTable)
Me.ShowCurrentRecord()
End Sub
While setting up this project, I did run into one oddity that may or may not be related. The book instructed me to add references to a couple of name spaces and check off some names in a list, including: System.Data, System.Data.SqlClient and System.Xml.System.Data. The System.Xml.System.Data item did not appear in the list of things I could check off, so that wasn't done.
I set a break point to stop after the database open and then displayed the m_cn properties - here they are:
?m_cn
{System.Data.SqlClient.SqlConnection}
_currentReconnectionTask: Nothing
_impersonateIdentity: Nothing
_lastIdentity: Nothing
_recoverySessionData: Nothing
_statistics: Nothing
_supressStateChangeForReconnection: False
AsyncCommandInProgress: False
Asynchronous: True
CanRaiseEvents: True
CanRaiseEventsInternal: True
ClientConnectionId: {00000000-0000-0000-0000-000000000000}
CloseCount: 0
ConnectionFactory: {System.Data.SqlClient.SqlConnectionFactory}
ConnectionOptions: {System.Data.SqlClient.SqlConnectionString}
ConnectionString: "Data Source=.\SQLEXPRESS; AttachDbFilename = c:\Temp\Test.mdf;Integrated Security=True;Connect Timeout=10;User Instance=True"
ConnectionTimeout: 10
ConnectRetryInterval: 10
Container: Nothing
Credential: Nothing
Database: ""
DataSource: ".\SQLEXPRESS"
DbProviderFactory: {System.Data.SqlClient.SqlClientFactory}
DesignMode: False
Events: {System.ComponentModel.EventHandlerList}
ExecutePermission: {<IPermission class="System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" AllowBlankPassword="False"> <add KeyRestrictions="" KeyRestrictionBehavior="AllowOnly"/> </IPermission> }
FireInfoMessageEventOnUserErrors: False
ForceNewConnection: False
HasLocalTransaction: {"Invalid operation. The connection is closed."}
HasLocalTransactionFromAPI: {"Invalid operation. The connection is closed."}
InnerConnection: {System.Data.ProviderBase.DbConnectionClosedNeverOpened}
IsContextConnection: False
IsKatmaiOrNewer: {"Invalid operation. The connection is closed."}
IsShiloh: {"Invalid operation. The connection is closed."}
IsYukonOrNewer: {"Invalid operation. The connection is closed."}
ObjectID: 1
PacketSize: 8000
Parser: {"Invalid operation. The connection is closed."}
PoolGroup: {System.Data.ProviderBase.DbConnectionPoolGroup}
ProviderFactory: {System.Data.SqlClient.SqlClientFactory}
ServerVersion: {"Invalid operation. The connection is closed."}
Site: Nothing
State: Closed {0}
Statistics: Nothing
StatisticsEnabled: False
TransactionBinding: ImplicitUnbind {0}
TypeSystem: Latest {2008}
TypeSystemAssemblyVersion: {10.0.0.0}
UserConnectionOptions: {System.Data.SqlClient.SqlConnectionString}
WorkstationId: "DEN"
With my limited knowledge at this point, I don't see the problem - can anyone point me in the right direction?