ZoomCities IT Community Webmaster Forum

Full Version: ASP.NET page connecting to SQL SERVER
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Introduction: My provider is GoDaddy, I built a database there, and I wanted a file (as simple as possible) that would access it so we could start making ASP.NET pages linked to databases.

Since I don't run the server, I can't go in and change things (plus I'm learning this) so one objective was to have everything on one page.  I expect any future discuss to mention there are drawbacks to this--please consider this just a "starter".

History: Two days digging through tutorials, learning things, a few hours ago I found something for Microsoft Access but another page showed comparisons so I took the gamble, to convert it over--and it worked.

However, a few weeks ago I learned that PHP pages that worked were not necessarily the best (in terms of safety and/or efficiency). 

Comment about not all queries working deleted--it was fixed the next morning.

Here's the code, in case another newbie wants to try it, or if a highly skilled expert can look at it and immediately see opportunities for improvement:

(it isn't PHP, it's ASP.NET but I'm thinking you'll want the colors)

PHP Code:
<%@ Import Namespace="System.Data.SqlClient" %>

<
script  runat="server">
sub Page_Load
dim dbconn
,sql,dbcomm,dbread
dbconn
=New SqlConnection("Server=somename.secureserver.net; uid=username; pwd=password; database=DB_123456")
dbconn.Open()
sql="SELECT * FROM separation ORDER BY last_name ASC"
dbcomm=New SqlCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
actors.DataSource=dbread
actors
.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="actors" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr bgcolor="#b0c4de">
<th>First Name</th>
<th>Last Name</th>
<th>Movie</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#Container.DataItem("first_name")%> </td>
<td><%#Container.DataItem("last_name")%> </td>
<td><%#Container.DataItem("movie")%> </td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</body> 



I'll be coming through later and adding comments on the various types of "things" above.  I'm not an expert so this shouldn't be considered a tutorial.  I'm somewhat assuming if this is new to you, you may be coming here with some knowledge of PHP, so some comments will be "compare and contrast" to PHP.

Namespace="System.Data.SqlClient"

Reference: http://msdn2.microsoft.com/en-us/library...lient.aspx

Initially I used this to identify if a page was coded for SQL Server, or something else like Microsoft Access or Oracle.

<form runat="server"> </form>

So far I've only seen this with "server" (tried Googling to get an alternate)

sub Page_Load    or    sub  end sub

Still looking to find something that talks specifically about this.

dim

used to declare variables (as int, single, double)
Kind of begs the question, how come the code doesn't say what dbconn,sql,dbcomm,dbread are supposed to be--unless we find out later that they default to something if nothing is said

caution: there are two similarly named variables: dbconn, dbcomm

In MySQL, making a connection required two commands: mysql_connect($server, $username, $password); mysql_select_db($database_name);

The same four entities are used in one command, New SqlConnection.

sql is a query (at this point I started noticing the lack of a $ sign for string.)

I kind of don't want to say it (in case I'm wording it wrong) but it looks like dbread is something that opens something, because a few lines later it was necessary close it.

Exclamation My reading so far suggests the need to deliberately close a database.  A few weeks ago it was mentioned that with PHP and MySQL, a connection only had to be closed if it was a persistent connection.

The component actors in two commands comes from the id in the <asp: repeater

databinding-
http://www.akadia.com/services/dotnet_da...l#Overview

asp:repeater


(Later) One of the reasons I wanted this to work from a single page, was that some of the things I read (like: go in and change file X) might be things someone who depended on a provider couldn't do.  Tutorials kept refering to a web.config file.  I hadn't seen one on my site, so I thought maybe I didn't have access to it.  However, while working on a different problem today, I found that it was a simple matter of going to the root director and adding a web.config file (edited using text editor and being sure to save it using "save as type: all files").  I haven't tried using web.config to store my connection information, but this is fairly close to the top of my list of priorities.

- - -
(version 2)

It was suggested that I use Gridview instead of the above HTML:

My reference for GridView: http://aspnet101.com/aspnet101/tutorials.aspx?id=51

And an .aspx page with it that worked:



Code:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:GridView id="MyGridView" DataSourceID="MyDataSource1" Runat="Server"/>

<asp:SqlDataSource ID="MyDataSource1" runat="server"
  ConnectionString="Server=something.secureserver.net;uid=username;pwd=password;database=DB_123456"
  ProviderName="System.Data.SqlClient"
  SelectCommand="SELECT * FROM tablename">
</asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>[/code

]


General reference:

http://www.newobjects.com/pages/ndl/alp/...ucture.htm

ldechent Wrote:
Namespace="System.Data.SqlClient"

Reference: http://msdn2.microsoft.com/en-us/library...lient.aspx


This just says that you want to use the Sql libraries, which allows you to declare dbconn and dbcomm

ldechent Wrote:
Initially I used this to identify if a page was coded for SQL Server, or something else like Microsoft Access or Oracle.

I'm pretty sure it is for SQL Server. Orcale can use either OLEDB or it's own which is pretty similar (OracleDataAdaper, OracleCommand, etc)

ldechent Wrote:
<form runat="server"> </form>

So far I've only seen this with "server" (tried Googling to get an alternate)

It can only be with "server." This just tells the compiler that you want to execute it on the server side. All <asp: XXX> tags should be placed INSIDE the form, and there can only be one form tag on the page.

Anything else like <asp:Textbox /> must also have a runat="server" attribute, or it won't work. This is a common cause of bugs. Essentially, you are telling the compiler that you want to use the textbox as a part of your server side controls. Without this, .net won't know that it exists, and it will just turn into a normal html textbox. You will not be able to manipulate it from the server.

ldechent Wrote:
sub Page_Load or sub end sub

Still looking to find something that talks specifically about this.


The Page_Load function is executed when the page loads, so any calls to other functions should generally be done in here. It's kind of like the main() function in C++.

Sub/End Sub just denotes the start and end of a function in VB.

Code:
Sub SquareIt
End Sub


Same thing as this in C#/Java/C++

Code:
void SquareIt()
{

}

ldechent Wrote:
sql is a query (at this point I started noticing the lack of a $ sign for string.)

The $ is only for perl/similar languages. In VB, it isn't required that you declare the type of the variable. In C# you do, but you just say:

Code:
string varname;



ldechent Wrote:
I kind of don't want to say it (in case I'm wording it wrong) but it looks like dbread is something that opens something, because a few lines later it was necessary close it.

Exclamation My reading so far suggests the need to deliberately close a database. A few weeks ago it was mentioned that with PHP and MySQL, a connection only had to be closed if it was a persistent connection.

dbread is just one way of reading from the database. You can either read it line by line (which is kind of what DBRead does), or fill it into a dataset and bind it to a repeater or a gridview.

ldechent Wrote:
The component actors in two commands comes from the id in the <asp: repeater

databinding-
http://www.akadia.com/services/dotnet_da...l#Overview

asp:repeater


Yea, you forgot the most important one, but you mentioned it in your second post. GridView/DataGrid is probably the most handy component i've ever seen Smile

Macrelay:

Thanks for the enhancement! :-)

I've made some progress since working with that. I'm doing a lot more drag and drop with Visual Web Developer 2005 Express Edition.

I'll be putting up some questions soon to try to expand further into this.

-Larry
No prob there Smile I hope I got everything accurately, its been a little while since I programmed in .net Tongue
Visual Studio 2005 ftw !!!

alternatively to extract data from SQL

you can use

dim dataAdapter As New SqlDataAdapter(sql)
dim result As New Data.DataSet // or DataSet if ur using VS2003

dbconn.Open()
dataAdapter.Fill(result,"TableName")
dbconn.Close()

then you can extract info from the DataSets

like ...

result.Tables("TableName").Items(0)..... etc, i think for VS2003 its Rows instead of Items.
Yea, those rows/cols/items things really screw me up. Thats what intellisense is for Smile
Reference URL's