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. Database & SysAdmin
  3. Database
  4. Inserting a record into a database with a front web form

Inserting a record into a database with a front web form

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

    The code below should insert this record into the database, I don't the error. I drops through and run the line that enables the btnAdd.Enable = true. How do I fix it? 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; } } bhumber

    D 1 Reply Last reply
    0
    • B bhumber

      The code below should insert this record into the database, I don't the error. I drops through and run the line that enables the btnAdd.Enable = true. How do I fix it? 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; } } bhumber

      D Offline
      D Offline
      dwatkins dirq net
      wrote on last edited by
      #2

      the btnAdd.Enabled = true; line is in the finally block so it will always run. You might want to put that in the try block. Also if you do this: int intAffectedRecords = cmSQL.ExecuteNonQuery(); you can get the count of affected records so you could then say if there were records affected the button would be enabled: btnAdd.Enabled = (intAffectedRecords > 1) ? true : false; You can also just use the Exception exp's ToString method to see the entire error message: Response.Write(exp.ToString()); Dirk Watkins

      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