none
newbie - SqlException: Login failed for user ''. RRS feed

  • Question

  • I am requesting assistance in understanding why the sample code shown below generates the following error message.  I'm just starting to work with SQL Express and Linq.  There are a few other posts which report a similar problem but they occurred several years back and I was not able to understand the problem from information provided in these posts.  I did not find any additional information in the SQL log.   Can anyone point me in the right direction?   

    The error that I've encountered is:

    System.Data.SqlClient.SqlException: Login failed for user ''.
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       ...
       at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)


    SQLExpress 2008 has been configured for windows authentication.   I am working with VS2008 SE on a computer running Vista Home Premium.  

    The sample code that generates the above error is:

    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace consoleTestSQL
    {
          class Program
          {
                static void Main( string[] args )
                {
                try
                {
                    // 1. Create the DataContext.
                    NorthWindDataContext db = new NorthWindDataContext(@"Data Source=DA4\SQLEXPRESS;Initial Catalog=NorthWind");

                    // 2. Instantiate an entity object.
                    Customer cust =
                    new Customer
                    {
                        CustomerID = "LAWN",
                        CompanyName = "Lawn Wranglers",
                        ContactName = "Mr. Abe Henry",
                        ContactTitle = "Owner",
                        Address = "1017 Maple Leaf Way",
                        City = "Ft. Worth",
                        Region = "TX",
                        PostalCode = "76104",
                        Country = "USA",
                        Phone = "(800) MOW-LAWN",
                        Fax = "(800) MOW-LAWO"
                    };
                    // 3. Add the entity object to the Customers table.
                    db.Customers.InsertOnSubmit(cust);
                    // 4. Call the SubmitChanges method.
                    db.SubmitChanges();

                    // 5. Query the record.
                    Customer customer = db.Customers.Where(c => c.CustomerID == "LAWN").First();
                    Console.WriteLine("{0} - {1}", customer.CompanyName, customer.ContactName);
                    // This part of the code merely resets the database so the example can be
                    // run more than once.
                    Console.WriteLine("Deleting the added customer LAWN.");
                    db.Customers.DeleteOnSubmit(cust);
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.ToString());
                }
                }
          }
    }




    Tuesday, May 12, 2009 10:38 PM

Answers

  • this simply means your connection string is failing to log you in.  nothing to do with linq I guess. The way I get connection strings is kind of backwards.  I go into visual studio and drop one of the data controls on the work surface, then look at the connection string visual studio figure out.  I'm guessing it will be different than what you have here.
     HTH's.

    "Data Source=DA4\SQLEXPRESS;Initial Catalog=NorthWind"


    Peter Kellner, MVP ASP.NET
    • Marked as answer by IB00 Wednesday, May 13, 2009 8:53 PM
    Wednesday, May 13, 2009 12:54 AM
  • Connection strings are not specific to LINQ to SQL they've been around since ODBC days and are used in OLEDB and the managed SQL Client.

    Login failed for user '' indicates it isn't trying a specific user-name and a quick glance at the connection string shows it isn't specifying one or providing the option to tell it to use the integrated security.

    [)amien
    • Marked as answer by IB00 Wednesday, May 13, 2009 1:36 PM
    Wednesday, May 13, 2009 4:40 AM
    Moderator
  • I don't see the connection between the error message and the actual solution.   But not worry, perhaps this will become clearer with time.   The important point is that I can move forward.   Thank you for helping out.

    Ian

    • Marked as answer by IB00 Wednesday, May 13, 2009 1:36 PM
    Wednesday, May 13, 2009 1:36 PM
  • Login failed for user ''  <- there is a user name missing there between the quotes so logically the connection would be "how do i supply a username and password".

    A bit of searching on that would lead you realise most people just use integrated security these days.  Connection strings aren't the most discoverable of tools in this IntelliSense age :(

    [)amien
    • Marked as answer by IB00 Wednesday, May 13, 2009 8:53 PM
    Wednesday, May 13, 2009 3:51 PM
    Moderator

All replies

  • this simply means your connection string is failing to log you in.  nothing to do with linq I guess. The way I get connection strings is kind of backwards.  I go into visual studio and drop one of the data controls on the work surface, then look at the connection string visual studio figure out.  I'm guessing it will be different than what you have here.
     HTH's.

    "Data Source=DA4\SQLEXPRESS;Initial Catalog=NorthWind"


    Peter Kellner, MVP ASP.NET
    • Marked as answer by IB00 Wednesday, May 13, 2009 8:53 PM
    Wednesday, May 13, 2009 12:54 AM
  • NorthWindDataContext db = new NorthWindDataContext(@"Data Source=DA4\SQLEXPRESS;Initial Catalog=NorthWind;Integrated Security=SSPI");

    [)amien
    • Marked as answer by Damien GuardModerator Wednesday, May 13, 2009 2:00 AM
    • Unmarked as answer by IB00 Wednesday, May 13, 2009 4:01 AM
    Wednesday, May 13, 2009 2:00 AM
    Moderator
  • Hello Damien,

    Your suggestion worked.   Thank you.

    How does one go about resolving such a problem on his own.   Specifically, how do you make a link between "Login failed for user '' " and adding an option to the connection string?   This does seem to be a very cryptic error message. 


    Ian
    • Marked as answer by IB00 Wednesday, May 13, 2009 2:28 AM
    • Unmarked as answer by IB00 Wednesday, May 13, 2009 3:56 AM
    Wednesday, May 13, 2009 2:28 AM
  • Connection strings are not specific to LINQ to SQL they've been around since ODBC days and are used in OLEDB and the managed SQL Client.

    Login failed for user '' indicates it isn't trying a specific user-name and a quick glance at the connection string shows it isn't specifying one or providing the option to tell it to use the integrated security.

    [)amien
    • Marked as answer by IB00 Wednesday, May 13, 2009 1:36 PM
    Wednesday, May 13, 2009 4:40 AM
    Moderator
  • I don't see the connection between the error message and the actual solution.   But not worry, perhaps this will become clearer with time.   The important point is that I can move forward.   Thank you for helping out.

    Ian

    • Marked as answer by IB00 Wednesday, May 13, 2009 1:36 PM
    Wednesday, May 13, 2009 1:36 PM
  • Login failed for user ''  <- there is a user name missing there between the quotes so logically the connection would be "how do i supply a username and password".

    A bit of searching on that would lead you realise most people just use integrated security these days.  Connection strings aren't the most discoverable of tools in this IntelliSense age :(

    [)amien
    • Marked as answer by IB00 Wednesday, May 13, 2009 8:53 PM
    Wednesday, May 13, 2009 3:51 PM
    Moderator
  • Hello Peter,

    I believe I now understand what you are referring to.   Creating a new database in VS2008 added a connection string to the 'app.config' file.   This connection string provides the details I was looking for.  

    Thanks,

    Ian

    Wednesday, May 13, 2009 8:53 PM
  • Hi Peter,

    Years later, yours is the answer that keeps on giving. 

    What a great insight to simply drop something like a DataGridView on a new Form and follow the Wizard to get the connection string.  Connection strings are so difficult and your solution is very elegant.  It fixed my issue immediately.  So glad I saw this.

    Best Regards,

    Alan

    Thursday, June 19, 2014 8:19 PM