I must admit I feel a little embarrassed to post this, but I don't seem to get this simple flipping application to work and I'm getting this error:

Login failed for user ''.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Login failed for user ''.

Source Error: 


Line 21:         sqlCmd.Parameters.Add("@town", town.Value);
Line 22:         sqlCmd.Parameters.Add("@price", price.Value);
Line 23:         hookup.Open();
Line 24:         sqlCmd.ExecuteNonQuery();
Line 25:         hookup.Close();

Source File: c:\Users\antonio.borrillo\Documents\Visual Studio 2012\WebSites\testing\Default.aspx.cs    Line: 23 

Stack Trace: 


[SqlException (0x80131904): Login failed for user ''.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295887
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +69
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +30
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +317
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +889
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +225
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +37
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions) +558
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions) +67
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1052
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +167
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +143
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +83
   System.Data.SqlClient.SqlConnection.Open() +96
   _Default.uploadOnDatabase(Object sender, EventArgs e) in c:\Users\antonio.borrillo\Documents\Visual Studio 2012\WebSites\testing\Default.aspx.cs:23
   System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) +116
   System.Web.UI.HtmlControls.HtmlInputButton.RaisePostBackEvent(String eventArgument) +102
   System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237

OK, so this is the application, very simple one:
aspx file

<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>SQL tests</title>
    <link href="css/styles.css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div class="wrapper">
        <div class="row">
            <label class="control-label" for="firstName" runat="server">First name<span class="mandatory">*</span></label>
            <div class="controls">
                <input id="firstName" type="text" runat="server" />
            </div>
        </div>
        <div class="row">
            <label class="control-label" for="surname" runat="server">Surname<span class="mandatory">*</span></label>
            <div class="controls">
                <input id="surname" type="text" runat="server" />
            </div>
        </div>
        <div class="row">
            <label class="control-label" for="town">Town/city<span class="mandatory" runat="server">*</span></label>
            <div class="controls">
                <input id="town" type="text" runat="server" />
            </div>
        </div>
        <div class="row">
            <label class="control-label" for="price">Price paid<span id="Span1" class="mandatory" runat="server">*</span></label>
            <div class="controls">
                <input id="price" type="text" runat="server" />
            </div>
        </div>
        <div class="row">
            <input type="Submit" id="Submit1" runat="server" onserverclick="uploadOnDatabase" />
        </div>
    </div>
    </form>
</body>
</html>

Code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    private string strInsert;
    private SqlCommand sqlCmd;
    private SqlConnection hookup;

    protected void uploadOnDatabase(object sender, EventArgs e) {
        hookup = new SqlConnection("Server=localhost\\SqlExpress;Database=tests" + "Integrated Security=True");
        strInsert = "INSERT INTO transactions(Name,Surname,Town,Price) VALUES (@name,@surname,@town,@price)";
        sqlCmd = new SqlCommand(strInsert, hookup);
        sqlCmd.Parameters.Add("@name", firstName.Value);
        sqlCmd.Parameters.Add("@surname", surname.Value);
        sqlCmd.Parameters.Add("@town", town.Value);
        sqlCmd.Parameters.Add("@price", price.Value);
        hookup.Open();
        sqlCmd.ExecuteNonQuery();
        hookup.Close();
    }
}

And screenshot of the table definition:http://s21.postimg.org/bdsw7oqw7/sql_Test.jpg
I can't understand for the life of me why I get that error. It's only that applications, I have others open and they are find and connect to the database with no problems at all. Any idea?!
The using statements seem right to me, same thing about how it connects to the database.

Recommended Answers

All 5 Replies

hookup = new SqlConnection(
"Server=localhost\SqlExpress;Database=tests" +
"Integrated Security=True");

Isn't that missing a semi-colon (after tests)?

Facepalm. And so it is. I've been looking at this code over and over, and didn't spot the flipping missing semi-colon.
Can't apologize enough...
Sorry for wasting your time on such a trivial issue. I put it down to the fact that I haven't copied and pasted but typed every single word in there. Sorry.

Just thinking about something though. Surely I can turn this around and learn a bit more about the error and the way the query is handled. So, I missed the semicolon: what effect does that have on the application? Meaning, how is the query interpreted without the semicolon? Why does the login fail?

Why does the login fail?

The login fails because it cannot connect to the database. Since the semi-colon is missing, the connection doesn't see that it has to use integrated security. That results in the error "Login failed for user ''". If integrated security is off, then you have to specify a user and password in the connection string.

Brilliant, thanks for clarifying that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.