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
bhumber
Posts
-
Inserting a record into a database with a front web form -
Urgent Help on C#~~Good morning Write a method like the below to populate the form // could be global private string strErrorMsg = "Database error: " + exp.Message; try { SqlConnection conn = new SqlConnection(put your connection string hear); strSQL = "SELECT * FROM theNameOftheTable"; SqlCommand cmd = new SqlCommand(strSQL, conn); cmd.CommandType = CommandType.Text; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); // a method to display the information, is this a grid or form with textboxes? conn.Close(); cmd.Dispose(); conn.Dispose(); } catch { //some message about the error strErrorMsg; }
-
add/save a row to a db C#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
-
add/save a row to a db C#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; } }