?
Solved

read connection string given in xml file

Posted on 2013-10-23
3
Medium Priority
?
337 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 1000 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 1000 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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