Updating Primary Key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dotNetDummi
    New Member
    • Nov 2007
    • 9

    Updating Primary Key

    Hi experts,

    (I hope I didn't post my question in the wrong category of the forum)
    I am using in C#, Asp.Net, SQL Server for my project.
    I have a situation whereby I need to update a primary key.

    In my table, I have these columns:
    proj_id, emp_name, emp_email

    I have the following method

    public static void updatePID(Strin g newpid,String oldpid)
    {
    SqlConnection conn = new SqlConnection(c onnString);
    SqlCommand comm = new SqlCommand("Upd ate table_name set proj_id = newpid where proj_id = oldpid");
    conn.Open();
    comm.Connection = conn;
    comm.Parameters .AddWithValue(" @projectid", newpid);
    comm.Parameters .AddWithValue(" @projectid", oldpid);

    comm.ExecuteNon Query();
    conn.Close();
    }


    I have a problem with the following two statements.
    comm.Parameters .AddWithValue(" @projectid", newpid);
    comm.Parameters .AddWithValue(" @projectid", oldpid);

    My connection with the database and the variables "newpid" and "oldpid" values are correct.
    May I know if there is any other methods to update a primary using the old primary key? Thank You in Advance.
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    try the following code will help you

    public static void updatePID(Strin g newpid,String oldpid)
    {
    SqlConnection conn = new SqlConnection(c onnString);
    SqlCommand comm = new SqlCommand("Upd ate table_name set proj_id = " + newpid + " where proj_id = " + oldpid);
    conn.Open();
    comm.Connection = conn;

    comm.ExecuteNon Query();
    conn.Close();
    }

    Comment

    • dotNetDummi
      New Member
      • Nov 2007
      • 9

      #3
      Ok! Thank you ver much!!!

      Comment

      Working...