简体   繁体   中英

sqlexception unhandled by user code

I experience this error when I try to load my web page. Can anyone advise what cause this to happen and the remedy to it?

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

This is the C# code

string conStr = ConfigurationManager.ConnectionStrings["MSSQLConnectionString"].ConnectionString;

SqlConnection connection = new SqlConnection(conStr);

string strSqlCmd;
string strSqlCmd2;

strSqlCmd = "INSERT INTO User " + "(UserName, UserPwd, email, address, mobileNo, firstname, lastname, dateofBirth, NRIC, gender)";
strSqlCmd += " VALUES (@Username, @Userpwd, @email, @address, @mobileNo, @firstName, @lastName, @DateOfBirth, @nric, @gender)";

strSqlCmd2 = "INSERT INTO Patient " + "(Allergies, Childhoodillness)";
strSqlCmd2 += "VALUES(@allergies, @Childhood_illness)";

//create command and assign the query and connection from the constructor
SqlCommand cmd = new SqlCommand(strSqlCmd, connection);
SqlCommand cmd2 = new SqlCommand(strSqlCmd2, connection);

cmd.Parameters.AddWithValue("@Username", UserName.Text);
cmd.Parameters.AddWithValue("@Userpwd", Password.Text);
cmd.Parameters.AddWithValue("@email", Email.Text);
cmd.Parameters.AddWithValue("@address", Address.Text);
cmd.Parameters.AddWithValue("@mobileNo", MobileNo.Text);
cmd.Parameters.AddWithValue("@firstName", Password.Text);
cmd.Parameters.AddWithValue("@lastName", Password.Text);
cmd.Parameters.AddWithValue("@DateOfBirth", DOB.Text);
cmd.Parameters.AddWithValue("@nric", NRIC.Text);
cmd.Parameters.AddWithValue("@gender", Gender.Text);

cmd2.Parameters.AddWithValue("@allergies", Allergies.Text);
cmd2.Parameters.AddWithValue("@Childhood_illness", ChildhoodIllness.Text);

connection.Open();
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
connection.Close();

This is the web.config code

<add name="MSSQLConnectionString"
     connectionString="Server=localhost; Database= App_Data\PMS.mdf ; Integrated Security=True"
     providerName="System.Data.SqlClient" />

This error is self explanatory and it means that there is an error with your connection string. You may address wrong server name, or invalid database name. Also, if all these are OK, you may take a look at configuration manager and check to see if SQL Server is ready for accepting remote requests. (This is for remote connecting ONLY.)

You can get useful tips about connections strings in the following Url: http://www.connectionstrings.com/

This is incorrect connection string for local MDF database. Try something like;

<add name="MSSQLConnectionString"
 connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|PMS.mdf;Integrated Security=True;"
 providerName="System.Data.SqlClient" />

Or

<add name="MSSQLConnectionString"
 connectionString="Data Source=.\MSSQLSERVER;AttachDbFilename=|DataDirectory|PMS.mdf;Integrated Security=True;"
 providerName="System.Data.SqlClient" />

or any other instance that you have installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM