question

HabiburRehman avatar image
0 Votes"
HabiburRehman asked HabiburRehman commented

Login failed for User

I get "Login failed for User" error in visual basic connection to sql-server. This error occurs when I move my database from one PC to another.
I solve this problem by using same "PC-name" & "User-Name" of my old PC in new one.
How should I avoid this I mean I want to use different "PC-name" & "User-Name" on new system. Note that my Sql Login credentials are same in both New & Old PC still it won't let me login.

sql-server-generaldotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered HabiburRehman commented

The error message is:

Cannot open database "POS" requested by the login. The login failed.

Login failed for user 'HSSS'.

There are two possible causes for this error:

- The database POS does not exist in this instance
- POS exists, but the login HSSS does not map to a user in this database.

I note that your connection string says Data Source =.. That is, you are logging on to the local instance. Obviously, just because a database exists on one machine, it does not mean that it exists on the other. So that is the first thing to check.

But I also note that you say when I move my database from one PC to another. And if that is what you have done, this can explain the problem. The mapping between login on server level and user on database level is not by name, but by SID. When you create an SQL Login on two different servers will typically have different SID - unless you take precautions.

That is, let's say you have created a login HSSS on server A, and now you want to move your database to server B. On server A, you run

SELECT suser_sid('HSSS')

When you create the login on server B, you say:

CREATE LOGIN HSSS WITH PASSWORD = '123456', SID = 0x...

where 0x... is the value you got in the first query.

In this case you have already created the login, so you need to drop it first.
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ErlandSommarskog Thank you very much this solved my problem; and it also cleared my different PC and User names issue also(Now I won't need to keep them same on any PC or new installations).
Once again a bundle of thanks.

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

How should I avoid this I mean I want to use different "PC-name" & "User-Name" on new system. Note that my Sql Login credentials are same in both New & Old PC still it won't let me login.

I'm not sure that I get this. First you say that you want to use different user-names, and then you say that your SQL login credentials are the same in both?

Are you using Windows authentication or SQL authentication? What does your connection string look like?

If you look in the SQL Server errorlog, you will find more details on why connection failed.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Cathyji-msft avatar image
0 Votes"
Cathyji-msft answered Cathyji-msft edited

Hi @HabiburRehman,

I mean I want to use different "PC-name" & "User-Name" on new system. Note that my Sql Login credentials are same in both New & Old PC still it won't let me login.

Agree with Erland. In addition, please make sure that the user account used in connection string is created in SQL server logins under security folder in new PC .
215478-screenshot-2022-06-28-102600.jpg
Please share us more detail error message for analysis.


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HabiburRehman avatar image
0 Votes"
HabiburRehman answered

@Cathyji-msft @ErlandSommarskog

Here are the details about the error.

My Connection String "Public Shared con As SqlConnection = New SqlConnection("Data Source = .; Initial Catalog = POS; User ID = HSSS; Password = 123456;")"

Error Details
https://drive.google.com/file/d/1cu5HnBNUMPy4Ip7pt5ZA-azcviG-8bqt/view?usp=sharing
Error Image
https://drive.google.com/file/d/1thZ1zjN-yVs0b4UxujLC8mWtwSvz6X6E/view?usp=sharing

'Was unable to upload file here directly due to error "No such upload." thus I used google drive.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.