locked
Displaying data from Access DB in Textboxes RRS feed

  • Question

  • User904890884 posted

    I'm new to Visual web developer and am trying to display data from an access database in simple textboxes. I dont want to use gridview/detailsview. I'm assuming that when I can display data it should be easy to use the same code for updating database, by changing SQL statement. The database holds information on conferences. I have some code attached to a button but it doesnt post the data back: 

    Imports System.Data
    Imports System.Data.OleDb
    
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim adoCon As Object         'Holds the Database Connection Object
            Dim recordset As Object
            'Holds the recordset for the records in the database
            Dim strSQL As Object         'Holds the SQL query to query the database
    
            'Create an ADO connection object
            adoCon = Server.CreateObject("ADODB.Connection")
            'Set an active connection to the Connection object using a DSN-less connection
            adoCon.Open(" Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & Server.MapPath("sampleattempt.mdb"))
            'Create an ADO recordset object
            recordset = Server.CreateObject("ADODB.Recordset")
    
            'Initialise the strSQL variable with an SQL statement to query the database
    
            strSQL = "Select Description From ConferenceList Where Name = 'Economic Conference'"
    
            'Open the recordset with the SQL query
            recordset.Open(strSQL, adoCon)
            'Loop through the recordset
    
    
            'Write the HTML to display the current record in the recordset
    
            TextBox1.Text = recordset.ToString()
    
            'Reset server objects
            recordset.Close()
            recordset = Nothing
            adoCon = Nothing
        End Sub
    End Class

     

    When i run this "ADODB.RecordsetClass" is displayed in the textbox

    I'm not sure if this is the easiest way of doing things so i may be on a totally wrong track.

    Any help would be appreciated, Thanks

    Monday, January 12, 2009 11:21 AM

Answers

  • User-1171043462 posted

    use ADO.Net in an easy way See Sample Code here

    http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/UseOleDbCommandtoloaddatafromAccessdatabasemdb.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 12, 2009 12:16 PM
  • User-821857111 posted

    Here's a VB sample that will work with ASP.NET:

    Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & Server.MapPath("sampleattempt.mdb")
    Dim SqlString As String = "Select Description From ConferenceList Where Name = 'Economic Conference'"
    Using conn As New OleDbConnection(ConnString)
      Using cmd As New OleDbCommand(SqlString, conn)
        conn.Open()
        Using reader As OleDbDataReader = cmd.ExecuteReader()
          While reader.Read()
            TextBox1.Text = reader("Description").ToString()
          End While
        End Using
      End Using
    End Using
    

    In this case, you know that filter criteria is Name = 'Economic Conference'. However. most often, you will be selecting records based on a value that the user supplies from a DropDownList or similar. In that case, you need to work with parameters. Here's an article that will help you with that:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=26

    There are also some DataControls that should help you with Access:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=78

    I'm not sure where you got your original sample from, but don't trust it any further as a source of knowledge.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 12, 2009 2:51 PM

All replies

  • User-1171043462 posted

    use ADO.Net in an easy way See Sample Code here

    http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/UseOleDbCommandtoloaddatafromAccessdatabasemdb.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 12, 2009 12:16 PM
  • User-821857111 posted

    Here's a VB sample that will work with ASP.NET:

    Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & Server.MapPath("sampleattempt.mdb")
    Dim SqlString As String = "Select Description From ConferenceList Where Name = 'Economic Conference'"
    Using conn As New OleDbConnection(ConnString)
      Using cmd As New OleDbCommand(SqlString, conn)
        conn.Open()
        Using reader As OleDbDataReader = cmd.ExecuteReader()
          While reader.Read()
            TextBox1.Text = reader("Description").ToString()
          End While
        End Using
      End Using
    End Using
    

    In this case, you know that filter criteria is Name = 'Economic Conference'. However. most often, you will be selecting records based on a value that the user supplies from a DropDownList or similar. In that case, you need to work with parameters. Here's an article that will help you with that:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=26

    There are also some DataControls that should help you with Access:

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=78

    I'm not sure where you got your original sample from, but don't trust it any further as a source of knowledge.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 12, 2009 2:51 PM