Solved

VB.NET and ASPX Saving problem

Posted on 2004-10-29
501 Views
Last Modified: 2012-05-05
Hello there
i have a problem while saving new record in ASPX but its displaying the records correctly, am using the following :
VB.NET "programming application"
ADODB "added in referance"
.MDB file "Database"
connecting to a server supports ASP.NET and Access Database
ok lets start the coding :
-------------------------------------------------------------------------------------------------
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Connect to the DB
        'On Error Resume Next

        cn = New ADODB.Connection
        rs = New ADODB.Recordset

        cn.Provider = "microsoft.jet.oledb.4.0"
        cn.Open(Server.MapPath("database.mdb"))


        rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
        rs.MoveFirst()
        TextBox1.Text = rs.Fields(1).Value

    End Sub
    Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick

        On Error Resume Next

        rs.MoveNext()
        TextBox1.Text = rs.Fields(1).Value

    End Sub

    Private Sub Button2_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ServerClick
        On Error Resume Next

        rs.MovePrevious()
        TextBox1.Text = rs.Fields(1).Value

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'clearing the current text box to type the new record
        TextBox1.Text = ""
        rs.AddNew()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

       'rs("Title1").Value = TextBox1.Text

        rs.save

        rs.Close()
        cn = Nothing
    End Sub
End Class

------------------------------------------------------------------------------------
any help will be appreciated .
0
Question by:fadixp
    34 Comments
     
    LVL 2

    Author Comment

    by:fadixp
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

           rs("Title1").Value = TextBox1.Text

            rs.save

            rs.Close()
            cn = Nothing
        End Sub
    End Class


    sorry with out the comma " ' "
    am still getting the same error :

    Server Error in '/DEMO' Application.
    --------------------------------------------------------------------------------
    Can't save.
    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.Runtime.InteropServices.COMException: Can't save.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace:


    [COMException (0x80030103): Can't save.]
       ADODB.RecordsetClass.Save(Object Destination, PersistFormatEnum PersistFormat) +0
       DEMO.WebForm1.Button4_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\VSWebCache\lnlgeneration.com\DEMO\webform.aspx.vb:77
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1292

     
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    check if you have write access to the mdb file or try running under integrated authentication.

    why dont you use ADO.Net instead of going for interop?

    Pratap
    0
     
    LVL 5

    Expert Comment

    by:Hammadian2
    you're not creating a new record this way
    u'r modifying an existing one (the one rs is pointing, even if u emptied the text box)

    if update is what u want then use
     rs.update

    instead of
     rs.save

    if u want to add a new record
    then use the AddNew method, then Update the recordset

    rs.AddNew "Title1", TextBox1.Text
    rs.Update

    for more details about recordsets

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdobjodbrecpme.asp

    for more details about AddNew and code samples

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthaddnew.asp
    0
     
    LVL 2

    Author Comment

    by:fadixp
    well i tried using your way rs.addnew "title1" , textbox1.text
    rs.update or rs.save and both are not working
    it shows like its ok if use update but actually its not being updated in the server and when i use save it shows me the above error, about using ado.net i dont know a thing about it ..so i dont want to take a risk .
    other wise you will have to tell me all the functions to let it work using ADO.NET i mean query , update , save , move next and back and all :P
    0
     
    LVL 44

    Expert Comment

    by:Arthur_Wood
    the correct syntax should be

    rs.AddNew
    rs("Title1") = TextBox1.Text
    rs.Update

    rs.save serves a completely different purpose, allowing you to 'pertsist' the recordset to an offline file (not to the database).  The persisted file can then later be 'reconsistuted' back into your application.  This is NOT the same as saving a record back to the table in your database.

    AW
    0
     
    LVL 13

    Assisted Solution

    by:davidlars99
    right now you have
    >>> rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)



    try  >>> adLockBatchOptimistic <<<  if you are updating more then one record
     rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic)

    OR

    try  >>> adLockOptimistic <<<  if you are updating just one record
     rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)


    0
     
    LVL 13

    Expert Comment

    by:davidlars99
    0
     
    LVL 2

    Author Comment

    by:fadixp
    i tried all still not working i cannot update , and when some times it happens its not adding the text i've typed its just duplicating the existing record
    0
     
    LVL 2

    Author Comment

    by:fadixp
    any one can find the mistake am doing here please ?!
    i've post it the code am using ..any other comments ?
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    your page load has a bug..

    what you are doing is regard less of whether it is a post back or not you are initializing the recordset and then assigning a value to your textbox (meaning whatever you typed in manually is lost)

    change your initialization code to do the rs query only on the first load use ispostback


    Pratap
    0
     
    LVL 2

    Author Comment

    by:fadixp
    man am dump i know ! but plz tell me where exactly to use the ispostback because i tried and i always get errors ,   this is my latest code :


      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            cn = New ADODB.Connection
            rs = New ADODB.Recordset
            cn.Provider = "microsoft.jet.oledb.4.0"
            cn.Open(Server.MapPath("bin\database.mdb"))
            rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
            rs.MoveFirst()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
        Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
            On Error Resume Next
            rs.MoveNext()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
        Private Sub Button2_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ServerClick
            On Error Resume Next
            rs.MovePrevious()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
        Private Sub AddCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCmd.Click
            TextBox1.Text = ""
            rs.AddNew()
        End Sub
        Private Sub SaveCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCmd.Click
            Dim strInput As String
            strInput = TextBox1.Text
            rs("Title1").Value = strInput
            rs.Update()
        End Sub
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    heres the code.....

        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                cn.Provider = "microsoft.jet.oledb.4.0"
                cn.Open(Server.MapPath("bin\database.mdb"))
                rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
                rs.MoveFirst()
                TextBox1.Text = rs.Fields(1).Value
                Session("cn") = cn
                Session("rs") = rs
            Else
                cn = Session("cn")
                rs = Session("rs")
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            On Error Resume Next
            rs.MoveNext()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            On Error Resume Next
            rs.MovePrevious()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
        Private Sub AddCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCmd.Click
            rs.AddNew()
            rs("Title1").Value = TextBox1.Text
            rs.Update()
        End Sub
        Private Sub SaveCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCmd.Click
            Dim strInput As String
            strInput = TextBox1.Text
            rs("Title1").Value = strInput
            rs.Update()
        End Sub
    0
     
    LVL 13

    Assisted Solution

    by:davidlars99


    With rs
      .addNew
         .Fields("field1")=value1
         .Fields("field2")=value2
         .Fields("field3")=value3
      .Update
    End With
    0
     
    LVL 2

    Author Comment

    by:fadixp
    well ur code some how should be right but its not .
    i got this err message
    Server Error in '/DEMO' Application.
    --------------------------------------------------------------------------------

    Object reference not set to an instance of an object.
    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.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace:


    [NullReferenceException: Object reference not set to an instance of an object.]
       DEMO.WebForm1.AddCmd_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\VSWebCache\lnlgeneration.com\DEMO\webform.aspx.vb:43
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1292

     
    0
     
    LVL 13

    Expert Comment

    by:davidlars99
    add this at the top of the page and post entire error here


    <%@ Page Language="VB" Debug="true" %>


    this should give you error line
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    faxdip whose code are you refering to? the one I posted? and do post the entire code

    Pratap
    0
     
    LVL 2

    Author Comment

    by:fadixp
    sorry for not being clear but i was talking about yours pratap it looks clean and it looks like it solvs the problems but still i get another error message
    as i said in the last post

    and about david i will try now what ever you have said to post the error here
    0
     
    LVL 2

    Author Comment

    by:fadixp
    if you want to see the error live you can press on this link so you will be knowing what am facing exactly
    http://lnlgeneration.com/DEMO/webform.aspx
    0
     
    LVL 13

    Expert Comment

    by:davidlars99
    there's no error at all
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    hmmm hey fadixp, i dont see any error but the records are not getting updated i guess.. can you post the entire code..? both for the .aspx and for .cs

    Pratap
    0
     
    LVL 2

    Author Comment

    by:fadixp
    are you sure guys ! ? i can see the problem !
    any ways this is the code again .. am sorry guys i know i make a big headech but i think i have to increase the points and i will because you guyz deserve more than 500 :(
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Dim cn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
        Protected WithEvents Button1 As System.Web.UI.HtmlControls.HtmlInputButton
        Protected WithEvents AddCmd As System.Web.UI.WebControls.Button
        Protected WithEvents SaveCmd As System.Web.UI.WebControls.Button
        Protected WithEvents Button2 As System.Web.UI.HtmlControls.HtmlInputButton
    #Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

    #End Region
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                cn.Provider = "microsoft.jet.oledb.4.0"
                cn.Open(Server.MapPath("bin\database.mdb"))
                rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
                rs.MoveFirst()
                TextBox1.Text = rs.Fields(1).Value
                Session("cn") = cn
                Session("rs") = rs
            Else
                cn = Session("cn")
                rs = Session("rs")
            End If
        End Sub
        Private Sub AddCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCmd.Click
            rs.AddNew()
            TextBox1.Text = ""
        End Sub
        Private Sub SaveCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCmd.Click
            Dim strInput As String
            strInput = TextBox1.Text
            rs("Title1").Value = strInput
            rs.Update()
        End Sub

        Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
            On Error Resume Next
            rs.MoveNext()
            TextBox1.Text = rs.Fields(1).Value
        End Sub

        Private Sub Button2_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ServerClick
            On Error Resume Next
            rs.MovePrevious()
            TextBox1.Text = rs.Fields(1).Value
        End Sub
    End Class

    and plz try to add new in the web and then save
    thanks
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    the save & addnew are failing now.. hmm.. the same code works fine on my system though, how about the security rights on the mdb file?
    0
     
    LVL 2

    Author Comment

    by:fadixp
    security read and write for all ..
    http://lnlgeneration.com/DEMO/bin/database.mdb
    this is it ! check it out if you want
    0
     
    LVL 2

    Author Comment

    by:fadixp
    in one of the steps we have done it was duplicating records if you noticed .
    0
     
    LVL 11

    Accepted Solution

    by:
    can you try replacing the page_load with this? hmm.. there seems to be nothing wrong with the code because it works fine on my system. i am trying to figure out what settings are different.. now i am checking if session is valid..

            If Not IsPostBack Then
                cn.Provider = "microsoft.jet.oledb.4.0"
                cn.Open(Server.MapPath("bin\database2.mdb"))
                rs.Open("DynDB", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
                rs.MoveFirst()
                TextBox1.Text = rs.Fields(1).Value
                Session("cn") = cn
                Session("rs") = rs
            Else
                Session("cn") = Nothing
                cn = Session("cn")
                If cn Is Nothing Then
                    Response.Write("Session Invalid")
                    Response.End()
                End If
                rs = Session("rs")
                If rs Is Nothing Then
                    Response.Write("Session Invalid")
                    Response.End()
                End If
            End If

    0
     
    LVL 2

    Author Comment

    by:fadixp
    well i will try now ..but before i want to tell you i got it working with OLEDB but i still want it to work with the ADODB so i will do the test now and give you my feedback
    thanks
    0
     
    LVL 2

    Author Comment

    by:fadixp
    yeah the connection is invalid
    you can try
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    good that it is working.. hmm as for the adodb.. session is not enabled for your web application... enable the session state and it should work fine. thats the only difference that I can see right now
    0
     
    LVL 2

    Author Comment

    by:fadixp
    well , i know am really new in this ..but can you please tell me how or whom should i ask about enabling the session state ?
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    check to see if you have something like this line in your machine.config file
    <add name="sessionState" type="System.Web.SessionState.SessionStateModule, Version=1.0.3300.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" />


    and this in your web.config file

            <sessionState mode="Inproc"
                          cookieless="false"
                          timeout="20"/>
            </sessionState>
    0
     
    LVL 2

    Author Comment

    by:fadixp
    ok i dont have machine.config
    but i do have the web.config with the statement above .
    0
     
    LVL 11

    Expert Comment

    by:pratap_r
    the machine.config is in WINDOWS\Microsoft.NET\Framework\<version>\CONFIG directory
    0
     
    LVL 2

    Author Comment

    by:fadixp
    no i dont have it
    by the way am using v1.1.4322 of .NET

    this may helps...
    0
     
    LVL 2

    Author Comment

    by:fadixp
    well i think am going to use Oledb. thanks pratap_r
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now