yash patel

yash patel

  • 1.1k
  • 283
  • 21k

connection.string is showing closed

Dec 5 2020 7:41 AM
below wis the code
everything is fine model state is also getting treue but when it is checking the connection string at that time why the connection string is showing closed.
I want to insert details using stored procedure.
 
static string ConnectionString = "Data Source=DESKTOP-O4FO06T/SQL2017;Initial Catalog=practical;Integrated Security=True";
public static int InsertUserDetails( UserModel obj )
{
int rowAffected = 0;
using (IDbConnection con = new SqlConnection(ConnectionString))
{
if (con.State == ConnectionState.Closed)
con.Open();
DynamicParameters parameters = new DynamicParameters();
parameters.Add("@Name", obj.Name, DbType.String);
parameters.Add("@Gender", obj.Gender, DbType.String);
parameters.Add("@Address", obj.Address, DbType.String);
parameters.Add("@Mobile", obj.Mobile, DbType.String);
rowAffected = con.Execute("InsertUserDetails", parameters, commandType: CommandType.StoredProcedure);
}
return rowAffected;
}

Answers (2)