Login failed for User

Habib ur Rehman 41 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.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,808 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 101.8K Reputation points MVP
    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.


3 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 101.8K Reputation points MVP
    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.

    0 comments No comments

  2. CathyJi-MSFT 21,096 Reputation points Microsoft Vendor
    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".

    0 comments No comments

  3. Habib ur Rehman 41 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 = 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.

    0 comments No comments