Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This has me so frustrated, because this SP works fine executing in SSMS, but will execute 1 out of 5 times using my project. I am just going to post my code and see if anyone sees something I don't. I am using Sql Azure DB:

//get variables needed
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
Guid newUserGuid = (Guid)newUser.ProviderUserKey;

            //Insert Initial Registration data
SqlConnection cn = new    SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
SqlCommand cmd = new SqlCommand("dbo.spInsertInitialUserConfirmationInfo", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@fkUserId", newUserGuid.ToString());
            cn.Open();
            cmd.BeginExecuteNonQuery();
            cn.Close();
Posted
Comments
David_Wimbley 12-Oct-13 11:41am    
Are you getting an exception? If so can you paste the stack trace?
JoCodes 12-Oct-13 14:46pm    
Post your exception so that it will be easy to find the error
JasonMacD 14-Oct-13 9:27am    
I have not been getting any exceptions, the inserts just dont take place.

I needed to add the cmd.EndExecuteQuery(); statement see below:


C#
cn.Open();
            IAsyncResult result = cmd.BeginExecuteNonQuery();
            try
            {
                cmd.EndExecuteNonQuery(result);
                Response.Redirect("~/MyAccount");
            }
            catch (Exception ex)
            {
                errorsOccured.Text = "An error occurred: " + ex.Message;
            }
            finally
            {
                cn.Close();
            }
 
Share this answer
 
You must clear your cmd parameters each time you call proc then add parameters again,
check the links:
Calling Stored procedures in ADO.NET[^]
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx[^]
Calling a stored procedure using ADO.NET connection[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900