Solved

read connection string given in xml file

Posted on 2013-10-23
3
330 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 55
execute SQL Stored Procedure 1000 times from vb.net 3 33
ASP.NET MVC 2 31
How do you do a one to many list in .NET CORE? 2 24
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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 …

786 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