Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.NET and ASPX Saving problem

Posted on 2004-10-29
34
Medium Priority
?
515 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
Comment
Question by:fadixp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 10
  • 5
  • +2
34 Comments
 
LVL 2

Author Comment

by:fadixp
ID: 12445759
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
ID: 12445935
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
ID: 12446040
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:fadixp
ID: 12448906
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
ID: 12449679
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
davidlars99 earned 400 total points
ID: 12449877
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
ID: 12449904
0
 
LVL 2

Author Comment

by:fadixp
ID: 12450710
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
ID: 12451056
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
ID: 12451069
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
ID: 12451622
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
ID: 12451768
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
davidlars99 earned 400 total points
ID: 12451872


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

Author Comment

by:fadixp
ID: 12452390
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
ID: 12452410
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
ID: 12452601
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
ID: 12453537
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
ID: 12453697
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
ID: 12453742
there's no error at all
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12455758
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
ID: 12456269
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
ID: 12461159
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
ID: 12461427
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
ID: 12461631
in one of the steps we have done it was duplicating records if you noticed .
0
 
LVL 11

Accepted Solution

by:
pratap_r earned 1600 total points
ID: 12461707
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
ID: 12461752
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
ID: 12461771
yeah the connection is invalid
you can try
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12461799
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
ID: 12461974
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
ID: 12462091
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
ID: 12462126
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
ID: 12462168
the machine.config is in WINDOWS\Microsoft.NET\Framework\<version>\CONFIG directory
0
 
LVL 2

Author Comment

by:fadixp
ID: 12462259
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
ID: 12471783
well i think am going to use Oledb. thanks pratap_r
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Progress

618 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