Just trying to do a simple local database connect to Microsoft SQL Server in Visual Studio. Trying to figure out why it's not connecting. I made a user specifically for this and have the correct username and password. I omitted the User Id and Password from this purposely. Seems to be throwing an error at my connection string, what am I doing wrong?
System.Data.SqlClient.SqlException: 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)
My code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assetmvc
{
public partial class SearchPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Connect to the db
string connStr = "Server=localhost;Database=AssetTracking;User Id=;Password=; ";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
// Create a command
SqlCommand cmd = new SqlCommand("SELECT [EmployeeId],[FirstName],[LastName],[HiredDate],[FiredDate],[CurrentItems],[SupervisorId],[SupervisorName] From [dbo].Employees");
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = conn;
string temp = "";
// Read from database
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
temp += reader["EmployeeId"].ToString();
temp += reader["FirstName"].ToString();
temp += reader["LastName"].ToString();
temp += reader["HiredDate"].ToString();
temp += reader["FiredDate"].ToString();
temp += reader["CurrentItems"].ToString();
temp += reader["SupervisorId"].ToString();
temp += reader["SupervisorName"].ToString();
temp +=
temp += "<br/>";
}
conn.Close();
lbl_test.Text = temp;
}
}
}