Connect to a Database ( and read it )
Tutorials and code examples using ASP and ASP.Net
7 posts
Page 1 of 1
Hello,
I'm learning this @ school so why I won't share it?

First of all make a database: Named dbPersons ( I used acces 2010 ):

Then save the table as "tblPersons". Then don't just save the db but publish ( if using acces 2007 or higher ):

Then go to you're microsoft Visual Web Developer and Create a new aspx project. I named it "20110304-Codenstuff-aspxdatareader"
then u search you're db file " dbPersons" and drag it in App_Data:

U can also see I deleted some files. But that isn't necesairy. Then we are going to add a page named "Datareader.aspx":

So now the coding can begin !
Lets start with getting the messy stuff out of the page! Delete the first sententce and replace it with:

Then the connection!

The Source files Will cost some credits because I explained all ;)
RAR: ZIP: So this is it?
Thx
And rep me if helped plox 
Credits:
My teacher who taught me and my class.
I'm learning this @ school so why I won't share it?

First of all make a database: Named dbPersons ( I used acces 2010 ):

Then save the table as "tblPersons". Then don't just save the db but publish ( if using acces 2007 or higher ):

Then go to you're microsoft Visual Web Developer and Create a new aspx project. I named it "20110304-Codenstuff-aspxdatareader"
then u search you're db file " dbPersons" and drag it in App_Data:

U can also see I deleted some files. But that isn't necesairy. Then we are going to add a page named "Datareader.aspx":

So now the coding can begin !
Lets start with getting the messy stuff out of the page! Delete the first sententce and replace it with:
Code: Select all
So u'll gain this:<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
</script>

Then the connection!
Code: Select all
This will result:Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("App_Data\dbPersons.mdb")
'Specifie the Database type and path
Dim strSQL As String = "SELECT * FROM tblPersons"
'SQL Query
Dim cn As New OleDbConnection(strConn)
'Make CN ( Not open )
Try
cn.Open()
'Open CN
Dim cm As New OleDbCommand(strSQL, cn)
'make command from SQL
Dim dr As OleDbDataReader = cm.ExecuteReader()
'make Reader
If dr.HasRows() Then
' check if there are rows in the Db
While dr.Read()
'He is checking every row
Label1.Text = Label1.Text & dr("Id").ToString() & " " & _
dr("Name").ToString() & " " & _
dr("Prename").ToString() & " " & _
dr("E-Mail").ToString() & " " & _
dr("Country").ToString() & "<br>"
End While
Else
'No rows are found. Add some to the DB
Label1.Text = "No rows found."
End If
Catch ex As Exception
Trace.Warn(ex.Message)
'Problem probably you have a wrong path or SQL
Label1.Text = "Problem with database."
Finally
'Close connection ALWAYS
cn.Close()
End Try

The Source files Will cost some credits because I explained all ;)
RAR: ZIP: So this is it?
Thx


Credits:
My teacher who taught me and my class.
You do not have the required permissions to view the files attached to this post.
Last edited by Kieken72 on Sat Mar 05, 2011 3:01 pm, edited 2 times in total.
Nice tutorial, I have been waiting for this for weeks now. Finally someone shows me how to do it!
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

Very nice and detailed tutorial. +rep
btw. its called "taught"
btw. its called "taught"
Credits:should be
My teacher who learnd it to me and my class.
Credits:
My teacher who taught me and my class.
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
CodexVideos wrote:Very nice and detailed tutorial. +repSorry ^^
btw. its called "taught"
Credits:should be
My teacher who learnd it to me and my class.Credits:
My teacher who taught me and my class.
You would need to use the MySQL connector instead of an Access database, but the syntax would be very similar.
7 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023