I can't Insert my Access Database with ExecuteNonQuery

Last post 01-19-2007 1:15 PM by fynne. 8 replies.

Sort Posts:

  • I can't Insert my Access Database with ExecuteNonQuery

    12-26-2006, 3:04 PM
    • Loading...
    • ipvn
    • Joined on 12-24-2006, 12:09 PM
    • Posts 10
    Operation must use an updateable query.

    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.Data.OleDb.OleDbException: Operation must use an updateable query.

    Source Error:

    Line 95: //				mycommand.Parameters.Add(new System.Data.OleDb.OleDbParameter("@ngay",System.Data.OleDb.OleDbType.DBDate,9,"@ngay"));
    Line 96: // mycommand.Parameters["@ngay"].Value =ngay;
    Line 97: mycommand.ExecuteNonQuery();
    Line 98: myconnection.Close();
    Line 99: }



     

    This is my code:

     

                    ngay = System.DateTime.Today.ToString() ;
                    string chuoiketnoi= "provider=microsoft.jet.oledb.4.0;Data Source="+ Server.MapPath("db_head.mdb");
                    string insertquery="Insert into tblnews(noidung,ngay) values('"+TextBox1.Text + "'" + "," + "'" + ngay + "')";
                    myconnection = new OleDbConnection(chuoiketnoi);
                    myconnection.Open();
                    mycommand = new OleDbCommand(insertquery,myconnection);
                    mycommand.ExecuteNonQuery();
                    myconnection.Close();

    Kevin Pham
    Student
    A Stupid man, and Hope get helpful
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-26-2006, 3:48 PM
    • Loading...
    • ipvn
    • Joined on 12-24-2006, 12:09 PM
    • Posts 10

    help me please !!!!!!!

     

    I can't INSERT data

    Kevin Pham
    Student
    A Stupid man, and Hope get helpful
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-26-2006, 3:54 PM
    • Loading...
    • jminond
    • Joined on 07-21-2003, 6:33 PM
    • New York
    • Posts 589

    1) There is a securiy detail we must check:  ( The message you got is very misleading, and you should foward it on to microsoft, so that maybe they fix in future ).;

    The reason for this error message possibly is that the process runs
    under an identity (usually ASPNET) that only has read access
    to the database.

    Right click on the database file in Windows Explorer, go to the
    Security tab, and give the ASPNET user modify rights to the file 

    Remember, that Access needs to create the *.ldb (lock) file in the same
    directory as the database. Giving rights to just the *.mdb may not be
    enough. I like to keep my *.mdb & *.ldb in a subfolder and permission that
    instead.

    2) Are you sure its the executenon query that is not working and not your command?

    Have you tryied writing out yor insertquery and testing it against the accessdb directly?

               Insert into tblnews(noidung,ngay) values('"+TextBox1.Text + "'" + "," + "'" + ngay + "')";

     assuming it does, then we go with number 1 as the lilkley issue.

     

    Jonathan Minond
    http://www.Jonavi.com
    http://www.jonavi.com/Default.aspx?pageID=21
    http://RainbowBeta.com
    http://community.rainbowportal.net/blogs/jonathans_rainbow_blog/default.aspx
    http://dotnetslackers.com/community/blogs/jminond/default.aspx
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-26-2006, 4:44 PM
    • Loading...
    • augustwind
    • Joined on 07-21-2002, 11:16 PM
    • Garland, TX
    • Posts 4,124
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    make sure the entire directory where your database is, has 'modify'  permissions for the ASPNet user - it needs to be able to write to that directory.
    David Wier
    MVP/MCP/ASPInsider
    ASPNet101.com - where to look first!
    iWrite Pro - doc/rtft to HTML/One Click PDF & much more
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-27-2006, 7:29 AM
    • Loading...
    • ipvn
    • Joined on 12-24-2006, 12:09 PM
    • Posts 10
    I still not do it T_T. I don't know how to solve it now :(( SleepTongue Tied
    Kevin Pham
    Student
    A Stupid man, and Hope get helpful
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-27-2006, 4:23 PM
    • Loading...
    • ipvn
    • Joined on 12-24-2006, 12:09 PM
    • Posts 10

    I don;t know how to repair it,

    Please Show me how to do it

    I use Access 2003  and C# ASPnet 1.1 

    Kevin Pham
    Student
    A Stupid man, and Hope get helpful
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-27-2006, 5:57 PM
    • Loading...
    • augustwind
    • Joined on 07-21-2002, 11:16 PM
    • Garland, TX
    • Posts 4,124
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Depending on your operating system - right-click on the folder where your database is kept.

    either click on Properties or "Sharing and Security" (whichever) - click on the Security tab - make sure the ASPNet user is in the "Group or user names" list. If not, click the 'Add' button to find and add it.

    Then, with that user selected, look at the bottom box (Permissions list) - make sure 'Modify' is checked - then click the Apply button.

    That will stop your "Operation must use an updateable query." error message from happening

    David Wier
    MVP/MCP/ASPInsider
    ASPNet101.com - where to look first!
    iWrite Pro - doc/rtft to HTML/One Click PDF & much more
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    12-30-2006, 2:07 PM
    • Loading...
    • nzj
    • Joined on 09-28-2003, 8:29 AM
    • Jordan
    • Posts 8

    Dear ipvn,

    You may face this error if your MS-Access Database doesnt have the right secuirity permissions, or may be it's open by another process

    1- If your file system is FAT32 then the security permissions don't have any effect, there will be no restrictions

    2 - If you are using NTFS file system on your hard drive then you should grant ASPNET user account MODIFY level on the .mdb file

    If you cant find the Sharing and Security option on the file or the folder do the following:

    [ go to FOLDER OPTIONS ----> VIEW ] and make sure that USE SIMPLE FILE SHARING is UNchecked

     3 - Grant MODIFY permission as shown on the other replies to your current post

     4 - Make sure that there is no active connection within your .NET IDE with the databse, if so close that connection

    * To prevent errors while using MS-Access databases try your best to keep only one active connection to the database

     I hope that my post is usefull, good luck

    nzj


    Distinti saluti,
    Nazeeh Ghatasheh
    | Jordan Engineers Association JEA
    | mob : +962 77 457466
    | Lavoro: +962 6 5607616 Ext. 247
    | nazeehgh@yahoo.com
    | nazeehgh@gmail.com
    | nzgh@hotmail.com
    | nazeeh@jea.org.jo
  • Re: I can't Insert my Access Database with ExecuteNonQuery

    01-19-2007, 1:15 PM
    • Loading...
    • fynne
    • Joined on 12-08-2006, 4:34 PM
    • Posts 29

    I actually pasted this code in another message thread.  It should do what you want.

        Dim insertSQL As String

        insertSQL = "insert into tblusers (username, pwd, datecreated) values " & _
            "(@username, @pwd, @datecreated)"

        Dim conn As New OleDbConnection(connString)
        Dim cmd As New OleDbCommand(insertSQL, conn)

        cmd.Parameters.AddWithValue("@username", txtUserName.Text)
        cmd.Parameters.AddWithValue("@pwd", txtPassword.Text)

        If txtDateCreated.Text = "" Then
            cmd.Parameters.AddWithValue("@datecreated", Now.ToString)
        Else
            cmd.Parameters.AddWithValue("@datecreated", txtDateCreated.Text)
        End If

        Try
            conn.Open()
            cmd.ExecuteNonQuery()
            lblResult.Text = "User Added"
        Catch ex As Exception
            lblResult.Text = ex.Message
        Finally
            conn.Close()
        End Try

Page 1 of 1 (9 items)
Microsoft Communities
Page view counter