Solved

read connection string given in xml file

Posted on 2013-10-23
3
324 Views
Last Modified: 2013-11-07
Question: What is wrong with this code that it doesn't put out the connection string given in a xml file?

   
Imports System.Data.Sqlclient
Imports System.Xml
Public Class Form1

    Dim dsWSCGSoftware As New DataSet
    Dim conWSCGSoftware As New SqlConnection
    Dim strConnectionString As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xmlReader As New XmlTextReader("csWSCGSoftware.xml")
        'strConnectionString = xmlReader.ReadElementString("connection")
        conWSCGSoftware = New SqlConnection(xmlReader.ReadElementString("connection"))

        conWSCGSoftware.Open()
        MessageBox.Show(conWSCGSoftware.ConnectionString)
        'Get_CustomerIDs()
        'Get_Products()
        conWSCGSoftware.Close()

        ' lblInvTotal.Text = "Total Invetory Value: " & FormatCurrency(Me.Total_Inventory, 2)
    End Sub
End Class

Open in new window


Here is the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<connection>
  Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\WSCGSoftware.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
</connection>

Open in new window

0
Comment
Question by:Mike Eghtebas
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 250 total points
ID: 39597355
It worked fine for me when I copied and pasted your code. Are you sure the xml file is in the same folder as the executable? By default Visual Studio doesn't copy XML files into the output folder. Check the properties of the xml file and make sure it's being copied to the output folder. Also make sure you spelled the file name correctly. If I purposely misspelled the file name the code runs without throwing an exception but nothing happens. You might want to add the following to ensure you're finding the file.

        If Not File.Exists("csWSCGSoftware.xml") Then
            Throw New FileNotFoundException("Could not find csWSCGSoftware.xml in executable directory.")
        End If

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 39597368
Hi eghtebas;

To your question, "What is wrong with this code that it doesn't put out the connection string given in a xml file?", I see nothing wrong with it and have tested your code to get the connection string and it worked for me. What is the exception and inner exception that you are getting when you run your code?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 39597937
Hi CraigWagner,
I will get back to this question to figure out and understand your description of the problem. In the mean time, I have posted a new question where I want to accomplish the same task without use of xml file at:

http://www.experts-exchange.com/Programming/Languages/.NET/Q_28276256.html

I had a similar error to:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

FYI, I have deleted the xml file and revised the application so I was not able to test and post the exact error. But, after a solution for the new question is available, I will rebuild it to includ xml file again to make sure I fully understand the concept.

Thank you,

Mike
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

13 Experts available now in Live!

Get 1:1 Help Now