Print Page | Close Window

ASP.Net Page Using SQL Server

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=11988
Printed Date: 29 March 2026 at 7:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: ASP.Net Page Using SQL Server
Posted By: Misty
Subject: ASP.Net Page Using SQL Server
Date 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

 




Replies:
Posted By: michael
Date 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

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date 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

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date 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?



Posted By: Mart
Date 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()



Posted By: Mart
Date Posted: 30 September 2004 at 2:12am
btw you have to put the connection string in where I wrote strConnect


Posted By: michael
Date 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....

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Diep-Vriezer
Date Posted: 30 September 2004 at 9:55am

You missed a "

>> strSQL = "Select * From Spa2



-------------
Gone..


Posted By: Misty
Date Posted: 30 September 2004 at 12:58pm

I am really new at using SQL Server with an ASP.Net page. I am confused. I think I understand Michael's code better. But I still cannot get it to work. I am getting the following error now:

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 4:  
Line 5:  <%
Line 6:  Function SQLConnection(servername,username,password,database) as SQLConnection 

I don't understand about SQLCommand because I noticed that it was not defined in Michael's code.

Here's my code now:

<%
Function SQLConnection(servername,username,password,database) as SQLConnection

    servername = "myservername"
        username = "username"
        password = "password"
        databasename = "dbname"
        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

 

        Dim dtaMusic As New OleDbDataAdapter()
        Dim dtsMusic As New DataSet()
        Dim StrSQL As String

         
       SQLConnection.ConnectionString = conString
       SQLConnection.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
        SQLCommand.Connection = SQLConnect
        SQLCommand.CommandType = CommandType.Text
        SQLCommand.CommandText = strSQL

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

        '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 Function

 

 



Posted By: michael
Date Posted: 30 September 2004 at 3:05pm
Seems you are not doing it in code behind so you have to put it within the server code tags of your form. I'd put it in Codebehind though.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Misty
Date Posted: 30 September 2004 at 4:27pm

Do you mean for me to put it between the <Form> tags? I know how to use ASP.Net with an Access database. I'm just lost on how to use it with SQL Server.

I have an ASP Page that retrieves data from a SQL Server. I set up a second web page with a function. But I am still lost on ASP.net pages.



Posted By: michael
Date Posted: 30 September 2004 at 5:00pm
Unless there is a typo, this page should bind a grid the way you want.


<%@ Page Language="VB" %>
<script runat="server">

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             If Not Page.IsPostBack THEN
                BindDataGrid()
             END IF
    End Sub
    
    
    Private Sub BindDataGrid()
    
             Dim strConnect As String
             Dim objConnect As New System.Data.SqlClient.SQLConnection(ConnectionString())
             Dim objCommand As New System.Data.SqlClient.SQLCommand
             Dim strSQL as String
             Dim dtaMusic As New System.Data.SqlClient.SQLDataAdapter()
             Dim dtsMusic As New DataSet()
    
             objConnect.Open()
    
             'Start SQL statement
             strSQL = "Select * From Spa2"
    
             'Add Order By
             strSQL = strSQL & " Order By SpaStoreName"
    
    
             '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
    
    Private Function ConnectionString () as String
    
        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)
        Return conString
    
    End Function

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:DataGrid id="dtgMusic" runat="server"></asp:DataGrid>
    </form>
</body>
</html>


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Mart
Date Posted: 30 September 2004 at 5:07pm
eek. thats a lot of code for such a simple thing


Posted By: Misty
Date Posted: 30 September 2004 at 7:23pm

Michael,

The code that you gave me in the last posting worked. Thank you very much for your help! I have a better idea of how an ASP.Net page works with SQL Server.



Posted By: Amateur
Date Posted: 01 October 2004 at 9:00am
Anyone know of a tutorial or website which details how ASP.NET and SQL SERVER interact, code examples etc?


Posted By: michael
Date Posted: 01 October 2004 at 9:19am
Originally posted by Mart Mart wrote:

eek. thats a lot of code for such a simple thing

I know that but it makes it easier for him to visualize what is going on.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 01 October 2004 at 9:21am
Originally posted by Amateur Amateur wrote:

Anyone know of a tutorial or website which details how ASP.NET and SQL SERVER interact, code examples etc?


How they interact? If you look at above code they are interacting. What goes on behind the scenes is a little different but there are no code samples, just diagrams.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net