-
January 5th, 2010, 01:29 AM
#1
OleDbCommand problem
hi,
i have a code that i cant make it work
Code:
string myName = "homerfavenir";
string source = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=user.mdb";
string input = @"INSERT INTO Users (firstname) values (" + myName + ")";
OleDbConnection conn = new OleDbConnection(source);
conn.Open();
// Do something useful
OleDbCommand cmd = new OleDbCommand(input,conn);
cmd.ExecuteNonQuery();
conn.Close();
error
Code:
No value given for one or more required parameters.
thanks
-
January 5th, 2010, 03:31 AM
#2
Re: OleDbCommand problem
I think that myName shoud be qouted in SQL to get something like INSERT INTO Users (firstname) values ('myName')
Try
Code:
string input = @"INSERT INTO Users (firstname) values ('" + myName + "')";
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 5th, 2010, 04:23 AM
#3
Re: OleDbCommand problem
thanks!
ok, but when the INSERT is success, i need to close the application and open it again to update the table and see the record.
how can i refresh it so that when i insert a record it will show automatically
tia
-
January 5th, 2010, 11:04 AM
#4
Re: OleDbCommand problem
ok, but when the INSERT is success, i need to close the application and open it again to update the table and see the record.
how can i refresh it so that when i insert a record it will show automatically
How about reading the information from the table using another command before you close the connection? The SQL should be simple
Code:
"SELECT * FROM Users"
How you read the data depends on how you've structured your application and how you want to display the data. The two simple options are using a OleDbDataReader and custom class representing the user or using OleDbDataAdapter and a DataSet. I generally go for the former approach but second is equally valid.
Using OleDBDataReader
Using OleDbDataAdapter
These links should get you going.
Last edited by nelo; January 5th, 2010 at 11:05 AM.
Reason: Correctness...
-
January 6th, 2010, 04:17 AM
#5
Re: OleDbCommand problem
It is good practice to enumare all required columns in select clause rather than using "*" because it is more robust forward schema changes.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 6th, 2010, 05:24 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|