50

I have following code on page load event:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        con = New SqlConnection("Data Source=14GRAFICALI\\SQLEXPRESS;Initial Catalog=sagar;Integrated Security=True")
        '-----------------------fill name ddl------------------------------'

        Try

            da = New SqlDataAdapter("select EmpName from empMaster_VB", con)
            ds = New DataSet()
            da.Fill(ds)
            For i As Integer = 0 To ds.Tables(0).Rows.Count

                ddlName.Items.Add(ds.Tables(0).Rows(i)(0).ToString())

            Next


        Catch ex As Exception

        End Try

        '--------------------------------------------------------------------'


        '----------------fill expence-------------------------------------'

        Try

            da = New SqlDataAdapter("select ExpName from expenceType_VB", con)
            ds = New DataSet()
            da.Fill(ds)
            For i As Integer = 0 To ds.Tables(0).Rows.Count

                ddlExpence.Items.Add(ds.Tables(0).Rows(i)(0).ToString())

            Next


        Catch ex As Exception

        End Try


        '---------------------------------------------------------------'



    End Sub

This code is to fill drop downs with names and expence values in database tables.

I am getting 'instance failure' error while executing the code.

I checked one of the answers on stack and checked my connection string. But, my connection string is also correct.

Please help me if anything else is missing in this code.

Community
  • 1
  • 1
C Sharper
  • 8,284
  • 26
  • 88
  • 151

8 Answers8

70

As you got the error "instance failure", that might be the error with your SQL Server instance..

Make sure your SQL Server instance(MSSQLSERVER) is running, where you can check in: Services list. TO get into services list: open run dialog box and type: "services.msc" (without quotes) and hit Enter. That takes you to services management console, where you can check whether your instance in running or not..

If the problem still persists, then try using: Data Source=.\SQLEXPRESS instead.. :)

Happy Coding... :)

Vidhya Sagar Reddy
  • 1,521
  • 1
  • 13
  • 25
43

I have the following connection:

Data Source=MyComputerName\SQL2012ENTERPRS;Initial Catalog=RESTFUL; User Id=myuser; Password=mypass123;

My server's identifier is: MyComputerName\SQL2012ENTERPRS

But since I'm use a string, I add another blackslash \ so the string becomes:

public string connectionString = "Data Source=DAFWKN409C67Q\\SQL2012ENTERPRS;Initial Catalog=RESTFUL; User Id=rest_user; Password=rest_pwd_01;";

I have forgotten that I must remove one of \ since I am not use the default string block, I use an XML file to save my connection string and then everything is okay so my suspicion is that your instance name is not correct.

This is my connection string it I use on a local pc using SQL Express:

string servername = @"Data Source=.\SQLExpress;Initial Catalog=Workshop;Integrated Security=True";

You should modify the above string with your server name and instance name and ensure it is correct.

Skully
  • 2,882
  • 3
  • 20
  • 31
toha
  • 5,095
  • 4
  • 40
  • 52
28

I had this issue because I got the connection string from appsettings.Development.json:

"Server=msi\\DataBaseName;Database=Super25;Trusted_Connection=True;"

but when I changed to

"Data Source=msi\DataBaseName;Initial Catalog=Super25;Integrated Security=True;"

solved!

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Almeida
  • 1,254
  • 12
  • 26
26

in my case just kick up the double \\ to one slush \ :=)

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
  • 1
    I didn't update my exe configuration file and some how this got updated with two back slashes. I checked my source control and it was a single slash up until this started happening. You were right on the money; thanks. – Zonus Aug 26 '19 at 15:09
  • 1
    this worked for me, many time in c# code we pur @ then there is no need of \\ just use \ if @ is preceding the connection string. – Tushar Kshirsagar Oct 27 '19 at 17:26
  • Worked like a charm. Two slashes were fine in ASP.NET Core project, but crashed in plain ASP.NET. – Eternal21 Jul 24 '20 at 15:19
10

I shared an image for figure out the issue on my EF Core 3.0 in a Separate Class Library. The root cause is Regular literal ("Backslash: \") and Verbatim literal ("@"Backslash: \"") . Reference https://csharpindepth.com/Articles/Strings

L.Guan
  • 211
  • 1
  • 4
  • 5
5

Use the wildcard "@" before "Data Source" declaration. someting like this: connetionString = @"Data Source=...

So you'll be able to use only a back slash. Like this: @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\jcabarros...

JBarros
  • 101
  • 1
  • 3
2

Your answer lies in the connected services. edit provider and select the three dots once you find your database select advanced in the advanced you will see your connection string which looks like below.

Data Source=Server\ServerInstance;Initial Catalog=yourDatabaseName;Integrated Security=True

You can add more services and configure more there.

sdw
  • 21
  • 2
1

I know this is an old thread, but perhaps a good update. I couldn't find a good answer on the web search.

I got the same 'Instance Failed' error when trying to reference a DbContext from another project in the same solution. The 1st project had a DbContext and the connection string, and the 2nd project in the solution was trying to reference it. The 1st app ran ok on its own, issue only occurred when running the 2nd app.

The issue was that the app.config file that had the connection string from the 1st project was not in view/scope of the 2nd project. So the connection string wasn't assembled.

My solution was to copy the app.config from the first project to the second project.

This was on VS 2019.

Hope this helps

CloudEmber
  • 99
  • 4