Solved

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

Posted on 2014-04-23
1
291 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
[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
1 Comment
 
LVL 70

Accepted Solution

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

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

with:
_sqlconnection = New SqlConnection(BlockConStr)
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

732 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