• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

ASP.NET question (again...*sigh*)

Martin

Lifer
Alright, I am really stuck this time. I simply can't insert a damn record into a database.

After trying several other methods, I ran accross this sample:
http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=insertmdb and more specifically this part:
Sub doInsert(Source as Object, E as EventArgs)
Dim MySQL as string = "Insert into testInsert (fname, lname) values (@Fname, @Lname)"
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\Data\TestInsertDB.mdb") & ";")
Dim Cmd as New OleDbCommand(MySQL, MyConn)
cmd.Parameters.Add(New OleDbParameter("@Fname", frmfname.text))
cmd.Parameters.Add(New OleDbParameter("@Lname", frmlname.text))
MyConn.Open()
cmd.ExecuteNonQuery
MyConn.Close()
label1.visible="true"
BindData()
label1.text = "Your data has been received!"
End Sub

I adapted it to my needs, but when I tried to access my page it says Operation must use an updateable query. on the line where I call the ExecuteNonQuery() method.

Confused, I deciced to download the whole example and run it on my computer. I downloaded their mdb file and copy/pasted the code in a new page. When I ran the sample, it gave the same error where cmd.ExecuteNonQuery is.

I have no idea what's going on.

 
How do you remember all those commands and what not? You must be a genius!!! Are you? Or do you just have a photographic memory?
 
You need to set the commandtype.

I just can't remember the valid options. I'll be able to tell you when I get to work tomorrow @ 8am.
 
Bah, I was wrong. the default commandtype text should work for that query.

I have almost the exact same example now that I'm looking at it.

I'll bold a few changes to your sample that make it look virtually the same as code I know works:

Sub doInsert(Source as Object, E as EventArgs)
Dim MySQL as string = "Insert into testInsert (fname, lname) values (@Fname, @Lname)"
Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\Data\TestInsertDB.mdb") & ";")
MyConn.Open()
Dim Cmd as New OleDbCommand(MySQL, MyConn)

cmd.Parameters.Add("@Fname", frmfname.text))
cmd.Parameters.Add("@Lname", frmlname.text))


Dim records As Integer = cmd.ExecuteNonQuery
MyConn.Close()
label1.visible="true"
BindData()
label1.text = "Your data has been received!"
End Sub
 
I haven't looked that closely at your code, and I haven't tried it out on my computer, but one thing you should check is if your Access database is read-only. Does the user you are running as have permission to write to your database?
 
Kilrsat, I just tried your Sub (copy/pasted it instead of the doInsert sub in the sample file), and it gives me the same error. I modified my own method to work like yours, but it still gives me the same error.

oog, I gave Internet Guest Account full acess to the dirs where the mdb files are, but it doesn't change anything.

 
Originally posted by: oog
I know I asked this before, but is the MDB file read only?

Sorry, I've answered my own question. I've reproduced the problem here. Let me take a closer look.
 
Okay. I downloaded the MDB file and the code, and I got it to work without changing any code. The problem is that the default user that ASP .NET runs as does not have access to much at all. The default user, machine\ASPNET, is generally pretty restricted, and maybe it cannot access the MDB file.

What I did was to turn off anonymous authentication, and turned on impersonation, and the insert worked. In this way, my Windows login account was used to execute the code instead of the ASPNET account.

To turn off anonymous authentication, go to the IIS management component. To turn on authentication, create a web.config file in your web directory, and include the following code:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authentication mode="Windows" />
<identity impersonate="true"/>
</system.web>
</configuration>
 
Originally posted by: MartyTheManiak


oog, I gave Internet Guest Account full acess to the dirs where the mdb files are, but it doesn't change anything.

Although this sounds like enough, be aware that it is the machine\ASPNET account that is trying to access the database, not the machine\IUSER_machine account.
 
Originally posted by: oog
Originally posted by: MartyTheManiak


oog, I gave Internet Guest Account full acess to the dirs where the mdb files are, but it doesn't change anything.

Although this sounds like enough, be aware that it is the machine\ASPNET account that is trying to access the database, not the machine\IUSER_machine account.

I gave the ASPNET account full access to the database and now it works without a prob. Thanks so much for your help (both in this thread and in the last one). Now that I have the basics down, hopefully my little programming excercise will go smoothly 🙂
 
No problem. Feel free to continue posting .NET questions. Personally, I'm more comfortable with C# than VB .NET (though I have had many years of VB programming prior to .NET), but either language is fine.
 
Back
Top