Inserting a record into a database with a front web form
-
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
-
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
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