Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - ASP.Net Page Using SQL Server
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

ASP.Net Page Using SQL Server

 Post Reply Post Reply Page  123>
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: ASP.Net Page Using SQL Server
    Posted: 29 September 2004 at 1:20pm

I would like to create an ASP.Net Page that uses a SQL Server database. I already know how to connect to a SQL Server database from an ASP page.

How would you convert the following code to work with ASP.Net:

     dim connString

      dim serverName, userName, password

     

      'Set connection values

      serverName = "servername;"

      databaseName = databasename & ";"

      userName = "username;"

      password = "password;"

     

      'Build connection string

  connString = "PROVIDER=SQLOLEDB; " & _

                            "SERVER=" & serverName & _

                            "UID=" & userName & _

                            "PWD=" & password & _

                            "DATABASE=" & databaseName & ";"

                                 

      'Return connection string

      BuildSQLServerConnectionString = connString

 

Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2004 at 1:43pm
   Dim servername, username, password, databasename As String
        servername = "myserver"
        username = "sa"
        password = "mypassword"
        databasename = "mydatabase"
        Dim conString As String
        conString = conString.Format("server={0};User ID={1};Password={2};Database={3};", servername, username, password, databasename)
        Dim objSQLCon As New System.Data.SQLClient.SqlConnection(conString)
        Return objSQLCon
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 29 September 2004 at 1:55pm
Sorry that is of course assuming it is a function like
Function SQLConnection(servername,username,password,database) as SQLConnection
....
End Function

and you take the Dim's out
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2004 at 12:49am

Michael,

Thank you for the information! I have a problem because I am not sure how to put this on the following ASP.Net page. I have tried using the function you have provided me with, but I have been unsuccessful due to OleDbConnection() and OleDbCommand()

. I am new at using SQL Server on an ASP.net page. Please look at the code below:

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>


'---------------------------------------------
' name: BindDataGrid()
'---------------------------------------------
Sub BindDataGrid()

        Dim strConnect As String
        Dim objConnect As New OleDbConnection()
        Dim objCommand As New OleDbCommand()
        Dim strSQL as String
        Dim dtaMusic As New OleDbDataAdapter()
        Dim dtsMusic As New DataSet()

        'Build connection object
        strConnect  
  (I am not sure what I should do).
  
        objConnect.ConnectionString = strConnect
        objConnect.Open()

        'Start SQL statement
        strSQL = "Select * From Spa2

        'Add Order By
        strSQL = strSQL & " Order By SpaStoreName"

        Trace.Warn ("strSQL = " & strSQL)

        'Set the Command Object properties
        objCommand.Connection = objConnect
        objCommand.CommandType = CommandType.Text
        objCommand.CommandText = strSQL

        'Point the DataAdapter object at a Command object to work with
        dtaMusic.SelectCommand = objCommand

        'Get the data from the database and
        'put it into a DataTable object named dttMusic in the DataSet object
        dtaMusic.Fill(dtsMusic, "dttMusic")

        'Bind the DataGrid
        dtgMusic.DataSource = dtsMusic
        dtgMusic.DataBind()

End Sub

Can someone please help me figure out how to use this with SQL Server?

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2004 at 2:11am
I can't see why that code would not work (without the bold areas)..

1 thing, you could do it easier by


Dim da As New SqlDataAdapter("Select * From Spa2 Order By SpaStoreName", strConnect)
Dim ds As New DataSet

da.Fill(ds)

dtgMusic.Datasource = ds
dtgMusic.DataBind()

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2004 at 2:12am
btw you have to put the connection string in where I wrote strConnect
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2004 at 9:18am
also, as you are using sql there is no reason to use oledbcommand, use sqlcommand,sqlconnection and sqldataadapter....
Back to Top
Diep-Vriezer View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 August 2003
Location: Netherlands
Status: Offline
Points: 831
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diep-Vriezer Quote  Post ReplyReply Direct Link To This Post Posted: 30 September 2004 at 9:55am

You missed a "

>> strSQL = "Select * From Spa2

Gone..
Back to Top
 Post Reply Post Reply Page  123>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.