Results 1 to 7 of 7

Thread: Using Connection String from App.Config

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Using Connection String from App.Config

    For the very first time I am using a SQL file that is not actualy from the server. I am using a local sql datbase. I have added the connection string to my app.config file but now I am not sure how to use it in code

    my config string is named MateCare
    VB Code:
    1. Private Sub AddPatient()
    2.         Dim con As String = ConfigurationSettings.AppSettings("MateCare")
    3.         Dim strSp As String = "InsertPatient"
    4.         Dim cn As SqlConnection = New SqlConnection
    5.         Dim cmd As SqlCommand = New SqlCommand(strSp, cn)
    6.         cn.ConnectionString = con
    7.         Try
    8.             cn.Open()
    9.             With cmd
    10.                 .CommandType = CommandType.StoredProcedure
    11.                 .Parameters.Add("PatientID", _
    12.                     SqlDbType.Int).Direction = ParameterDirection.Input
    13.                 .Parameters("PatientID").Value = txtPatientID.Text
    14.             End With
    15.         Catch ex As Exception
    16.             MessageBox.Show(ex.Message & vbNewLine & _
    17.             ex.Source, "ExecuteStartStop Dates", MessageBoxButtons.OK)
    18.         End Try
    19.     End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using Connection String from App.Config

    You should add your connection string on the Settings tab of the project properties. You can then access it via My.Settings. You might like to follow the Protected Configuration link in my signature for an example of storing a connection string in My.Settings and also accessing the config file in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Using Connection String from App.Config

    Hey,

    The first thing that I noticed was that you are using the ConfigurationSettings class, as of .Net 2.0, you should really be using the ConfigurationManager class. This includes the ability to get to the ConnectionStrings collection in your configuration file using ConfigurationManager.ConnectionStrings, for instance:

    Code:
    Dim MySettings As ConnectionStringSettings = _
        ConfigurationManager.ConnectionStrings("AdventureWorksString")
    If Not MySettings Is Nothing Then
        Dim MyConnection As New SqlConnection(MySettings.ConnectionString)
        Console.WriteLine(MySettings.ConnectionString)
    End If
    Hope this helps!!

    Gary

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Using Connection String from App.Config

    I am really lost. I did add the Connection String in the Project Settings and I also followed your link about encrypting the string but I still unsure of how to use the connection string in my block of code.

    gep when I tried that I get a Error Type 'ConnectionStringSettings' is not defined.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Using Connection String from App.Config

    There's not that much code in that project. I already said in my previous post that you access it via My.Settings and the only place that that's done in my code, which you could have found with a Find In Files, is here:
    Code:
    'Load the current connection string.
    Dim builder As New SqlConnectionStringBuilder(My.Settings.PrimaryConnectionString)
    That IS your connection string.

    That said, you're making an attempt to get the connection string in your original code but you never use the value you get. It should be obvious that the connection string relates to the SqlConnection so if you don't know how to put them together the logical thing to do SHOULD be to go to the MSDN documentation and read about the SqlConnection class. Alternatively, just about every ADO.NET example or tutorial, such as the Data Access link in my signature, will show you how to use a connection string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Using Connection String from App.Config

    Thanks I'll keep working on it.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Using Connection String from App.Config

    Quote Originally Posted by FastEddie
    I am really lost. I did add the Connection String in the Project Settings and I also followed your link about encrypting the string but I still unsure of how to use the connection string in my block of code.

    gep when I tried that I get a Error Type 'ConnectionStringSettings' is not defined.
    To clarify, the sample that I posted will work if you include a reference in your project to System.Configuration, and then add an Imports System.Configuration at the top of your code file.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width