Login failed for User

Habib ur Rehman 21 Reputation points
2022-06-27T17:45:45.307+00:00

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.

Developer technologies | VB
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 137.7K Reputation points MVP Volunteer Moderator
    2022-06-28T21:09:39.293+00:00

    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.

    Was this answer helpful?


  2. Habib ur Rehman 21 Reputation points
    2022-06-28T16:54:12.177+00:00

    @CathyJi-MSFT @Erland Sommarskog

    Here are the details about the error.

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

    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.

    Was this answer helpful?

    0 comments No comments

  3. CathyJi-MSFT 22,281 Reputation points Microsoft External Staff
    2022-06-28T02:29:10.013+00:00

    Hi @Habib ur Rehman ,

    > 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".

    Was this answer helpful?

    0 comments No comments

  4. Erland Sommarskog 137.7K Reputation points MVP Volunteer Moderator
    2022-06-27T21:58:53.033+00:00

    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.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.