Inserting into Access Database?

Results 1 to 2 of 2

Thread: Inserting into Access Database?

  1. #1
    Join Date
    Dec 1969

    Default Inserting into Access Database?

    Hi,<BR><BR>i am inserting data into Access database but i am having problems. i am always getting "Database Error" as defined in code below but dont know whats there anyother easier way to insert data into access database. <BR><BR>my insert function is as below. Please help<BR><BR>******************************<BR><BR> Function MyInsertMethod(ByVal id As Integer, ByVal name As String) As Integer<BR> Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:Inetpubwwwr"& _<BR>"ootproproject1db1.mdb"<BR><BR> Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )<BR> <BR> Dim queryString As String = "INSERT INTO [student] ([id], [name]) VALUES (@id, @name)"<BR> Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand<BR> dbCommand.CommandText = queryString<BR> dbCommand.Connection = dbConnection<BR><BR> Dim dbParam_id As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter<BR> dbParam_id.ParameterName = "@id"<BR> dbParam_id.Value = id<BR> dbParam_id.DbType = System.Data.DbType.Int32<BR> dbCommand.Parameters.Add(dbParam_id)<BR> Dim dbParam_name As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter<BR> dbParam_name.ParameterName = "@name"<BR> dbParam_name.Value = name<BR> dbParam_name.DbType = System.Data.DbType.String<BR> dbCommand.Parameters.Add(dbParam_name)<BR><BR> Dim rowsAffected As Integer = 0<BR> dbConnection.Open<BR> Try<BR> rowsAffected = dbCommand.ExecuteNonQuery<BR> Catch<BR> msgLabel.Text = "Database Error"<BR> msgLabel.Visible = true<BR> Finally<BR> dbConnection.Close<BR> End Try<BR><BR> Return rowsAffected<BR> End Function<BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Inserting into Access Database?

    In order to get the exact error message add this bit into your Page directive as shown below ...<BR><BR>&#060;%@ Page language="C#" codebehind="WebForm1.aspx.cs" Inherits ="YourApp.WebForm1" Trace="true" %&#062;<BR><BR>So you are just adding Trace = "true"<BR><BR>In your codebehind page <BR><BR>Add trace messages where ever you want like this ...<BR>Trace.Write("XXXX");<BR><BR>In your catch exception add something like this ...<BR><BR> catch(SqlException se)<BR> {<BR> Trace.Write(se.Message);<BR> Trace.Write((se.Number).ToString());<BR> Trace.Write(se.ToString());<BR> }<BR><BR>I am sorry for giving C# code.<BR><BR>The idea is same i guess. <BR><BR>You will get the exact error and then you can easily figure what&#039;s wrong.<BR><BR>The trace log will then appear at the bottom of your page itself. You can then remove the trace later on after you find what&#039;s wrong with your code.<BR><BR>I too have started learning dotNET. Hope this helps.<BR><BR>cheers,<BR>Sarav

Posting Permissions

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