Question

I added a stored query in MS Access to insert the data in a table with the parameters while creating the database.

cmmd.CommandText = @"CREATE PROCEDURE queryAddAcc(AccNum text(50), AccName Text(50), By long, Date Date) AS 
INSERT INTO tableAccounts (AccNumber, AccountName, By, Date)
VALUES ([AccNum], [AccName], [By], [Date])";
cmmd.ExecuteNonQuery();

Please tell How to call the stored query to insert values on "Save" button click ?

Was it helpful?

Solution

Well, I would absolutely change those reserved words in a more neutral way. I mean By and Date are reserved words for access and thus require squared brackets around them, but in your context there is no reason to call those parameters in that way

cmmd.CommandText = @"CREATE PROCEDURE queryAddAcc(AccNum text(50), AccName Text(50), 
                     ByParam long, DateParam Date) AS 
                   INSERT INTO tableAccounts (AccNumber, AccountName, [By], [Date])
                   VALUES ([AccNum], [AccName], [ByParam], [DateParam])";
cmmd.ExecuteNonQuery();

Of course I suggest to change also the field's names for the same reason. I am sure that if I were you I would forget everytime

Next, to call the query, you need only to

OleDbCommand cmd = new OleDbCommand("queryAddAcc", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("AccNum", valueForAccNum);
..... add the other parameters here .....
cmd.ExecuteNonQuery();

Notice that you need to add the parameter in the exact order expected by the stored procedure because OleDb won't make efforts to recognize your parameters by their name and assign the values in the correct order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top