Getting error SqlException was unhandled by user code

16,373

Your connection string seems off

Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;

Using the AttachDbFilename=... element indicates you're using SQL Server Express, but the Express default installation would be using the SQLEXPRESS instance name - so your connection string should be

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;

Have you tried with this connection string? Any luck?

If that doesn't work - can you make sure what edition of SQL Server you have installed? Connecting to it in Management Studio - what do you use as server name?? And if you're connected - what does SELECT @@Version return?

Share:
16,373
Partha
Author by

Partha

Updated on June 04, 2022

Comments

  • Partha
    Partha almost 2 years

    I'm creating a Registration form for new user sign up. Im getting the following error. I searched for solution on google, but none of them helped me.

    Error : 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).

    Could you please help me out with this?

    Code :

    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from regform where username='" + TextBox1.Text + "'", con);
            SqlDataReader dr = cmd.ExecuteReader();
    
            if (dr.Read())
            {
                Label1.Text = "User Name is Already Exist";
            }
            else
            {
                Label1.Text = "UserName is Available";
            }
            con.Close();
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
            con.Open();
            String str = "Insert into regform values ( '" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
    
            Session["name"] = TextBox1.Text;
            Response.Redirect("Default.aspx");
            con.Close();
        }
    }
    
  • Habib
    Habib about 10 years
    using string.Format to format queries is still prone to SQL injection. Also the field is of numeric type, and expects single quotes around the passed value.
  • MethodMan
    MethodMan about 10 years
    Habib I agree but I am giving him an example I think that using properties in this case would be the more optimal path to take also he needs to change his Insert to utilize parameters and or create a Stored Procedure in my opinion I removed the string.Format as well..
  • Habib
    Habib about 10 years
    taken my downvote back, an example with SqlParameter would be great for OP, IMO