Solved

Visual Basic - connecting to SQL Server:  overcoming an unhandled exception occurred.

Posted on 2014-04-23
1
284 Views
Last Modified: 2014-04-23
The following code is failing me on line:  _sqlconnection.Open()

Public Function GetTables() As DataSet
        _sqlconnection = New SqlConnection(ConfigurationManager.ConnectionStrings(BlockConStr))
        Dim ds As DataSet = New DataSet()

        Try
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "GetBlockandFiles"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = _sqlconnection
            If _sqlconnection.State = ConnectionState.Closed Then
                _sqlconnection.Open()
            End If
            Dim _sqladapter As SqlDataAdapter = New SqlDataAdapter(cmd)


            _sqladapter.Fill(ds)
            _sqlconnection.Close()
        Catch ex As Exception
            MessageBox.Show("An Unhandled exception occured : " + ex.Message.ToString())
        End Try
        Return ds

    End Function

Open in new window


here is the function for BlockConStr:

    Public Function BlockConStr()
        'Return "Server=.;Database=BlocksCycle; User Id=sa;Pwd=123456;connection timeout = 1"
        Return "Server=10.0.1.72\SQLEXPRESS;Database=BlocksCycle;User Id=BlocksCycle;Pwd=bl0ckCycle;connection timeout = 1"
    End Function

Open in new window


I'm interested in getting exact code in order to overcome the following error:

An Unhandled exception occurred:  The ConnectionString property has not been initialized.  (please note error attachment)

This was originally working through app.config file and was told I need to avoid using that since the end result is a plugin.

Thank you in advanced.
error.PNG
0
Comment
Question by:stephenlecomptejr
1 Comment
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
you are not using a config file.

replace:
_sqlconnection = New SqlConnection(ConfigurationManager.ConnectionStrings(BlockConStr)

with:
_sqlconnection = New SqlConnection(BlockConStr)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now