0

I have problem with executing stored procedure in my asp.net project using .mdb database. I would like to use stored procedure but after executnion code...

using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString()))
                {
                    conn.Open();
                    using (OleDbCommand com = new OleDbCommand("Insert", conn))
                    {
                        com.CommandType = CommandType.StoredProcedure;
                        com.Parameters.AddWithValue("@Login", UserName0.Text);
                        com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text));
                        com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue);
                        com.ExecuteNonQuery();
                    }
                    conn.Close();

i have exception:

System.Data.OleDb.OleDbException: Expected query name after EXECUTE.

but when i use below the code everything is ok. i also changed: CommandType.StoredProcedure to CommandType.Text but it's still not work. Could somebody help me?

using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString()))
                {
                    conn.Open();
                    using (OleDbCommand com = new OleDbCommand("INSERT INTO Workers ( st_login, st_password, st_role ) VALUES (login, password, role);", conn))
                    {
                        com.Parameters.AddWithValue("@Login", UserName0.Text);
                        com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text));
                        com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue);
                        com.ExecuteNonQuery();
                    }
                    conn.Close();
user1578754
  • 43
  • 2
  • 5

2 Answers2

1

If your code listing is accurate, it would seem you have a stored procedure named Insert which cannot be advisable. Try creating the procedure using a name that isn't a reserved keyword and see if that helps.

JustinStolle
  • 4,182
  • 3
  • 37
  • 48
0
using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString()))
                {
                    conn.Open();
                    using (OleDbCommand com = new OleDbCommand("INSERT INTO Workers ( st_login, st_password, st_role ) VALUES (?, ?, ?);", conn))
                    {
                        com.Parameters.AddWithValue("@Login", UserName0.Text);
                        com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text));
                        com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue);
                        com.ExecuteNonQuery();
                    }
                    conn.Close();

Please note Ms-Access do not supports stored procedures. If you need so or complex queries, go for Sql Server. As your application is using asp.net. I would recommend ASP.Net with Sql Server as backend. Anyways for your answer, Replace parameters with ? when using MS-Access. See above.

  • Do you think `@Login` is relevant when you have `?` ? – V4Vendetta Aug 06 '12 at 10:44
  • @V4Vendetta: another question with same scenario... http://stackoverflow.com/questions/10941284/how-to-insert-a-record-into-a-access-table-using-oledb –  Aug 06 '12 at 10:47
  • 1
    Yes it would work, coz only the order is required (if you say @Login for all of them it would still work), just wanted to highlight that part, Also it should accept `@Login` in the insert which makes it more readable rather than counting the `?` and then the params ! – V4Vendetta Aug 06 '12 at 10:56
  • whatever, but i would prefer ? instead of named params in case of oledb or wherever required –  Aug 06 '12 at 17:03