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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. add/save a row to a db C#

add/save a row to a db C#

Scheduled Pinned Locked Moved Web Development
databasequestioncsharphelpannouncement
4 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.
  • B Offline
    B Offline
    bhumber
    wrote on last edited by
    #1

    How do you add/save a row using the INSERT sql command in a web form with textbox not a datagrid? Below is my code, what wroung and why does fall through to the "finally block?" private void SaveRecord() { string strSQL; if(btnSave.CommandArgument == "Add") { strSQL = "INSERT INTO CoinCollection " + " (ID, TypeOfCoin, YearOfCoin, StateOfCoin, DateReceive)" + "VALUES " + " (@ID, @TypeOfCoin, @YearOfCoin, @StateOfCoin, @DateReceive)"; } else { // The user is updating an existing item strSQL = "UPDATE CoinCollection " + " SET TypeOfCoin = @TypeOfCoin, " + " YearOfCoin = @YearOfCoin, " + " StateOfCoin = @StateOfCoin, " + " DateReceive = @DateReceive " + "WHERE ID = @ID"; } SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connString"]); SqlCommand cmSQL = new SqlCommand(strSQL, conn); // Add all the requuired SQL Parmeters. cmSQL.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)).Value = Convert.ToInt32(tbID.Text); cmSQL.Parameters.Add(new SqlParameter("@TypeOfCoin", SqlDbType.VarChar, 50)).Value = tbToc.Text; cmSQL.Parameters.Add(new SqlParameter("@YearOfCoin", SqlDbType.VarChar, 50)).Value = tbYoc.Text; cmSQL.Parameters.Add(new SqlParameter("@StateOfCoin", SqlDbType.VarChar, 50)).Value = tbSoc.Text; cmSQL.Parameters.Add(new SqlParameter("@DateReceive", SqlDbType.VarChar, 50)).Value = tbDr.Text; try { conn.Open(); cmSQL.ExecuteNonQuery(); strMsg = "Coin successfully saved to the database."; } catch(Exception exp) { strErrorMsg = "Database error! Coins not saved to database. Error Message: " + exp.Message; } finally { conn.Close(); btnAdd.Enabled = true; } }

    E 1 Reply Last reply
    0
    • B bhumber

      How do you add/save a row using the INSERT sql command in a web form with textbox not a datagrid? Below is my code, what wroung and why does fall through to the "finally block?" private void SaveRecord() { string strSQL; if(btnSave.CommandArgument == "Add") { strSQL = "INSERT INTO CoinCollection " + " (ID, TypeOfCoin, YearOfCoin, StateOfCoin, DateReceive)" + "VALUES " + " (@ID, @TypeOfCoin, @YearOfCoin, @StateOfCoin, @DateReceive)"; } else { // The user is updating an existing item strSQL = "UPDATE CoinCollection " + " SET TypeOfCoin = @TypeOfCoin, " + " YearOfCoin = @YearOfCoin, " + " StateOfCoin = @StateOfCoin, " + " DateReceive = @DateReceive " + "WHERE ID = @ID"; } SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["connString"]); SqlCommand cmSQL = new SqlCommand(strSQL, conn); // Add all the requuired SQL Parmeters. cmSQL.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)).Value = Convert.ToInt32(tbID.Text); cmSQL.Parameters.Add(new SqlParameter("@TypeOfCoin", SqlDbType.VarChar, 50)).Value = tbToc.Text; cmSQL.Parameters.Add(new SqlParameter("@YearOfCoin", SqlDbType.VarChar, 50)).Value = tbYoc.Text; cmSQL.Parameters.Add(new SqlParameter("@StateOfCoin", SqlDbType.VarChar, 50)).Value = tbSoc.Text; cmSQL.Parameters.Add(new SqlParameter("@DateReceive", SqlDbType.VarChar, 50)).Value = tbDr.Text; try { conn.Open(); cmSQL.ExecuteNonQuery(); strMsg = "Coin successfully saved to the database."; } catch(Exception exp) { strErrorMsg = "Database error! Coins not saved to database. Error Message: " + exp.Message; } finally { conn.Close(); btnAdd.Enabled = true; } }

      E Offline
      E Offline
      enjoycrack
      wrote on last edited by
      #2

      Hi there, Can u post the error exception message? << >>

      B 1 Reply Last reply
      0
      • E enjoycrack

        Hi there, Can u post the error exception message? << >>

        B Offline
        B Offline
        bhumber
        wrote on last edited by
        #3

        Good morning and thanks for looking at this for me. The answer to your question is "no" the program drops through the exceptions to the finally block and executes the code to enable the Add button. Brent

        E 1 Reply Last reply
        0
        • B bhumber

          Good morning and thanks for looking at this for me. The answer to your question is "no" the program drops through the exceptions to the finally block and executes the code to enable the Add button. Brent

          E Offline
          E Offline
          enjoycrack
          wrote on last edited by
          #4

          I did'nt try.... :confused:

          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