0

i've created a database and called it "database1", now i went to web.config to set the the connection string, this is what i've added :

    <add name="dbconstring"
 connectionString=" Server=(local);Integrated Security=SSPI;Database=Database1;"
 providerName="System.Data.SqlClient" />

and when i run this code :

 System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbconstring"].ToString());

        con.Open();
        string strqry = "INSERT INTO data (first,second) VALUES (value1,value2)";
        System.Data.SqlClient.SqlCommand myCom = new System.Data.SqlClient.SqlCommand(strqry, con);
        int numrow = myCom.ExecuteNonQuery();
        con.Close();

note: there's a table called "data" with two columns "first" and "second" in the database.

i get error says "The system cannot find the file specified"

and if i try to view the table i get this :

enter image description here

any idea ?

1 Answer 1

2

value1 and value2 are strings in this example so just escape them. Use following code:

INSERT INTO data (first,second) VALUES ('value1','value2')
Sign up to request clarification or add additional context in comments.

4 Comments

can you post the ddl of the table?
sorry im new but where i can find that, i've created the table manually from the SQL window in visual studio.
@j3tb1ack Although he did need to quote those strings, that's still not the problem. Neither is the DDL of the table relevant to this issue. The problem is it's not finding his database file. My guess is that his connection string is incorrect but I am not a SQL Server expert.
Well if the connection string is the issue then here is an example of sql express connection strings (assuming you are using sql express): <add name="dbconstring" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|somedb.sdf; User Instance=true" providerName="System.Data.SqlClient" />

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.