Join 244,303 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 782 people online right now. Registration is fast and FREE... Join Now!
Hi, my code is working fine for selecting records, inserting records and deleting records, but for some reason it is not taking the changes when I go to update the records... but yet it is making it past the try/catch block...
Here is my code...
CODE
// A bunch of input validation and calculations done previous to this point... if (error == 0) { try { string sSQL = "UpdateTicket"; SqlConnection cnUpdateRecord = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmdUpdateRecord = new SqlCommand(); cmdUpdateRecord.Parameters.Clear(); cmdUpdateRecord.CommandText = sSQL; cmdUpdateRecord.CommandType = CommandType.StoredProcedure; cmdUpdateRecord.Parameters.AddWithValue("@TicketID", int.Parse(Session["TicketID"].ToString())); cmdUpdateRecord.Parameters.AddWithValue("@TicketDate", dateTextBox.Text); cmdUpdateRecord.Parameters.AddWithValue("@TicketDescription", descriptionTextBox.Text); cmdUpdateRecord.Parameters.AddWithValue("@TherapistID", therapistID); cmdUpdateRecord.Parameters.AddWithValue("@CustomerID", int.Parse(Session["CID"].ToString())); cmdUpdateRecord.Parameters.AddWithValue("@TicketBillableTime", billableTimeDecimal); cmdUpdateRecord.Parameters.AddWithValue("@TicketBillableRate", billableRateDecimal); cmdUpdateRecord.Parameters.AddWithValue("@TicketTotal", ticketTotalDecimal); cmdUpdateRecord.Parameters.AddWithValue("@TicketPaid", ticketPaidString); cmdUpdateRecord.Parameters.AddWithValue("@TicketProfit", ticketProfitDecimal); cmdUpdateRecord.Connection = cnUpdateRecord; cnUpdateRecord.Open(); cmdUpdateRecord.ExecuteNonQuery(); cnUpdateRecord.Close(); } catch { errorText = "There was an error and the ticket did not get edited. <br />"; error++; } } if (error != 0) { errorLabel.Text = errorText; } else { Server.Transfer("Tickets.aspx"); }
Here is the Stored Procedure that it uses, "UpdateTicket"...
@eclipsed4utoo, yes, the StoredProcedure works correctly, if I feed direct values into the parameters instead of variables, the update takes... for instance...
Here is my procedure call... This won't make any changes to my record as is...
Have you put a break on that line of code and checked that the controls/variables/sessions all contain values to be passed into your EditRecord method?
Have you put a break on that line of code and checked that the controls/variables/sessions all contain values to be passed into your EditRecord method?
I know this is going to sound crazy, but I assumed that Visual Web Designer didn't have break points/stepping ... I had tried several times prior to click on the left side but to no avail... so I just figured there was something about it that wouldn't allow for it... I was going to ask later... But since you mentioned it, I tried again, this time to the left of the line count and it worked... Now I feel pretty stupid... LOL, please keep in mind I'm not doing this professionally but to learn the language...
But anyway, for some reason or another, when I input data into the textbox controls and step in to see the values, the new values aren't there, only the database values... I'll post the whole code so you can see how I'm populating the controls...
CODE
using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; using System.Data.SqlClient; using System.Configuration;
public partial class EditTicket : System.Web.UI.Page { int therapistID = 0; decimal therapistPercentageDecimal = 0m;
Each time a page posts back, it will fire the Page Load event prior to the event of the control that initiated the post back.
Your load event is currently overwriting any changes before the addTicketButton_Click event fires.
To prevent this, wrap the code in the Load event inside an If statement that checks if the Page.IsPostBack. You want the code to fire the first time the page loads, but you want to prevent it from overwriting any time the client initiates a post back.
CODE
protected void Page_Load(object sender, EventArgs e) { //Check the PostBack property of the page to see if the client //initiated a post back event if (!Page.IsPostBack) { string ticketPaidString = ""; string sSQL = "GetTicketByTicketID"; SqlConnection cnGetRecords = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmdGetRecords = new SqlCommand(); SqlDataAdapter daGetRecords = new SqlDataAdapter(); cmdGetRecords.Parameters.Clear(); cmdGetRecords.CommandText = sSQL; cmdGetRecords.CommandType = CommandType.StoredProcedure; cmdGetRecords.Parameters.AddWithValue("@TicketID", int.Parse(Session["TicketID"].ToString())); cmdGetRecords.Connection = cnGetRecords; daGetRecords.SelectCommand = cmdGetRecords; cnGetRecords.Open(); SqlDataReader rdr = null; rdr = cmdGetRecords.ExecuteReader(); while (rdr.Read()) { ticketDateTextBox.Text = ((DateTime)rdr["TicketDate"]).ToShortDateString(); ticketDescriptionTextBox.Text = (string)rdr["TicketDescription"]; therapistID = (int)rdr["TherapistID"]; billableTimeTextBox.Text = ((decimal)rdr["TicketBillableTime"]).ToString(); billableRateTextBox.Text = ((decimal)rdr["TicketBillableRate"]).ToString(); ticketPaidString = (string)rdr["TicketPaid"]; } cnGetRecords.Close();