Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. connection code in app_cofig file using windows form

connection code in app_cofig file using windows form

Scheduled Pinned Locked Moved ASP.NET
databasesysadminhelpquestionworkspace
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    h ma
    wrote on last edited by
    #1

    my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??

    $h@ma!|@

    S P 2 Replies Last reply
    0
    • H h ma

      my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??

      $h@ma!|@

      S Offline
      S Offline
      Sonia Gupta
      wrote on last edited by
      #2

      i write in the folowing manner

      Sonia Gupta Soniagupta1@yahoo.co.in Yahoo messengerId-soniagupta1 Love is Friendship and Friendship is Love....

      1 Reply Last reply
      0
      • H h ma

        my connection code is: String strConnection; strConnection = "server=localhost;User ID=sa;Password=sas;database=db"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); how will i write it in any application configuration file working in windows form,and call it on every page that needs to b connected?? can any body help me??

        $h@ma!|@

        P Offline
        P Offline
        Psycho Coder Extreme
        wrote on last edited by
        #3

        First and foremost, this is a VB.Net question (Not ASP.Net since you are using Win Forms) second in your app.config you need a < connectionStrings > < /connectionStrings > section (like below) then add your connection string as I have. The weird thing about your post is you ask how to make it work in the app.config in Windows Forms then you ask how to call it on every "page" taht needs it, Windows Forms dont have "pages". The below example will work in Windows Forms (which is what you asked). As for the 2nd part of your question, the easiest way to call it is to write a function like below:

        'This returns the connection string
        Public Shared Function GetConnectionString(ByVal strConnection As String) As String
        Dim strReturn As New String("")
        'If you have multiple connection strings then you can pass it a connection name otherwise it defaults to your default connection
        If Not String.IsNullOrEmpty(strConnection) Then
        strReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString
        Else
        strReturn = ConfigurationManager.ConnectionStrings("MyConnectionName").ConnectionString
        End If
        Return strReturn
        End Function

        Then on the form that needs a DB connection call it like this:

        Dim oConnection As New SqlConnection(YourClassName.GetConnectionString("MyConnectionName"))

        <connectionstrings>
        <add name="MyConnectionName">
        connectionString="Persist Security Info=False;Data Source=localhost;Initial Catalog=db;Integrated Security=SSPI;Trusted_Connection=TRUE;Application Name=AppName"
        providerName="System.Data.SqlClient" />
        </add>
        </connectionstrings>

        If you mis-worded your question and need this for an ASP.Net application (not Windows Forms) reply and let me know and I'll help as much as I can

        "Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups