1

Im encountering a problem when setting sqlconnection. this is my Web.Config file :

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <connectionStrings>
    <add name="SimpleDB"
         connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\FluksikartoN\Documents\SimpleDB.mdf;Integrated Security=True;Connect Timeout=30"
         providerName=".NET Framework Data Provider for SQL Server"/>
  </connectionStrings>
</configuration> 

So basically i want to add row "Hello" to sql table named "book" which has only one string column caled "Name" on button click , but i get error : "[Win32Exception (0x80004005): The network path was not found]" and i dont see anything wrong in sqlconnection set up.

aspx.cs file :

using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Web.Services;

    namespace ProjectWWW
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            [System.Web.Services.WebMethod]     
            public static string InsertData(string ID){
                string connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:/Users/FluksikartoN/Documents/SimpleDB.mdf;Integrated Security=True;Connect Timeout=30";
                using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("Insert into Book (Name) values(@Name)", con))
                        {
                            con.Open();
                            cmd.Parameters.AddWithValue("@Name", ID);
                            cmd.ExecuteNonQuery();
                            con.Close();
                            return "True";
                        }
                    }
        }




        protected void Button1_Click(object sender, EventArgs e)
        {
            InsertData("hELLO");
        }
1
  • 2
    Just out of curiosity, why are you putting a connection string in web.config and then not using it? You're hardcoding the same connection string in code instead. Commented Jul 1, 2014 at 15:35

3 Answers 3

5

You've changed your backslashes to forward slashes. Change them back and use @ to not treat them as escape characters:

string connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\FluksikartoN\Documents\SimpleDB.mdf;Integrated Security=True;Connect Timeout=30";

or just pull it from app.config since you put it there as well:

string connectionString = ConfigurationManager.ConnectionStrings["SimpleDB"];
Sign up to request clarification or add additional context in comments.

1 Comment

holy jesus thanks this bothered me 3 days in a row im new and i hate when this kind of missunderstandings fail me
1

Why not use Web.Config connection string

string connectionString = ConfigurationManager.ConnectionStrings["SimpleDB"].ToString();

there is no need of specifying connection string on page when you have specified it in Web.Config

2 Comments

you should include System.Configuration namespace, like this using System.Configuration
add .ToString() in the end like this string connectionString = ConfigurationManager.ConnectionStrings["SimpleDB"].ToString(); , updated the ans with .ToString()
0

You can simply use like this in your .cs file

SqlConnection con = new SqlConnection(System.Configuration.CofigurationManager.ConnectionString["SqlConn"].ConnectionString.ToString());

I hope this will work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.