Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am having trouble getting my login to work and would like some expertise, below is the coding from Visual Studios:

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;

namespace CO6009DissertationV5
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=SQL2016.FSE.Network;Initial Catalog=db_1430715_co6009_dissertation;Persist Security Info=True;User ID=user_db_1430715_co6009_dissertation");
            cn.Open();
            SqlCommand cmd = new SqlCommand("select * from tbl_users where username = '" + txtUsername.Text + "', and password = '" + txtPassword.Text + "' ", cn);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            int count = 0;

            while (dr.Read())
            {
                count += 1;
            }

            if (count == 1)
            {
                MessageBox.Show("OK");
                frmWelcome1 formDefault = new frmWelcome1();
                formDefault.Show();
            }

            else if (count > 0)
            {
                MessageBox.Show("Duplicate username amd password");
            }

            else
            {
                MessageBox.Show("Username or password is not correct");
            }

            txtUsername.Clear();
            txtPassword.Clear();

            ///this.Hide();
            //frmWelcome1 formDefault = new frmWelcome1();
            //formDefault.Show();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Database from SQL Management Studio:
CREATE table tbl_users
(
	userid int PRIMARY KEY IDENTITY,
	username varchar(50) NULL,
	password varchar(50) NULL,
	role varchar (50) NULL,
)


When I try to run the code and enter the username and password, it gives me an error at cn.Open(), saying sqlException was unhandled. Please help
.

More Detail
{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}


What I have tried:

I tried following the steps on some other sites and YouTube, but they gave me the same error. Could it be a problem with the connection string?
Posted
Updated 18-Jan-17 18:14pm
v5
Comments
Ashwin. Shetty 18-Jan-17 23:21pm    
please share complete stack trace of error
Member 12945816 18-Jan-17 23:26pm    
What do you mean?
Ashwin. Shetty 18-Jan-17 23:45pm    
stack trace of your error
Member 12945816 18-Jan-17 23:49pm    
Do you mean this?
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\Users\user\Documents\Visual Studio 2015\Projects\CO6009DissertationV5\CO6009DissertationV5\bin\Debug\CO6009DissertationV5.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'CO6009DissertationV5.vshost.exe' (CLR v4.0.30319: CO6009DissertationV5.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbo
Member 12945816 18-Jan-17 23:52pm    
This is all I got when I debugged the application.

1 solution

Can you check your SQL server instance if it is running, Also check for its port configuration. If you are able to open SQL server from some other IDE, please check the value mentioned in connection string meets the SQL server security requirement.

Connection string sample are available here
https://msdn.microsoft.com/en-us/library/ms156450.aspx#Common connection string examples
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900