Friday, January 18, 2013

Cannot connect to local SQL Server Express 2008 R2

Some times while using SQL server R2 on local machines we get a connection error something looking like


TITLE: Connect to Server
Cannot connect to (local).
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)

To deal with it, there are two things we need to check
  1. Try this as your server: .\SQLEXPRESS
  2. This error occurs if SQLEXPRESS instance is not started. To verify the service is started or not run services.msc and look for  "SQL Server (SQLEXPRESS)" service, status should be "Started", if it is not, try to start it manually.
Submit this story to DotNetKicks

Read more...

Wednesday, January 9, 2013

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized

ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized

While using transaction in Asp.net/C# I got above error.

To solve this error I simply added Transaction object in SqlCommand like below
SqlTransaction Transaction;
conn.Open();
Transaction = conn.BeginTransaction(); 
SqlCommand cmd = new SqlCommand(sql, conn, Transaction);

Submit this story to DotNetKicks

Read more...