error "cannot open the action query"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarabonn
    New Member
    • Nov 2008
    • 69

    error "cannot open the action query"

    hallo

    Iam trying to insert a row into a access database using visual c# and iam getting an error " cannot open the action query". here is my code

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
         txt1 = textBox1.Text;
                txt2 = textBox2.Text;
                String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Userinfo.accdb";
                OleDbConnection connection = new OleDbConnection(connectionString);
                           OleDbCommand command = new OleDbCommand();           
    
                
                  command.CommandType = CommandType.TableDirect;
                  command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
                  command.Connection = connection;
                  connection.Open();
                  command.ExecuteNonQuery(); // getting in this line .
                  connection.Close();
    any idea why is this error ?

    Thank you.
    Dinesh.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [CODE] [/CODE] tags in future.

    MODERATOR

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Your CommandType should be
      CommandType.Tex t

      With TableDirect, I believe you would just make CommandText ="MyTableNam e" and it would return the contents of the entire table

      Comment

      • sarabonn
        New Member
        • Nov 2008
        • 69

        #4
        Originally posted by Plater
        Your CommandType should be
        CommandType.Tex t

        With TableDirect, I believe you would just make CommandText ="MyTableNam e" and it would return the contents of the entire table

        hallo plater,

        If i use the CommanType.Text it gives me error "No value given for one or more required parameters." in the line Command.Execute NonQuery();.

        Thank you.

        Dinesh.

        Comment

        • MrMancunian
          Recognized Expert Contributor
          • Jul 2008
          • 569

          #5
          Instead of

          Code:
          command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
          use

          Code:
          command.InsertCommand = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
          Steven

          Comment

          • sarabonn
            New Member
            • Nov 2008
            • 69

            #6
            Originally posted by MrMancunian
            Instead of

            Code:
            command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
            use

            Code:
            command.InsertCommand = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
            Steven
            hallo steven,

            Command is an object oldbcommand and it doesn't have the command.insertc ommand or do you mean oledbdataadapte r.InsertCommand ?..

            thank you,

            dinesh.

            Comment

            • MrMancunian
              Recognized Expert Contributor
              • Jul 2008
              • 569

              #7
              Originally posted by sarabonn
              hallo steven,

              Command is an object oldbcommand and it doesn't have the command.insertc ommand or do you mean oledbdataadapte r.InsertCommand ?..

              thank you,

              dinesh.
              Yes, sorry, I meant the DataAdapter.Ins ertCommand

              Steven

              Comment

              • sarabonn
                New Member
                • Nov 2008
                • 69

                #8
                Originally posted by MrMancunian
                Yes, sorry, I meant the DataAdapter.Ins ertCommand

                Steven
                hallo steven,

                I changed the code like this with the dataadapter.ins ertcommand but iam getting a error "No value given for one or more required parameters." in the executenonquery () line. Iam sure about the table details.

                Code:
                private void button1_Click(object sender, EventArgs e)
                        {
                         txt1 = textBox1.Text;
                            txt2 = textBox2.Text;
                            txt3 = textBox1.Text;
                             txt4 = textBox2.Text;
                            OleDbConnection cnJetDB = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Userinfo.accdb");
                            cnJetDB.Open();
                            OleDbDataAdapter oleDa = new OleDbDataAdapter();
                            OleDbCommand cmdInsert = new OleDbCommand("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl)" + "VALUES (" + txt1 + "," + txt2 + "," + txt3 + "," + txt4 + ")", cnJetDB);
                
                            oleDa.InsertCommand = cmdInsert;
                            
                            cmdInsert.ExecuteNonQuery();
                            cnJetDB.Close();
                }
                any idea steven ?..

                Thank you.
                Dinesh

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  I would say your problem is using the illegal quote character in your string. try using the regular ' single quote and see if that works for you.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Plater
                    I would say your problem is using the illegal quote character in your string. try using the regular ' single quote and see if that works for you.
                    I don't see any quotes around the text values there at all legal or otherwise. My colleagues say that I'm getting old so my eyes may be the problem.

                    Comment

                    • sarabonn
                      New Member
                      • Nov 2008
                      • 69

                      #11
                      Originally posted by Plater
                      I would say your problem is using the illegal quote character in your string. try using the regular ' single quote and see if that works for you.
                      hallo plater,

                      i have changed the code and the line which u say to change to single quote as follows
                      Code:
                                  OleDbCommand cmdInsert = new OleDbCommand("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl)" + "VALUES (" + txt1 + "," + txt2 + "," + txt3 + "," + txt4 + ")", cnJetDB);
                      then iam getting error in the executenonquery line.

                      thank you.

                      Dinesh.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        When passing text to the database, you need to pass it surrounded by single quotes. Suppose your value is stored in a variable called text, you would pass in "'"+ text + "'". Have a look at other people's code and you will see all those quotes all over the place. Not very elegant I might add. Better use Parameters and let the drivers handle that for you.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          Edit: ok, so yeah, the single quotes you used were the left-single-quote and right-single-quote. You need the non-directional-single quote
                          [code=c#]
                          "INSERT INTO userinfo (Username, Pwd) VALUES ('" + txt1 + "' , '" + txt2 + "')";
                          [/code]

                          Comment

                          • Curtis Rutland
                            Recognized Expert Specialist
                            • Apr 2008
                            • 3264

                            #14
                            OK, I think the problem is that you are trying to insert into varchar fields without using single quotes around your values.

                            Try this:
                            Code:
                            string cmdText = String.Format("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl) VALUES ('{0}','{1}','{2}','{3}')",txt1,txt2,txt3,txt4);
                            OleDbCommand cmdInsert = new OleDbCommand(cmdText,cnJetDB);
                            I've used the String.Format method here. Notice how much cleaner and easier it is to understand than that mess of string concatenation you had before.

                            Comment

                            • sarabonn
                              New Member
                              • Nov 2008
                              • 69

                              #15
                              Originally posted by insertAlias
                              OK, I think the problem is that you are trying to insert into varchar fields without using single quotes around your values.

                              Try this:
                              Code:
                              string cmdText = String.Format("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl) VALUES ('{0}','{1}','{2}','{3}')",txt1,txt2,txt3,txt4);
                              OleDbCommand cmdInsert = new OleDbCommand(cmdText,cnJetDB);
                              I've used the String.Format method here. Notice how much cleaner and easier it is to understand than that mess of string concatenation you had before.
                              Thank you so much moderator. You are right the mess i have made to the string concatenation. thank you once again. now the problem is solved.. I have one more doubt?. I have this database "userinfo.accdb " in my c:\userinfo.acc db". I have added this to my project. so when i use the executable version of my project ( i mean the exe file the bin folder) in another computer should i also carry this userinfo.accdb. if so how can change the datasource of the database in the .exe version of the project.

                              Dinesh.

                              Comment

                              Working...