Arch_interpreter
Arch_interpreter

Reputation: 85

set SqlConnection string properly

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");
        }

Upvotes: 1

Views: 1257

Answers (3)

Amit Kumawat
Amit Kumawat

Reputation: 602

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.

Upvotes: 0

Sid M
Sid M

Reputation: 4364

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

Upvotes: 1

D Stanley
D Stanley

Reputation: 152634

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"];

Upvotes: 5

Related Questions