Last post Jun 14, 2011 09:03 AM by tonyjoanes
May 27, 2011 07:45 AM|tonyjoanes|LINK
I have some code that calls a stored procedure that carries out an insert into my database. I just couldn't get the insert to work until I added a data adapter and call the fill method. I don't understand why I have had to do this as I just want to execute
the stored procedure. Code is below, anyone got any ideas?
service = ApplicationContext.Current.Services.Get<IDataService>();
(IDbConnection connection = service.GetConnection())
(IDbCommand command = connection.CreateCommand(_categorySetupProc))
// Create parameters
providerFactory = service.GetDbProviderFactory();
adapter = providerFactory.CreateDataAdapter();
adapter.SelectCommand = (
was an error inserting the new category: " + ex.Message);
May 29, 2011 10:54 PM|Decker Dong - MSFT|LINK
Show us your Stored procdure please. From your codes I cannot decide which reason causes the problem
Jun 13, 2011 06:17 AM|tonyjoanes|LINK
Here is the proc
'Audi Stockmarket, Branch Contact Editors'
-- Get the team seccodeid that we need to give access to this category
-- Insert the new category and get the id
-- Get the new category id
-- Insert the category security values
seccodeid from @seccodeid)
Jun 13, 2011 10:22 AM|tonyjoanes|LINK
Weirdly, ExecuteScalar() does work but ExecuteNonQuery() doesn't.
Jun 13, 2011 09:05 PM|Decker Dong - MSFT|LINK
It seems that in your Stored Procdure you've got two things——
1) Do inserting.
Generally speaking, programming's execution is always depending on the nearest (last) step. Becuase your last step is fetching data contents, and you can just call "ExecuteReader()" or "ExecuteScaler()". And if you call "ExecuteNonQuery()", maybe the select
won't be executed properly, which may cause the problem of inserting.
Jun 14, 2011 09:03 AM|tonyjoanes|LINK
Yea could be the reason. ExecuteScalar certainly works so I guess I will use this.