I get this error when trying to connect to SQL Server:
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)
I wish I could be more specific with what I am doing but I'm so confused. All I can say is that I am trying to write data to a SQL Server database. This is my code:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source = DESKTOP-4HV953I; initial catalog= VyPr-Logins");
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", textBox1.Text);
cmd.Parameters.AddWithValue("@Password", textBox2.Text);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i != 0)
{
MessageBox.Show(i + "Data Saved");
}
}
App.Config or Web.ConfigC# Database Connection Stringsalso change this code to execute inside a try{}catch(){}int i = cmd.ExecuteNonQuery();just usecmd.ExecuteNonQueryIf you are confused you can always do a google search plenty of simple easy to follow examples onlinesp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!