Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. SQL2005 INSERT / IDENTITY help required

SQL2005 INSERT / IDENTITY help required

Scheduled Pinned Locked Moved ASP.NET
databasehelpquestionannouncementworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    munklefish
    wrote on last edited by
    #1

    * Please note i have also posted this in SQL forum as i dont know which is most appropriate. If a mod feel it is wrongly placed here i appologise, feel free to delete. Problem: Hi, I need help with an sql query please. I wish to insert a new record into a table, and instantly retreive the new records id. Is it possible to do this in one sql statement rather than an INSERT followed by the SELECT statement? My INSERT looks like this: string conn = System.Configuration.ConfigurationManager.ConnectionStrings["artisanConnectionString"].ConnectionString; SqlConnection uniqueCon = new SqlConnection(conn); //set SQL statement SqlCommand cmd = new SqlCommand("INSERT INTO [tbl_Additional] (add_Item, add_Price) VALUES ('" + TextBox10.Text + "', '" + TextBox11.Text + "') ", uniqueCon); cmd.Connection.Open(); try {//insert/ add new record //commit to database cmd.ExecuteNonQuery(); //clear form add/edit TextBox10.Text = null; TextBox11.Text = null; //confirm to user ShowMessageBox("New unique Item Added!"); //update dataset GridView2.DataBind(); } catch { ShowMessageBox("Unable to add record!!! Please try again later."); } cmd.Connection.Close(); Any help would be greatly appreciated. Thanks & Happy Easter!

    V 1 Reply Last reply
    0
    • M munklefish

      * Please note i have also posted this in SQL forum as i dont know which is most appropriate. If a mod feel it is wrongly placed here i appologise, feel free to delete. Problem: Hi, I need help with an sql query please. I wish to insert a new record into a table, and instantly retreive the new records id. Is it possible to do this in one sql statement rather than an INSERT followed by the SELECT statement? My INSERT looks like this: string conn = System.Configuration.ConfigurationManager.ConnectionStrings["artisanConnectionString"].ConnectionString; SqlConnection uniqueCon = new SqlConnection(conn); //set SQL statement SqlCommand cmd = new SqlCommand("INSERT INTO [tbl_Additional] (add_Item, add_Price) VALUES ('" + TextBox10.Text + "', '" + TextBox11.Text + "') ", uniqueCon); cmd.Connection.Open(); try {//insert/ add new record //commit to database cmd.ExecuteNonQuery(); //clear form add/edit TextBox10.Text = null; TextBox11.Text = null; //confirm to user ShowMessageBox("New unique Item Added!"); //update dataset GridView2.DataBind(); } catch { ShowMessageBox("Unable to add record!!! Please try again later."); } cmd.Connection.Close(); Any help would be greatly appreciated. Thanks & Happy Easter!

      V Offline
      V Offline
      Vasudevan Deepak Kumar
      wrote on last edited by
      #2

      Before you attempt resolving this problem on hand, seriously consider a re-design of your database query approach. Your application is vulnerable for SQL Injection attacks.

      Vasudevan Deepak Kumar Personal Homepage
      Tech Gossips
      A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups