user2962142
user2962142

Reputation: 2066

Connecting a SQL database to asp.net web form

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 ?

Upvotes: 0

Views: 5163

Answers (1)

j3tb1ack
j3tb1ack

Reputation: 21

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

INSERT INTO data (first,second) VALUES ('value1','value2')

Upvotes: 2

Related Questions