We help IT Professionals succeed at work.
Get Started

inserting a record after turning identity on

300 Views
Last Modified: 2015-04-10
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();
     
         
        }
Comment
Watch Question
Contract ERP Admin/Consultant
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE