Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. reading and writing to a database?

reading and writing to a database?

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netcomsysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jamesbronw
    wrote on last edited by
    #1

    Ok im new to asp.net and im having trouble trying to populate a label with info from my database. Here is the code i have Imports System.Data.OleDb Module Module1 Public myconn As OleDbConnection Public mycommand As OleDbCommand Public myreader As OleDbDataReader Public gErrorOccurred As Boolean Public p Public strconn As String = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=D:\Documents and Settings\Tommy's\Desktop\heritagecove.com\db\dynamic.mdb;Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False" Public Sub createreader(ByVal SQL As String) gErrorOccurred = False myconn = New OleDbConnection(strconn) mycommand = myconn.CreateCommand mycommand.CommandText = SQL Try myconn.Open() Catch ex As Exception MsgBox("The database is inaccessible. Please verify the Server is accessible and try again.", MsgBoxStyle.Critical, "Server Unavailable") gErrorOccurred = True Exit Sub End Try Try myreader = mycommand.ExecuteReader Catch ex As Exception MsgBox("The data source is unable to process the request. Please notify the system administrator." & vbCrLf & SQL, MsgBoxStyle.Critical, "SQL Error") gErrorOccurred = True Exit Sub End Try End Sub Public Sub closereader() myreader.Close() myconn.Close() End Sub Public Sub executecommand(ByVal sql As String) Dim cmd As New OleDbCommand(sql, New OleDbConnection(strconn)) Try cmd.Connection.Open() cmd.ExecuteNonQuery() cmd.Connection.Close() Catch ex As Exception 'MsgBox(ex.Message, MsgBoxStyle.OKOnly) End Try Here is the code im writing to try to get info from the database. Dim sql As String Dim myds As New DataSet sql = "SELECT FileName from Gallery Where ID IS NOT NULL" createreader(sql) Do While myreader.Read Label1.Text = myreader.G

    M 1 Reply Last reply
    0
    • J jamesbronw

      Ok im new to asp.net and im having trouble trying to populate a label with info from my database. Here is the code i have Imports System.Data.OleDb Module Module1 Public myconn As OleDbConnection Public mycommand As OleDbCommand Public myreader As OleDbDataReader Public gErrorOccurred As Boolean Public p Public strconn As String = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=D:\Documents and Settings\Tommy's\Desktop\heritagecove.com\db\dynamic.mdb;Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False" Public Sub createreader(ByVal SQL As String) gErrorOccurred = False myconn = New OleDbConnection(strconn) mycommand = myconn.CreateCommand mycommand.CommandText = SQL Try myconn.Open() Catch ex As Exception MsgBox("The database is inaccessible. Please verify the Server is accessible and try again.", MsgBoxStyle.Critical, "Server Unavailable") gErrorOccurred = True Exit Sub End Try Try myreader = mycommand.ExecuteReader Catch ex As Exception MsgBox("The data source is unable to process the request. Please notify the system administrator." & vbCrLf & SQL, MsgBoxStyle.Critical, "SQL Error") gErrorOccurred = True Exit Sub End Try End Sub Public Sub closereader() myreader.Close() myconn.Close() End Sub Public Sub executecommand(ByVal sql As String) Dim cmd As New OleDbCommand(sql, New OleDbConnection(strconn)) Try cmd.Connection.Open() cmd.ExecuteNonQuery() cmd.Connection.Close() Catch ex As Exception 'MsgBox(ex.Message, MsgBoxStyle.OKOnly) End Try Here is the code im writing to try to get info from the database. Dim sql As String Dim myds As New DataSet sql = "SELECT FileName from Gallery Where ID IS NOT NULL" createreader(sql) Do While myreader.Read Label1.Text = myreader.G

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi there. For starters, if you are executing this code in the context of an ASP.NET application, you can't use the MsgBox() function. That would be appropriate in a Windows Forms application, but not for ASP.NET. You would more likely want to let ASP.NET render the exception details to the browser, say, by setting a Label.Text value to the Exception.ToString(), or execute a Response.Write(ex.ToString()), or a Trace.Write(ex.ToString()), or something like that. There are a bunch of reasons why your database code may not be working - it's common when working with an Access database for example to run across permissions issues (the ASP.NET user not only needs access to the .mdb file, but must also have write access to the .mdb's containing folder). But until you know the specific details of the exception (and possibly details of the exception's InnerException property), it will be tough to tell.

      J 1 Reply Last reply
      0
      • M Mike Ellison

        Hi there. For starters, if you are executing this code in the context of an ASP.NET application, you can't use the MsgBox() function. That would be appropriate in a Windows Forms application, but not for ASP.NET. You would more likely want to let ASP.NET render the exception details to the browser, say, by setting a Label.Text value to the Exception.ToString(), or execute a Response.Write(ex.ToString()), or a Trace.Write(ex.ToString()), or something like that. There are a bunch of reasons why your database code may not be working - it's common when working with an Access database for example to run across permissions issues (the ASP.NET user not only needs access to the .mdb file, but must also have write access to the .mdb's containing folder). But until you know the specific details of the exception (and possibly details of the exception's InnerException property), it will be tough to tell.

        J Offline
        J Offline
        jamesbronw
        wrote on last edited by
        #3

        It seems to be getting errored out at the myreader.read and or the myreader.getvalue(0) any ideas anyone?

        M 1 Reply Last reply
        0
        • J jamesbronw

          It seems to be getting errored out at the myreader.read and or the myreader.getvalue(0) any ideas anyone?

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          But what are the details of the exception? It's good to know that the error is happening on a particular line, but what is the specfic exception you're getting?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups