Solved

vs 2010, populate cbo box... error

Posted on 2013-10-24
4
202 Views
Last Modified: 2013-11-07
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.SqlException' 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.

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

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:ShareD_Point
ID: 39598059
Hi,

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.DataSource = dt
            Me.cobSelectCust.DisplayMember = "Costomer"
            Me.cobSelectCust.ValueMember = "CustomerID"

  Catch ex As SqlException
            MessageBox.Show(ex.Message.ToString)
       
  Catch ex1 As Exception
            MessageBox.Show(ex1.Message.ToString)
   End Try

Hope this Helps
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39598060
What is the actual error message associated with the exception? A SqlException can occur for many reasons.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39598174
Thanks for the posts, Using Try/ Catch like:
  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

Open in new window


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.SqlConnection.ConnectionString.set'

Upon execution of conWS.Open(), it jumps to Catch ex As SqlException telling:
Step into: Stepping over non-user code 'System.Data.SqlClient.SqlConnection.Open'
A first chance exception of type 'System.Data.SqlClient.SqlException' 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\Practice\Unit05\WindowsApplication1\WindowsApplication1\bin\Debug\WSCGSoftware.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
0
 
LVL 4

Accepted Solution

by:
ShareD_Point earned 500 total points
ID: 39598442
Can you try using the below connection string instead of what you have already.

Dim conStr As String = "Data Source=.\SQLEXPRESS; Integrated Security=SSPI; Initial Catalog= WSCGSoftware;"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question