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();
}
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?