Solved

read connection string given in xml file

Posted on 2013-10-23
3
336 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
[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
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 63

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 34

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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