4

I am trying to write an insert method in a C# console application. I have set a breakpoint and it does hit ExecuteNonQuery, however, after stepping through it just hangs there and control does not seem to return back to the application. When I perform a read action on the database it works perfectly.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    SqlCommand command = new SqlCommand("INSERT INTO dbo.dbDevices (ipAddress) VALUES (@value)", connection);
    command.Parameters.Add("@value", "SUCCESS");
    command.ExecuteNonQuery();
}

I have also checked the active connections to the database and it says that it is awaiting command.

.Net SqlClient Data Provider     5720       AWAITING COMMAND

More information can be provided if needed.

2
  • Have you compared the working and non-working queries with SQL Profiler? Commented Jul 12, 2015 at 3:21
  • Do you have a transaction scope around this block? ... and what number is returned by command.ExecuteNonQuery() Commented Oct 11, 2016 at 15:39

1 Answer 1

1

Not sure if it's related but you're passing parameter value using deprecated method. I recommend using AddWithValue method. So it's either:

command.Parameters.AddWithValue("@value", "SUCCESS");

or

command.Parameters.Add(new SqlParameter("@value", "SUCCESS"));
Sign up to request clarification or add additional context in comments.

5 Comments

I realised this and have changed it, thanks for the advice though : )
@Johnathon64 Did it solve your problem or you're still getting the same error?
I'm not actually getting any errors, it just hangs there
@Johnathon64 You could be getting timeouts if your table is locked for some reason or if your transaction log is full.
I just did a DBCC OPENTRAN on SSMS and it said no active open transactions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.