Solved

read connection string given in xml file

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

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:…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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