locked
Unhandled Exception Error - Login Failed for User RRS feed

  • Question

  • Hello,

    I am receiving an error when I run my application stating “Unhandled exception has occurred. If you click continue, the application will ignore the error………..Login failed for MyUser.

    When I click the Continue button the application runs and seems to work properly. This application used to run without receiving the error and I did not make any changes. I have another application that is using basically the exact same code and does not receive the error. The only difference is the application is querying a different DB but using the exact same connection string. The error only occurs when I run the exe file created from the build. When I run (debug) the application from within the VS 2013 IDE I do not receive the error.

    Here are the details to the error message:

    ************************************************************************************************

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.Data.OleDb.OleDbException (0x80040E4D): Login failed for user 'jobrunner'.
       at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.OleDb.OleDbConnection.Open()
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
       at LoanOrig_FDIC_Codes.DataSet1TableAdapters.I_LoansTableAdapter.Fill(I_LoansDataTable dataTable) in u:\Visual Studio Projects\LoanOrig_FDIC_Codes\LoanOrig_FDIC_Codes\LoanOrig_FDIC_Codes\DataSet1.Designer.cs:line 1668
       at LoanOrig_FDIC_Codes.Form1.Form1_Load_1(Object sender, EventArgs e) in u:\Visual Studio Projects\LoanOrig_FDIC_Codes\LoanOrig_FDIC_Codes\LoanOrig_FDIC_Codes\Form1.cs:line 152
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    LoanOrig_FDIC_Codes
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///U:/Visual%20Studio%20Projects/LoanOrig_FDIC_Codes/LoanOrig_FDIC_Codes/LoanOrig_FDIC_Codes/bin/Debug/LoanOrig_FDIC_Codes.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34238 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34234 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Data
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    System.Data.DataSetExtensions
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.DataSetExtensions/v4.0_4.0.0.0__b77a5c561934e089/System.Data.DataSetExtensions.dll
    ----------------------------------------
    System.Numerics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

    ****************************************************************************************************

    I'm not sure what the JIT debugger is but to my knowledge my other apps are not using that. The production username and password are correct in the connection string so I'm not clear why I am receiving this error especially since I'm not receiving the error on my other application. I have also attached the code just in case it is needed. Any assistance will be greatly appreciated. Thank you.

    Dave

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using ClosedXML.Excel;
    using DocumentFormat.OpenXml;
    using System.IO;
    
    namespace LoanOrig_FDIC_Codes
    {
        public partial class Form1 : Form
        {
            SqlCommand sqlCmd;
            SqlDataAdapter sqlDA;        
            DataSet sqlDS;
            DataTable sqlDT;
            SqlCommand sqlCmdCnt;
                
            public Form1()
            {
                InitializeComponent();
            }
    
            
                //BEGIN BUTTON LOAD CLICK EVENT
            private void btnLoad_Click(object sender, EventArgs e)
            {
                string sqlCon = "Data Source=FS-12345; Initial Catalog=ExtractGenerator; User ID=MyUser; Password=MyPW";
          
               //Set the 2 dateTimePickers to today's date
                DateTime @endDate = End_dateTimePicker.Value.Date;           
                DateTime @startDate = Start_dateTimePicker.Value.Date;
    
                //Validate the values of the 2 dateTimePickers
                if (endDate < startDate)
                {
                    MessageBox.Show("End Date must be greater than or equal to the Start Date OR Start Date must be less than or equal to the End Date ", "Incorrect Date Selection",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    //Reset both dateTimePickers to todays date
                    Start_dateTimePicker.Value = DateTime.Today;
                    End_dateTimePicker.Value = DateTime.Today;
                    return;                                                
                }
                //End of date validation                   
    
                string sqlData = @"SELECT AcctNbr, 
                                 CurrAcctStatCD, 
                                 Org, 
                                 MJAcctTypCD, 
                                 MIAcctTypCD, 
    	                         NoteOriginalBalance, 
                                 ContractDate, 
                                 FDICCATCD, 
                                 FDICCATDESC,
    	                         PropType, 
                                 PropTypeDesc
                                 FROM I_Loans
                                 WHERE CAST(ContractDate AS datetime) BETWEEN @startdate AND @enddate ORDER BY ContractDate";   
                SqlConnection connection = new SqlConnection(sqlCon);            
                SqlCommand sqlCmd = new SqlCommand(sqlData, connection);
                sqlCmd.Parameters.AddWithValue("@startDate", startDate);
                sqlCmd.Parameters.AddWithValue("@endDate", endDate);
                sqlDS = new DataSet();
                sqlDA = new SqlDataAdapter(sqlCmd); //SqlAdapter acts as a bridge between the DataSet and SQL Server for retrieving the data
                connection.Open();            
                sqlDA.SelectCommand = sqlCmd; //SqlAdapter uses the SelectCommand property to get the SQL statement used to retrieve the records from the table          
                sqlDA.Fill(sqlDS, "I_Loans"); //SqlAdapter uses the "Fill" method so that the DataSet will match the data in the SQL table
                sqlDT = sqlDS.Tables["I_Loans"];
    
                //Code section to get record count
                sqlCmdCnt = connection.CreateCommand();
                sqlCmdCnt.CommandText = "SELECT COUNT(AcctNbr) AS myCnt FROM I_Loans WHERE ContractDate BETWEEN @startDate AND @endDate";
                sqlCmdCnt.Parameters.AddWithValue("@startDate", startDate);
                sqlCmdCnt.Parameters.AddWithValue("@endDate", endDate);                            
                int recCnt = (int)sqlCmdCnt.ExecuteScalar();
                txtRecCnt.Text = recCnt.ToString();
                btnExport.Enabled = true;            
                //End of code section for record count
                            
                connection.Close();
    
                dataGridView1.DataSource = sqlDS.Tables["I_Loans"];
                dataGridView1.ReadOnly = true;
                
                //Reset both dateTimePickers to todays date
                Start_dateTimePicker.Value = DateTime.Today;
                End_dateTimePicker.Value = DateTime.Today;
            
            }
            //END BUTTON LOAD CLICK EVENT 
           
            //BEGIN BUTTON EXPORT CLICK EVENT
            private void btnExport_Click(object sender, EventArgs e)
            {
                 SaveFileDialog saveFD = new SaveFileDialog();
                {
                    {   //ClosedXML code to export datagrid result set to Excel                                                                 
                        string dirInfo = Path.GetPathRoot(@"\\FS-03250\users\dyoung\LoanOrig_FDIC_Codes");                    
                        if (Directory.Exists(dirInfo))
                        {
                            var wb = new XLWorkbook();
                            var ws = wb.Worksheets.Add(sqlDT);
                            ws.Tables.First().ShowAutoFilter = false;
    
                            //SaveFileDialog saveFD = new SaveFileDialog(); //I moved this variable to the top of the btnExport code block so that the variable will be available thru the entire scope of that code block
                            saveFD.Title = "Save As";
                            saveFD.Filter = "Excel File (*.xlsx)| *.xlsx";
                            saveFD.FileName = "LoanOrig_FDIC_Codes_" + DateTime.Now.ToString("yyyy-MM-dd");                       
                            if (saveFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                Stream stream = saveFD.OpenFile();
                                wb.SaveAs(stream);                            
                                stream.Close();                                                      
                            }
                            else if (saveFD.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                            {
                                MessageBox.Show("Save file operation has been canceled", "Save As Canceled", MessageBoxButtons.OK, MessageBoxIcon.Stop);                        
                            return;
                            }
                            //End of ClosedXML code
                            MessageBox.Show("File has been exported to " + saveFD.FileName, "File Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
                            //MessageBox.Show("File has been exported to U:\\LoanOrig_FDIC_Codes", "File Exported", MessageBoxButtons.OK, MessageBoxIcon.Information);                                                
                        }
                            else
                            MessageBox.Show("Drive " + saveFD.FileName + " " + "not found, not accessible, or  you may have invalid permissions");
                            //MessageBox.Show("Drive " + "U:\\Visual Studio Projects\\LoanOrig_FDIC_Codes" + " " + "not found, not accessible, or  you may have invalid permissions");
                           return;
                        //END THE SAVE AS PROCESS 
                    }
                }
    
            }   
            
            //END BUTTON EXPORT CLICK EVENT 
            
            
            private void Form1_Load(object sender, EventArgs e)
            {
                //Set dates to be today's date when the form is openend
                Start_dateTimePicker.Value = DateTime.Today;
                End_dateTimePicker.Value = DateTime.Today;
            }
    
            private void Form1_Load_1(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'dataSet1.I_Loans' table. You can move, or remove it, as needed.
                this.i_LoansTableAdapter.Fill(this.dataSet1.I_Loans);
    
            }
    
            private void iLoansBindingSource_CurrentChanged(object sender, EventArgs e)
            {
    
            }
            
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }                    
        }
    }
    


    David Young

    Tuesday, April 21, 2015 4:19 PM

Answers

  • In SQL you have a login which allows access to the server but you must also have permissions to access each database that you will use (including Master). So I would verify that your user also has the necessary permissions on the database itself and not just the SQL instance. The error you're getting is from SQL saying that the user doesn't have permissions.  If it works for 1 DB but not the other on the same SQL instance then it is likely a missing permission on the DB itself.

    I should note that you have a hard coded conn string in your code but the error is for a different user.  I'm assuming that you simply posted it there for convenience and that you changed the values to obscure the data.  If you are actually getting the info from your config file then verify the connection string is correct. Also be sure to not use Integrated Security since it appears that you are using SQL authentication.

    • Marked as answer by Kristin Xie Friday, May 8, 2015 1:30 AM
    Wednesday, April 22, 2015 1:43 PM

All replies

  • The error is coming from your SQL database and is stating that the user you're trying to connect to the DB as 'jobrunner' does not have sufficient privileges to connect to the DB.  Hence the connection fails.  You need to check with your DBA to ensure that the given user has login rights to the DB and sufficient privileges to the DB you're connecting to.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Tuesday, April 21, 2015 5:23 PM
  • Hi Michael,

    The user has a DB role of db_owner and has been granted the explicit permission of connect. Plus, I am using this exact same connection string on another application that points to a different DB but the user has the exact same role of db_owner and the permission of connect and I do not get the error on that connection. This why I am wondering why I get the error on one connection and not the other.

    Dave


    David Young

    Wednesday, April 22, 2015 12:13 PM
  • In SQL you have a login which allows access to the server but you must also have permissions to access each database that you will use (including Master). So I would verify that your user also has the necessary permissions on the database itself and not just the SQL instance. The error you're getting is from SQL saying that the user doesn't have permissions.  If it works for 1 DB but not the other on the same SQL instance then it is likely a missing permission on the DB itself.

    I should note that you have a hard coded conn string in your code but the error is for a different user.  I'm assuming that you simply posted it there for convenience and that you changed the values to obscure the data.  If you are actually getting the info from your config file then verify the connection string is correct. Also be sure to not use Integrated Security since it appears that you are using SQL authentication.

    • Marked as answer by Kristin Xie Friday, May 8, 2015 1:30 AM
    Wednesday, April 22, 2015 1:43 PM
  • Hi Dave,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

    I temporarily mark CoolDadTx's last response as an answer. You can unmark it if they provide no help.


    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Thursday, April 30, 2015 12:53 PM