0

I am trying to run my web app through Visual studio code on my Windows 10 laptop ( http://localhost:3000/ ) and connect to a service which is developed through asp.net in Visual studio ( http://localhost:44389/ ) in the same machine.

When the app runs, I get this error:

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.

I tried to add the service to SQL Server security by using this command based on this post:

IIS not able to connect to LocalDB

 CREATE LOGIN [IIS APPPOOL\MyAppPool] FROM WINDOWS;
 CREATE USER MyAppPoolUser FOR LOGIN [IIS APPPOOL\MyAppPool];

I replaced MyAppPool with the name of my project (ABC from ABC.dll) but I get this error:

Msg 15401, Level 16, State 1, Line 4
Windows NT user or group 'IIS APPPOOL\ABC' not found. Check the name again.

I even tried this one but failed too:

CREATE LOGIN [IIS APPPOOL\localhost:44389/] FROM WINDOWS;
CREATE USER MyAppPoolUser FOR LOGIN [IIS APPPOOL\localhost:44389];

I have already activated IIS on my machine, and since in parts of the code we have used the dataset, the connection string is set like this:

 "Data Source=127.0.0.1;Initial Catalog =XXDB ;Integrated Security = True"

Any advice is appreciated.

albert sh
  • 1,095
  • 2
  • 14
  • 31
  • The error message suggests a connectivity problem rather than security. Add the database connection string to your question. – Dan Guzman Oct 30 '22 at 17:15
  • Can you verify clearly where you hosted the web app, IIS or IIS Express? Otherwise, what you did on IIS side is irrelevant. Web apps on IIS Express run under your own account, and that's different from web apps on IIS. More can be found in https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 – Lex Li Oct 30 '22 at 17:38
  • @LexLi, Thank you for your comment, since I am running on my local machine, IIs express is used by Visual studio. Maybe there was no need to activate IIS in the machine right? I thought it has to be activated for IIS express. – albert sh Oct 30 '22 at 18:01
  • What kind of SQL Server are you using? Not all instances support `Data Source=127.0.0.1` (SQL Server Express or LocalDB). Again, hitting the same error message doesn't mean you found something useful, as your case can be quite different from others. – Lex Li Oct 30 '22 at 18:04
  • @LexLi, I am using sql server express – albert sh Oct 31 '22 at 02:30

1 Answers1

1

Hum, if everything is running local on your development box?

just use Visual studio to connect to sql server. It will do all the "dancing" for you.

So, in your vs project, just use the settings, and lets VS do this for you.

So, in your project, go Project->"ProjectName" Properties.

And then settings

And then you get this screen:

enter image description here

So, lets connect to a database called TEST4 (might as well call the connection TEST4).

With above, then you can use the wizards to setup a connection.

You can of course "always" connect to SQL server on your local machine using windows authentication, since you are the "super user" of your own machine.

So, lets do that:

So, from above, we can click on the [...] button in settings, and build the connection.

I VERY strong suggest you install SQL server express for this setup.

Ok, so click on this:

enter image description here

And now we can just follow the wizard to setup a connection.

In the drop down, your local instance of SQL server should appear.

Like this:

enter image description here

If you local server DOES NOT appear, then

Make sure you are running the so called "browser" service for SQL server (has nothing to do with web browsers!).

That would be this service:

enter image description here

Also, while you at this, make sure tc/ip is turned on. (you don't normally have to do this, but I recommend you do).

So, on SQL Server Network configuration, you see this:

enter image description here

So, as long as you KNOW sql server is running.

And again, without question, we assume you have SQL management stuido installed, right?

(Visual studio, SQL server express, and the SQL management studio are all free).

Ok, so back to the simple connection wizard that we have to just click away with the mouse to setup?

Then this:

enter image description here

and after selecting the database, then do hit test connection.

eg this:

enter image description here

So, you do not have to do all those steps you note.

IIS will connect fine to sql server, you just have to make sure SQL server is running, and there is ZERO ZERO ZERO ZERO use to try all kinds of permission settings to IIS when above will suffice.

And I would suggest that you install SQL server in mixed mode (both windows authenicatiaon and also sql server auntheication.).

For testing and dev, you can simple use the above windows authentication, but you might want to add a logon user using SSMS, and thus connect using a sql logon you created.

But, even on production servers? I have NEVER had to muck around with creating permissions in IIS to allow it to connect to sql server. All you require is a valid connecton string, and you should be good to go.

Ok, so now that I created the above connection?

Then in code I can use it this way.

Let's drop in a grid view into the aspx page like this:

        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>

And code behind is this:

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

    If Not IsPostBack Then
        LoadGrid
    End If

End Sub


Sub LoadGrid()

    Dim strSQL As String =
        "SELECT FirstName, LastName, City, Description, HotelName
        FROM tblHotelsA ORDER BY HotelName"

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            Dim rstData As New DataTable
            rstData.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rstData
            GridView1.DataBind()
        End Using
    End Using

End Sub

And now we get this:

enter image description here

So, make sure SQL server is up and running.

Make sure you can use/connect from SSMS.

Make sure you running the sql browser serivce.

Turn on tc/ip.

And then use the wizards in VS to connect and test the connection BEFORE you start coding away.

The above building of the connection also means we have a system wide connection to use (and no messy connection strings all over in code).

Even better bonus points? The project->settings when you build a connection with the wizard? It ALSO places that connection inside of web config for you, further saving hassle and world poverty.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Thank you so much Albert for your detailed answer, as usual as you are so accurate in answering, it is the second time that I see you answer my questions and always have guided me correctly. After doing the steps above, I get CORS error : Access to XMLHttpRequest at 'http://localhost:44389/api/Users/isValid' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource., Is there any thing that I have missed or it is new issue to be considered? – albert sh Oct 31 '22 at 02:29
  • In my example was a simple aspx page and we are making sure the web site project can freely connect to local instance of sql server express. (thats all we did above). Once that is working then I assume you are/will create a web service on that site, and then call that web service (usually from some JavaScript in a web page (client-side code)). However, at this point, we are assuming the web service call is into this one same project (and with a valid connecting to sql server). If you going to have TWO web sites running, then that's significantly difficult using vs + IIS express. – Albert D. Kallal Oct 31 '22 at 03:27
  • If you have two separate sites, (and with two different port numbers, this suggests as such). So, now issue becomes is that 2nd site on your dev computer also? You may well have to run 2 copies of vs, and ensure that the other site is also running. (unless the site is not running local????). You not explained how you managed (or are) running two sites at the same time on your dev box?? Only one web site runs on local development computer as a general rule. You might get two sites running, but if that is a requirement, then I would start running IIS full edition, and not IIS express. – Albert D. Kallal Oct 31 '22 at 03:30
  • Thank you Albert, Yes I am running both of them on the same machine. the web service is developed using VS .net and the front is developed using VS Code. I also followed this link but didnt help : https://davidsekar.com/asp-net/cors-development-in-localhost – albert sh Oct 31 '22 at 03:38
  • 2
    @albertsh to resolve CORS issues on IIS Express, you might follow https://github.com/lextm/iisexpress-cors – Lex Li Oct 31 '22 at 03:47
  • Ok, then are you using IIS or IIS Express? (setting up IIS Express to run two sites is rather painful. So, Lix Li suggests a link - but hopefully you sure that the database connection stuff is working. So, now you're on to part 2 (the CORS issue - I did not test that idea. When I need IIS, then I have a VM on my dev box that I launch, and run the web site under full version of IIS, since IIS express while lovable and useful during development, it not the instant when you need more than one site running). – Albert D. Kallal Oct 31 '22 at 03:51