Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsOtherProgramming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 15th, 2005, 06:27 AM
vishal_0009 vishal_0009 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 vishal_0009 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 45 sec
Reputation Power: 0
Problem adding records from .aspx page to access database.

Hi, there guys. I need a help to insert record from .aspx page to MsAcess database.

Well, the coding for that is shown below.

I have connection class called clsConnection to get connection to the database. The coding inside this class is :

Code:
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class ClsConnection

         Dim objConn As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" + _"data source= D:/Vishal/Website1/AppData/College Mgt System.mdb")

               Public Function GetConnection() As OleDbConnection

                     Try
                           objConn.Open()
                           Catch ex As Exception
                     End Try

                           Return objConn

               End Function

End Class


Now, I've got the Student Class called clsStudent and the coding inside this class is:

Code:
Imports Microsoft.VisualBasic
Imports system
Imports System.Data
Imports System.Data.OleDb

Public Class clsStudent

            Dim conObject As OleDbConnection

                  Public Sub ClsStudent()

                           Dim conn1 As New ClsConnection()
                          conObject = conn1.GetConnection()

                  End Sub

               Public Sub setSurname(ByVal strSurname As String)

                          Dim cmd1 As String = "Insert into tempStudent(Surname) values ('" & strSurname & "')"
                          Dim objcmd As New Data.OleDb.OleDbCommand(cmd1, conObject)
objcmd.ExecuteNonQuery()
conObject.Close()
            End Sub

End Class


Now, the coding inside the Appform.aspx.vb - codebehind file is :

Code:
Partial Class AppForm
Inherits System.Web.UI.Page

            Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

                     Dim strSurname As String
                     strSurname = Request.Form("Surname")

                     Dim std As New clsStudent()   
                    std.setSurname(strSurname)

         End Sub

End Class


And Finally the code in the AppForm.aspx is :

Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AppForm.aspx.vb" Inherits="AppForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>

<form id="form1" method =post runat="server">

<div>&nbsp;


<asp:Button ID="Button1" runat="server" Style="z-index: 101; left: 399px; position: absolute; top: 171px" Text="Button" />

<input id="Text1" name = "Surname" style="z-index: 102; left: 200px; position: absolute; top: 171px"type="text" />

</div>

</form></body></html>


Now the problem with the code above is it stores the value enter into textbox in the variable strSurname and passed it onto commandobject in the Student Class - clsStudent too, but it didn't pass the value into the database.

It gives me the following error.

Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

ExecuteNonQuery: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.

Source Error:

Line 19:
Line 20: Dim objcmd As New Data.OleDb.OleDbCommand(cmd1, conObject)
Line 21: objcmd.ExecuteNonQuery()
Line 22: conObject.Close()
Line 23: End Sub

Source File: D:\Vishal\WebSite1\App_Code\clsStudent.vb Line: 21

Stack Trace:

[InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.]
System.Data.OleDb.OleDbCommand.ValidateConnection( String method) +988413
System.Data.OleDb.OleDbCommand.ValidateConnectionA ndTransaction(String method) +15
System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior behavior, String method) +51
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +108
clsStudent.setSurname(String strSurname) in D:\Vishal\WebSite1\App_Code\clsStudent.vb:21
AppForm.Button1_Click(Object sender, EventArgs e) in D:\Vishal\WebSite1\AppForm.aspx.vb:11
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +72
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +78
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4839


I appreciate your help and suggestion regarding this problem.

Reply With Quote
Reply

Viewing: ASP Free ForumsOtherProgramming Help > Problem adding records from .aspx page to access database.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway