Click here to Skip to main content
15,434,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
error is


System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
at System.Data.SqlClient.SqlConnection.GetOpenConnection(String method)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at WebService.db_insert(String UserName, Int32 countryId, String IsActive) in e:\mine\WebSite8\App_Code\WebService.cs:line 46



programme


[WebMethod]
public void db_insert(string UserName ,int countryId ,string IsActive)
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
string sql = string.Format(@"INSERT INTO [Table_1].[TestDB]
([UserName]
,[countryId]
,[IsActive])
VALUES
('{0}'
,'{1}'
,'{2}')", UserName, countryId, IsActive);
SqlCommand cmd = new SqlCommand("insert into Table_1 values(@a,@b,@c)", con);
cmd.Parameters.AddWithValue("@a", UserName); cmd.Parameters.AddWithValue("@b", countryId); cmd.Parameters.AddWithValue("@c", IsActive); cmd.ExecuteNonQuery();
}
Posted
Comments
[no name] 20-Aug-14 2:33am     CRLF
Your error clearly states that connection object which you have created is closed.. ExecuteNonQuery requires an open and available Connection. The connection's current state is closed. Open the connection...
harshavardhan12345678 20-Aug-14 2:45am    
after that its display blank page

1 solution

C#
[WebMethod]
public void db_insert(string UserName ,int countryId ,string IsActive)
{
SqlConnection con = new SqlConnection("Data Source=SREENI\\SQLEXPRESS;Initial Catalog=TestDB;User ID=sa;Password=sa123");
string sql = string.Format(@"INSERT INTO [Table_1].[TestDB]
([UserName]
,[countryId]
,[IsActive])
VALUES
('{0}'
,'{1}'
,'{2}')", UserName, countryId, IsActive);
SqlCommand cmd = new SqlCommand("insert into Table_1 values(@a,@b,@c)", con);
cmd.Parameters.AddWithValue("@a", UserName); 
cmd.Parameters.AddWithValue("@b", countryId); 
cmd.Parameters.AddWithValue("@c", IsActive); 
if(con.State == ConnectionState.Closed)
   con.Open();
cmd.ExecuteNonQuery(); 
con.Close();
} 
 
Share this answer
 
Comments
harshavardhan12345678 20-Aug-14 2:44am    
after That it display blank page
Mayank Vashishtha 20-Aug-14 2:47am    
Yeah So?? What are you asking?
harshavardhan12345678 20-Aug-14 2:54am    
hmm then how can we use that page
harshavardhan12345678 20-Aug-14 2:54am    
do u got it wt iam saying
Mayank Vashishtha 20-Aug-14 2:56am    
Nope!

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



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