Mike Eghtebas
asked on
vs 2010, populate cbo box... error
The attached code has a database to go with it. The combo box is not able to get filled with the necessary information.
Error: A first chance exception of type 'System.Data.SqlClient.Sql Exception' occurred in System.Data.dll
Question: How can I correct my code to make this work?
I couldn't upload the application, perhaps I could email it to you if you send an email to my user id at hotmail. Or, if you know a way so I can get rid of the objectionable extensions such as .exe, sou. etc so I can upload it here.
Thank you.
Error: A first chance exception of type 'System.Data.SqlClient.Sql
Question: How can I correct my code to make this work?
I couldn't upload the application, perhaps I could email it to you if you send an email to my user id at hotmail. Or, if you know a way so I can get rid of the objectionable extensions such as .exe, sou. etc so I can upload it here.
Thank you.
Imports System.Data.SqlClient
Imports System.Xml
Public Class frmCustomer
Dim conWS As New SqlConnection()
Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\WSCGSoftware.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Dim selectCommand As SqlCommand
'Dim selectStatement As String
Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conWS.ConnectionString = conStr
'MessageBox.Show(conWS.ConnectionString)
'selectStatement = "SELECT CustomerID, FirstName + ' ' + LastName As Costomer FROM tblCustomers ORDER BY Name;"
'selectCommand.CommandText = selectStatement
conWS.Open()
Get_CustomerIDs() 'populate comboBox
conWS.Close()
End Sub
Private Sub Get_CustomerIDs()
MessageBox.Show(conWS.ConnectionString)
Dim rdr As SqlDataReader
Dim cmd As New SqlCommand("SELECT CustomerID, FirstName + ' ' + LastName As Costomer FROM tblCustomers ORDER BY Name;")
cmd.Connection = conWS
rdr = cmd.ExecuteReader()
Dim dt As New DataTable
dt.Load(rdr)
rdr.Close()
Me.cobSelectCust.DataSource = dt
Me.cobSelectCust.DisplayMember = "Costomer"
Me.cobSelectCust.ValueMember = "CustomerID"
End Sub
End Class
What is the actual error message associated with the exception? A SqlException can occur for many reasons.
ASKER
Thanks for the posts, Using Try/ Catch like:
I get the following messages in the immediate window:
Stepping through the code, when conWS.Open() is highlighted, I get:
Step into: Stepping over non-user code 'System.Data.SqlClient.Sql Connection .Connectio nString.se t'
Upon execution of conWS.Open(), it jumps to Catch ex As SqlException telling:
Step into: Stepping over non-user code 'System.Data.SqlClient.Sql Connection .Open'
A first chance exception of type 'System.Data.SqlClient.Sql Exception' occurred in System.Data.dll
And this is the full description of the message:
An attempt to attach an auto-named database for file C:\Users\Mike\Desktop\Prac tice\Unit0 5\WindowsA pplication 1\WindowsA pplication 1\bin\Debu g\WSCGSoft ware.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
conWS.ConnectionString = conStr
'MessageBox.Show(conWS.ConnectionString)
'selectStatement = "SELECT CustomerID, FirstName + ' ' + LastName As Costomer FROM tblCustomers ORDER BY Name;"
'selectCommand.CommandText = selectStatement
conWS.Open()
Get_CustomerIDs() 'populate comboBox
conWS.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message.ToString)
Catch ex1 As Exception
MessageBox.Show(ex1.Message.ToString)
End Try
End Sub
I get the following messages in the immediate window:
Stepping through the code, when conWS.Open() is highlighted, I get:
Step into: Stepping over non-user code 'System.Data.SqlClient.Sql
Upon execution of conWS.Open(), it jumps to Catch ex As SqlException telling:
Step into: Stepping over non-user code 'System.Data.SqlClient.Sql
A first chance exception of type 'System.Data.SqlClient.Sql
And this is the full description of the message:
An attempt to attach an auto-named database for file C:\Users\Mike\Desktop\Prac
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to check the following
Correctness of your SQL query.
Connection String.
Use try - catch block and get the exact exception by debugging. Here is the updated code.
Try
cmd.Connection = conWS
conWS.Open()
rdr = cmd.ExecuteReader()
Dim dt As New DataTable
dt.Load(rdr)
rdr.Close()
Me.cobSelectCust.DataSourc
Me.cobSelectCust.DisplayMe
Me.cobSelectCust.ValueMemb
Catch ex As SqlException
MessageBox.Show(ex.Message
Catch ex1 As Exception
MessageBox.Show(ex1.Messag
End Try
Hope this Helps