I want to insert a record to SQL Server Database but I got a strange exception while ExecuteNonQuery() process. I got this exception:
ExecuteNonQuery requires an open and available Connection.
The connection's current state is closed.
However my problem is not related with this. I am definitely sure that my connection's current state is Open. My problem is one of my parameters in my query. Here is my query:
sql = "update EMAIL_CONNECTIONS " +
"set END_DATE=@EndDate, " +
"STATUS=@Status, " +
"STATUS_DATE=@StatusDate, " +
"CATEGORY_CODE=@CategoryCode, " +
"EMAILOUT_SENTDATE=@EmailOutSentDate, " +
"TO_ADDRESS=@ToAddress " +
"where SESSIONID=@SessionId " +
"and STATUS = 'Inprocess'; ";
sql += "insert into ICS_EMAIL_CONNECTIONS_TRX(SESSIONID, AGENTID, STATUS, FIELD1) " +
"values(@SessionId, @AgentId, @Status, 'Sended this mail')";
Exception throw because of the @EmailOutSentDate parameter. It does not accept Datetime format or something I dont know. When I give DateTime.Now to this parameter query runs successfully. I also tried DBNull.Value and query runs perfectly. DateTime problem is not related with the Exception.
SqlConnection _cnn = new SqlConnection();
_cnn.ConnectionString = connectionString;
_cnn.Open();
SqlCommand cmd = new SqlCommand(sql, _cnn);
cmd.Parameters.Add(new SqlParameter("@EndDate", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@Status", "Sent"));
DateTime result = DateTime.MinValue;
result = DateTime.ParseExact(result.ToString("yyyy-mm-dd HH:mm:ss.fff"), "yyyy-mm-dd HH:mm:ss.fff", null);
DateTime newdate = DateTime.SpecifyKind(result, DateTimeKind.Local);
cmd.Parameters.Add(new SqlParameter("@EmailOutSentDate", newdate));
cmd.Parameters.Add(new SqlParameter("@ToAddress", interaction.AllAttributes["To"]));
cmd.Parameters.Add(new SqlParameter("@StatusDate", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@CategoryCode", long.Parse(CategoryCode)));
cmd.Parameters.Add(new SqlParameter("@SessionId", interaction.Id));
cmd.Parameters.Add(new SqlParameter("@AgentId", Agent.AgentId));
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
As you can see I have also tried DateTime.Kind property but still same Exception thrown. Where is the problem? Any suggesstions?