Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have problem in asp.net when i try to insert record to my db
and i get this error
An explicit value for the identity column in table 'wiki' can only be specified when a column list is used and IDENTITY_INSERT is ON.


and i have search for solution and i get that i must write the follwing,and i don't know where to write it or if it's a right solution

SET IDENTITY_INSERT wiki OFF how to write it in asp.net

here is my code:
C#
string qry;

            //String file_id=TextBox1.Text;
            String file_name = TextBox2.Text;
            String file_size = TextBox3.Text;
            String file_url = TextBox4.Text;
            String file_category = TextBox5.Text;
            String file_view = TextBox6.Text;
         //  String emp_id = TextBox7.Text;
            SqlConnection con = new SqlConnection(@"Data Source=SNIPERGHOST-PC\SQLEXPRESS;Initial Catalog=PM;Integrated Security=True;Pooling=False");
            con.Open();
            SqlCommand cmd;
            SqlDataSource sds = new SqlDataSource();
            
            qry = "insert into wiki values('" + file_name + "','" + file_size + "','" + file_url + "','" + file_category + "','" + file_view +"','"+ "')";
           

            cmd = new SqlCommand(qry, con);

            cmd.ExecuteNonQuery();
            con.Close();

            //Image1.Visible = true;
            string ToRedirectURL = "viewfiles.aspx";
            Response.AppendHeader("REFRESH", "5;URL=" + ToRedirectURL);
            // Response.Redirect("comment.aspx",true);
Posted
Updated 18-Jun-12 7:27am
v2
Comments
Nelek 18-Jun-12 13:28pm    
Edit: Code tags

1 solution

Firstly you should be using a stored procedure to do this.

Here's a solution:

SQL
qry = "insert into wiki (column1, column2, column3, column4) values('" + file_name + "','" + file_size + "','" + file_url + "','" + file_category + "','" + file_view +"','"+ "')";


You need to specify the columns into which the data is going to go. Rename the columns to those that you are actually putting the data into.
 
Share this answer
 
Comments
Tim Corey 18-Jun-12 15:48pm    
I agree, especiall about the need for a stored procedure.
Bernhard Hiller 19-Jun-12 3:51am    
A stored procedure is not required, though it might enhance performance.
More important is a paramerterized query: SQL injection, and all the problems arising from special characters in the data to be inserted.
R. Giskard Reventlov 19-Jun-12 10:23am    
In which case offer it as an alternate solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900