Last post Feb 11, 2010 12:32 PM by juniper20
Feb 11, 2010 03:15 AM|juniper20|LINK
Hello... I'm unable to write to my Access Database using C#/ASP.NET. What am I doing wrong? Please help me.
protected void btnSubmit_Click(object sender, EventArgs e)
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + Server.MapPath("~\\App_Data\\wscdb.mdb'") + "; Persist Security Info=True");
string sqlCmd = "INSERT INTO users (UserType, FName, LName, Email, Company, ShippingAddress1, ShippingAddress2, ShippingCity, ShippingState, ShippingZip, BillingAddress1, BillingAddress2, BillingCity, BillingState, BillingZip, Phone, Password) " +
"VALUES ('Contractor', 'Abc', 'Def', 'firstname.lastname@example.org', 'Abc Company', '123 Abc Road', '456', 'Test', 'FL', '32257', '123 Alligator Street', '', 'St. Cloud', 'FL', '34769', '9044465115', 'test')";
OleDbCommand insertCmd = new OleDbCommand(sqlCmd, myConnection);
I'm eventually going to be inserting user data, this is just test code. I've tested the SQL query in Access and it works perfectly fine. I've permanently "trusted" the App_Data folder for Access. I've also given writing permissions to the "ASP.NET Machine
Account" to the solution folder and App_Data folder. I'm able to successfully connect and to use GridView for editing/updating rows, but when I try to insert data using an SQL statement, no rows are inserted and I don't get any errors at all. Please help,
thank you :(
Feb 11, 2010 07:47 AM|Aquaren|LINK
It doesn't look like you are actually executing the OleDbCommand. You code should read:
OleDbCommand insertCmd = new OleDbCommand(sqlcmd, myConnection);
Feb 11, 2010 07:56 AM|hans_v|LINK
It doesn't look like you are actually executing the OleDbCommand
Correct. But if you would do that I expect an error, because Password is a
reserved Word in Jet, and shoudl not be used as a fieldname, or if it is used as a fieldname, it should be enclosed with brackets in the SQL string:
INSERT INTO users (UserType, FName, LName, Email, Company, ShippingAddress1, ShippingAddress2, ShippingCity, ShippingState, ShippingZip, BillingAddress1, BillingAddress2, BillingCity, BillingState, BillingZip, Phone,
[Password]) VALUES ....
More information on Access and ASP.NET:
Feb 11, 2010 08:00 AM|Pooja_AM22Tech|LINK
You have missed code to actually run insert command. You have just created the conenction with database and associated an insert query with the connection. You also need to exceute it using this command:
before myConnection.close() and your insert query should run fine.
Feb 11, 2010 12:32 PM|juniper20|LINK
Well now, don't I just feel silly... Thank you for your help. I had no idea that Password was a reserved word, so thank you for that also. All of your suggestions helped, problem solved!