• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

SQL Connection Question

I am new here, so please bare with me.  I understand what is going on in the code below.  My problem, is how do I or WHERE do I set the connection to SQL, as far as my Server and such?  This is an example from Microsoft.  I am so used to hard coding the information as in VB6, and getting used to this new stuff now.  This is to fill a datagrid

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter

        MyConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("BenchmarkingString"))
        MyCommand = New SqlDataAdapter("select * from Benchmarking", MyConnection)

        DS = New DataSet
        MyCommand.Fill(DS, "Authors")

        BenchmarkingDataGrid.DataSource = DS.Tables("Benchmarking").DefaultView
        BenchmarkingDataGrid.DataBind()

    End Sub
0
chrisryhal
Asked:
chrisryhal
  • 2
  • 2
1 Solution
 
laotzi2000Commented:
It's set in file web.config

<appSettings>
<add key="BenchmarkingString" value="here is the connection string"/>
</appSettings>
0
 
chrisryhalAuthor Commented:
Actually, there is nothing in the Web.Config like that.  Could you please show me a full example, with a fake connection string to a server.  
0
 
laotzi2000Commented:
Your web.config should be like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <appSettings>
            <add key="BenchmarkingString" value="server=.;database=CWCSReg;uid=sa;password=" />
      </appSettings>
      <system.web>
                ..............
                </system.web>
</configuration>
0
 
chrisryhalAuthor Commented:
That is great!!! See, I learn something new everyday.  So now, if I ever want to reference that connection string, I can just call upon my AppSettings.  Thanks a million

CR
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now