Results 1 to 6 of 6

Thread: ASP.NET and Access databases

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323

    ASP.NET and Access databases

    I have taken the tutorials within vbworld regarding .NET and I love it so far. I would like to start using it today instead of the ASP I am currently using but I don't know how to use databases yet.

    How would I write to a database (Access or SQL)? I know how to gather the data on the page but I don't know how to move it into a database. rst.Open....?

    I never got into VB enough to get into databases either so sample code would be greatly appreciated.

    Thank you.
    If you think education is expensive, try ignorance.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    See if this helps. Follow the Add_Click sub to see how to add to a databas using the Insert SQL command

    VB Code:
    1. ' ---------------------------------
    2. ' Code by Chris Andersen(AKA Cander)
    3. ' This example shows how to show data from an access database in the DataGrid control
    4. ' and how to write a new record to the database.
    5. ' ---------------------------------
    6.  
    7. ' Using Code Behind, we can fully seperate presentation from code.
    8. Imports System
    9. Imports System.Data
    10. Imports System.Data.OleDb
    11. Imports System.Web.UI
    12. Imports System.Web.UI.WebControls
    13.  
    14. Public Class Cander1
    15.     ' Declare the class and have it inherit the System.Web.UI.Page class. This is needed for
    16.     ' accessing the ASP.NET Page's Events/Properties/Methods
    17.     Inherits Page
    18.    
    19.     ' We need to declare all web controls we use. Since the aspx page will inherit this class, they
    20.     ' must be Public in order to be visible.
    21.     Public lblName As Label
    22.     Public lblAge As Label
    23.     Public MyDataGrid As DataGrid
    24.     Public tbName As TextBox
    25.     Public tbAge As TextBox
    26.     Public btnAdd As Button
    27.     Public btnView As Button
    28.     Dim myDS As New DataSet
    29.     Dim myConn As OleDbConnection
    30.     Dim myCommand As OleDbDataAdapter
    31.     Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.Mappath("/candersen/db/test.mdb")
    32.     Dim strSQL As String
    33.  
    34.     Sub Page_Load(Sender As Object, E As EventArgs)
    35.         ' This sub is called everytime the Page Loads
    36.     End Sub
    37.  
    38.     Sub Add_Click(Sender As Object, E As EventArgs)
    39.         Dim strName As String = Name.Text
    40.         Dim strAge As String = Age.Text
    41.  
    42.         ' Write posted data to the lables
    43.         lblName.Text = strName
    44.         lblAge.Text = strAge
    45.  
    46.         ' Insert Posted Data to database
    47.         strSQL = "Insert Into Test(Name,Age) VALUES('" & strName & "','" & strAge & "')"
    48.  
    49.         ' Populate DataSet
    50.         FillDataSet()
    51.  
    52.         ' Update DataGrid
    53.         UpdateGrid()
    54.     End Sub
    55.  
    56.     Sub UpdateGrid()
    57.         ' Updates the DataGrid
    58.         strSQL = "Select * FROM Test"
    59.  
    60.         'Populate DataSet
    61.         FillDataSet()
    62.  
    63.         ' Now set the DataGrid's source to the Dataset and bind it
    64.         MyDataGrid.DataSource = myDS.Tables("Test").DefaultView
    65.         MyDataGrid.DataBind()
    66.  
    67.     End Sub
    68.  
    69.     Sub FillDataSet()
    70.         ' Recordset no longer exists for ADO.NET. Create DataSets and populate it from the data you get from
    71.         ' a DataAdapter or other source(like XML). In this case we are getting ADO DataSet using the System.Data.OleDb.OleDbDataAdapter
    72.                
    73.         ' Start Connection to ADO source
    74.         myConn = New OleDbConnection (strConnString)
    75.  
    76.         ' Run the Sql statement and get the Data into the DataAdapter
    77.         myCommand = New OleDbDataAdapter(strSQL, myConn)
    78.  
    79.         ' Fill the DataSet and name the table
    80.         myCommand.Fill(myDS, "Test")
    81.         myConn.Close()
    82.     End Sub
    83. End Class
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    That is what I was looking for. Thank you very much.

    This is a totally different beast than ASP isn't it? This is a lot of coding from used to take about 5 lines.
    If you think education is expensive, try ignorance.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Well im doing alot more there than just adding a record. So really its about the same amount of code to add.

    In that example is a code behind component that is inheriting the calling asp page, declaring the objects, filling a datagrid, and adding new data. It was just the only example I had handy. Just concentrate on the filldataset and add_click routines. Oh and the Imports statements

    Imports System.Data
    Imports System.Data.OleDb
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Alright, thank you.

    I will give that a try.
    If you think education is expensive, try ignorance.

  6. #6
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brisbane Australia
    Posts
    150

    Your old thread re Access DBs on a Web site

    Cander
    Wonder if you can help
    I browsed the forums to try to solve my problem and came across this thread which you contributed to which I thought might help me.
    I have been programming in Vb for 10 years but VB.NET sometimes leaves me bewildered.

    I have a web site I have developed using an Access 2002 DB. It all works fine if I hard code the actual database location from my Erive as below

    Public dbcConnection As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = e:\irean\ireanweb\ireantest.MDB;"

    as you can see the project and all the associated VB files are in e:\irean\ireanweb and that where the DB is.

    I am preparing to upload it my clients Web Server and am a little unsure of where my DB is going to sit - I assume with all the HTML and ASPX files in my clients web site.

    I changed the connection string as per your example - dont understand the system.mapppath ?? but now its looking in \inetpub\wwwroot for the DB

    Is there some advice you can give me to make this uploading as painless as possible or do you know of any resources I could scoure to help me

    thanks
    BH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width