0

I've converted my project into 3Layers architecture,but I'm facing problem in database connectivity.Here error come

"ExecuteNonQuery requires an open and available Connection. 
  The connection's     current state is closed.

Description: An unhandled exception occurred during the 
 execution of the current web request. 
 Please review the stack trace for more information about        
 the error   and where it originated in the code. 
 Exception Details: System.InvalidOperationException: ExecuteNonQuery requires an open and 
available Connection.
  The connection's current state  is closed.
  Source Error: 
     Line 22:                 
   cmd.Parameters.AddWithValue("@USERNAME",   USERNAME);
 Line 23:                 cmd.Parameters.AddWithValue("@PASSWORD", PASS);
Line 24:                 cmd.Parameters.AddWithValue("@CONFIRMPASSWORD",   PASS2);
 Line 25:                 cmd.ExecuteNonQuery();
 Line 26:    
  Source File: C:\Users\Hameed\Documents\Visual Studio     2010\Projects\database\Data_Layer\csDL.cs    Line: 24   

Here is my data layer code

     public void SAVE(string USER, string EMAIL, string USERNAME, string PASS, string PASS2)
    {

        SqlConnection cnn = new SqlConnection("Data Source=.\\HAMEED_KHAN\\SQLEXPRESS; Initial catalog=db_compiler; Integrated security=True");
        cnn.Open();

            SqlCommand cmd = new SqlCommand("INSERT", cnn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@NAME", USER);
            cmd.Parameters.AddWithValue("@EMAIL", EMAIL);
            cmd.Parameters.AddWithValue("@USERNAME", USERNAME);
            cmd.Parameters.AddWithValue("@PASSWORD", PASS);
            cmd.Parameters.AddWithValue("@CONFIRMPASSWORD", PASS2);

            cmd.ExecuteNonQuery();
            cnn.Close();


            }

and here is my stored procedure code.I'm using microsoft sql server management 2012

      ALTER PROCEDURE [dbo].[INSERT]
      (@NAME varchar(50),
      @USER_NAME varchar(50),@EMAIL varchar(50),
      @PASSWORD varchar(50), @CONFIRM_PASSWORD varchar(50))
      AS
      BEGIN

       INSERT INTO tbl_user
     (NAME,Email,User_Name,Password,Confirm_Password) 
      VALUES(@NAME,@EMAIL,@USER_NAME,@PASSWORD,@CONFIRM_PASSWORD)


        END

Please,help me Thanks.I'm trying to resolve ,but I failed every time.

Saira
  • 123
  • 3
  • 18
  • 3
    That's a poor name for a Stored Procedure. – LarsTech Mar 25 '16 at 16:14
  • Based solely on what you've shown here, I'd suspect your connection string is not correct. – Kevin Mar 25 '16 at 16:15
  • @LarsTech procedure NAME is "INSERT" because i copy this from modify stored procedure so that's why it's written here in this form – Saira Mar 25 '16 at 16:23
  • That doesn't change my statement. The stored procedure should have a better name than that. – LarsTech Mar 25 '16 at 16:24
  • @Kevin can you tell me correct connection string, here is my connecting DB HAMEED_KHAN\SQLEXPRESS, – Saira Mar 25 '16 at 16:25
  • Unfortunately, without access to your servers I cannot. There are, however, many articles on the internet detailing how to go about properly constructing your connection string. – Kevin Mar 25 '16 at 16:33
  • If that's the string then why you put a point and a double blackslash before the HAMEED part? Go to Sql Server Management Studio, try to connect and copy the server name used in your connection string – Steve Mar 25 '16 at 17:22
  • I try this ,without double // ,but same problem remains – Saira Mar 25 '16 at 17:40

0 Answers0