473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void button1_Click(object sender, EventArgs e)
  3.         {
  4.      txt1 = textBox1.Text;
  5.             txt2 = textBox2.Text;
  6.             String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Userinfo.accdb";
  7.             OleDbConnection connection = new OleDbConnection(connectionString);
  8.                        OleDbCommand command = new OleDbCommand();           
  9.  
  10.  
  11.               command.CommandType = CommandType.TableDirect;
  12.               command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
  13.               command.Connection = connection;
  14.               connection.Open();
  15.               command.ExecuteNonQuery(); // getting in this line .
  16.               connection.Close();
  17.  
  18.  
any idea why is this error ?

Thank you.
Dinesh.
Nov 18 '08 #1
14 3800
Curtis Rutland
3,256 Expert 2GB
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
Nov 18 '08 #2
Plater
7,872 Expert 4TB
Your CommandType should be
CommandType.Text

With TableDirect, I believe you would just make CommandText ="MyTableName" and it would return the contents of the entire table
Nov 18 '08 #3
Your CommandType should be
CommandType.Text

With TableDirect, I believe you would just make CommandText ="MyTableName" 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.ExecuteNonQuery();.

Thank you.

Dinesh.
Nov 19 '08 #4
MrMancunian
569 Expert 512MB
Instead of

Expand|Select|Wrap|Line Numbers
  1. command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
  2.  
use

Expand|Select|Wrap|Line Numbers
  1. command.InsertCommand = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)"; 
  2.  
Steven
Nov 19 '08 #5
Instead of

Expand|Select|Wrap|Line Numbers
  1. command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
  2.  
use

Expand|Select|Wrap|Line Numbers
  1. command.InsertCommand = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)"; 
  2.  
Steven
hallo steven,

Command is an object oldbcommand and it doesn't have the command.insertcommand or do you mean oledbdataadapter.InsertCommand ?..

thank you,

dinesh.
Nov 19 '08 #6
MrMancunian
569 Expert 512MB
hallo steven,

Command is an object oldbcommand and it doesn't have the command.insertcommand or do you mean oledbdataadapter.InsertCommand ?..

thank you,

dinesh.
Yes, sorry, I meant the DataAdapter.InsertCommand

Steven
Nov 19 '08 #7
Yes, sorry, I meant the DataAdapter.InsertCommand

Steven
hallo steven,

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

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.          txt1 = textBox1.Text;
  4.             txt2 = textBox2.Text;
  5.             txt3 = textBox1.Text;
  6.              txt4 = textBox2.Text;
  7.             OleDbConnection cnJetDB = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Userinfo.accdb");
  8.             cnJetDB.Open();
  9.             OleDbDataAdapter oleDa = new OleDbDataAdapter();
  10.             OleDbCommand cmdInsert = new OleDbCommand("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl)" + "VALUES (" + txt1 + "," + txt2 + "," + txt3 + "," + txt4 + ")", cnJetDB);
  11.  
  12.             oleDa.InsertCommand = cmdInsert;
  13.  
  14.             cmdInsert.ExecuteNonQuery();
  15.             cnJetDB.Close();
  16.  
any idea steven ?..

Thank you.
Dinesh
Nov 19 '08 #8
Plater
7,872 Expert 4TB
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.
Nov 19 '08 #9
r035198x
13,262 8TB
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.
Nov 19 '08 #10
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
Expand|Select|Wrap|Line Numbers
  1.             OleDbCommand cmdInsert = new OleDbCommand("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl)" + "VALUES (" + txt1 + "," + txt2 + "," + txt3 + "," + txt4 + ")", cnJetDB);
  2.  
then iam getting error in the executenonquery line.

thank you.

Dinesh.
Nov 19 '08 #11
r035198x
13,262 8TB
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.
Nov 19 '08 #12
Plater
7,872 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO userinfo (Username, Pwd) VALUES ('" + txt1 + "' , '" + txt2 + "')";
  2.  
Nov 19 '08 #13
Curtis Rutland
3,256 Expert 2GB
OK, I think the problem is that you are trying to insert into varchar fields without using single quotes around your values.

Try this:
Expand|Select|Wrap|Line Numbers
  1. string cmdText = String.Format("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl) VALUES ('{0}','{1}','{2}','{3}')",txt1,txt2,txt3,txt4);
  2. OleDbCommand cmdInsert = new OleDbCommand(cmdText,cnJetDB);
  3.  
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.
Nov 19 '08 #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:
Expand|Select|Wrap|Line Numbers
  1. string cmdText = String.Format("INSERT INTO userinfo (Username, Pwd,Openid,ServerUrl) VALUES ('{0}','{1}','{2}','{3}')",txt1,txt2,txt3,txt4);
  2. OleDbCommand cmdInsert = new OleDbCommand(cmdText,cnJetDB);
  3.  
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.accdb". 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.
Nov 19 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Koen | last post by:
Hi all, At work I created a database which is really helpful. The database is used by approx 15 users. Everything worked great, until I added some 'scoreboard' forms and reports. I get the...
0
by: Mr.KisS | last post by:
Hello. I'm under Windows XP PRO SP1, IIS 5.1 ans SQL SERVER 2005 Express. When i try to open a connexion with : <connectionStrings> <add name="AppCnxStr"...
2
by: Mr.KisS | last post by:
Hello. I'm under Windows XP PRO SP1, IIS 5.1 ans SQL SERVER 2005 Express. When i try to open a connexion with : <connectionStrings> <add name="AppCnxStr"...
7
by: Mathew Butler | last post by:
I'm investigating an issue I have when pulling data back from MS access I'm retrieving all rows from a column in a table of 5000 rows - accessing a column of type "memo" ( can be 65353 character...
3
by: Joanne | last post by:
I have searched through previous threads regarding this error. I went back through all my code to make sure I closed and set to nothing all open db's and rs's. However, I still have one computer...
1
by: bhushan11 | last post by:
hello sir i am searching in your site but i dont get how to place question about visual c++. so i am post this vc++ question in .net , sorre about that i am working on visual c++ lang. but i...
1
by: alex21 | last post by:
Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery...
3
scubak1w1
by: scubak1w1 | last post by:
Hello, Did a search here, manuals and Googled, etc generally, so apologies if I did not find the answer to my issue - that is, I try and 'self help' as mush as I can... I adjusted my...
0
by: Carruthers Pen | last post by:
The command to open outlook is below: conPATH_TO_OUTLOOK is C:\Program Files\Microsoft Office\Office14\outlook.exe retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus) the error message is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.