0

I am trying to establish a connection between my application and sql server 2008. However, I am unable to connect as I am facing the below error.

Server Error in '/' Application.

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 15:             SqlConnection con = new SqlConnection("data source = SEKHAR-PC;database = MyDB, integrated security = SSPI");
Line 16:             SqlCommand cmd = new SqlCommand("Select * from EmployeeTable", con);
Line 17:             con.Open();
Line 18:             SqlDataReader rdr = cmd.ExecuteReader();
Line 19:             GridView1.DataSource = rdr;

Source File: C:\Users\Hp\documents\visual studio 2010\Projects\CSProject\CSProject\ADO1.aspx.cs    Line: 17 

Stack Trace: 


[SqlException (0x80131904): Login failed for user ''.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009598
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +183
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +239
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +33
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +524
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +479
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   CSProject.ADO1.Page_Load(Object sender, EventArgs e) in C:\Users\Hp\documents\visual studio 2010\Projects\CSProject\CSProject\ADO1.aspx.cs:17
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

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

Below is my code:-

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;

namespace CSProject
{
    public partial class ADO1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source = SEKHAR-PC;database = MyDB, integrated security = SSPI");
            SqlCommand cmd = new SqlCommand("Select * from EmployeeTable", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            GridView1.DataSource = rdr;
            GridView1.DataBind();
            con.Close();
        }

    }
}

I am using windows authentication. Server name = SEKHAR-PC. Could you please help me out ? I am new to ADO.NET, not getting how to proceed with this.

Sourav
  • 496
  • 2
  • 12
  • 28
  • http://stackoverflow.com/questions/7698286/login-failed-for-user-iis-apppool-asp-net-v4-0 – Steve Apr 17 '16 at 07:40

2 Answers2

0

I think the Problem is in your connection string. When you try to access sql server with windows authentication mode then the connection string should looks like this-

SqlConnection con = new SqlConnection("data source = SEKHAR-PC; initial catalog= MyDB, integrated security = true");
Khairul Islam
  • 1,207
  • 1
  • 9
  • 20
0

Please supply user name and password by providing impersonate in web.config.

<system.web>
        <identity impersonate="true" userName="domain\username" password="********"/>
    </system.web>

Please refer https://msdn.microsoft.com/en-us/library/bsz5788z.aspx for more details