2

I want to create a test case for Database Connection Error.

Any ideas how to do this?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Mark
  • 21
  • 1

1 Answers1

0

In your web.config, add a new connection string called ErrorConnectionString.

<connectionStrings>
 <add name="ErrorConnectionString" connectionString="server=192.168.1.1;user id=sa;password=WRONG_PASSWORD;database=MyDatabase"/>
</connectionStrings>

Then, to test the connection error, you can use the following code (VB.NET)

Dim connectionString = ConfigurationManager.ConnectionStrings("ErrorConnectionString").ConnectionString
Dim connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter("select * from MyTable", connection)
adapter.Fill(ds)
connection.Close()

Assuming you purposefully have incorrect login credentials in the connection string, you will get the exception thrown.

If you can't afford to modify the web.config, you can simply hardcode the invalid connection string into the connectionString variable.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225