inserting a record after turning identity on

Hi,
The following code is part of a class in C# Windows application.
The code tries to insert a record into the distribution table. But because identity is off I have to turn it on, insert the record, and turn identity back off after the insertion is completed.

The records which is being inserted, is copied from another table called ClosedTickets.

After inserting the  copied record into the distribution table I need to delete it from the ClosedTickets table.

As you can see, I attempted to do it all in one method, which end in a syntax error near the key word "select".
This is the first time I use the identity statement.
Please correct me where I am going wrong. Thanks



  public void RestoreClosedTicket(Int32 ID)
        {
            SqlConnection con = new SqlConnection();
             con = new SqlConnection("Server=PC;;Database=SupportProject;integrated security=True");
             SqlCommand commIDentityOn = new SqlCommand("Set Identity_Insert Distribution On", con);
             SqlCommand commIdentityOff = new SqlCommand("Set Identity_Insert Distribution off", con);
             SqlCommand comm = new SqlCommand("Insert into Distribution(ID,userID,dDate,tTime,sSubject,compName,TicketContent,desDepartmwnt)Values(Select * from  ClosedTickets where ID= @ID", con);
             SqlCommand commDelete = new SqlCommand("delete from ClosedTickets where ID = @ID", con);
             comm.Parameters.AddWithValue("@ID", System.Data.SqlDbType.Int);
             comm.Parameters["@ID"].Value = ID;
             con.Open();
             commIDentityOn.ExecuteNonQuery();
             comm.ExecuteNonQuery();
             commDelete.ExecuteNonQuery();
             commIdentityOff.ExecuteNonQuery();
             con.Close();
     
         
        }
adamtraskAsked:
Who is Participating?
 
dsackerContract ERP Admin/ConsultantCommented:
When using a SELECT in an INSERT, you don't use the VALUES. Once you close out your explicit field list, simply start your SELECT.
0
 
adamtraskAuthor Commented:
Thank you.... but now I have syntax error near @ID

Here is how i rewrote the insert/select statement

  SqlCommand comm = new SqlCommand("Insert into Distribution(ID,userID,dDate,tTime,sSubject,compName,TicketContent,desDepartmwnt)(Select * from  ClosedTickets where ID= @ID", con);
0
 
dsackerContract ERP Admin/ConsultantCommented:
@ID probably doesn't exist. You need to concatenate that on C# style.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.