Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a ASP.NET Expert!

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!




SQL edit record in ASP .NET (C#)

 
Reply to this topicStart new topic

SQL edit record in ASP .NET (C#)

webwired
5 Dec, 2008 - 03:45 PM
Post #1

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 3 times
Dream Kudos: 100
My Contributions
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"...
CODE

ALTER PROCEDURE dbo.UpdateTicket
    (
    @TicketDate datetime,
    @TicketDescription varchar (MAX),
    @TherapistID int,
    @CustomerID int,
    @TicketBillableTime decimal,
    @TicketBillableRate decimal,
    @TicketTotal decimal,
    @TicketPaid varchar (50),
    @TicketProfit decimal,
    @TicketID int
    )
AS
    SET NOCOUNT ON;
UPDATE Tickets SET
    TicketDate = @TicketDate,
    TicketDescription = @TicketDescription,
    TherapistID = @TherapistID,
    CustomerID = @CustomerID,
    TicketBillableTime = @TicketBillableTime,
    TicketBillableRate = @TicketBillableRate,
    TicketTotal = @TicketTotal,
    TicketPaid = @TicketPaid,
    TicketProfit = @TicketProfit
WHERE (TicketID = @TicketID);


User is offlineProfile CardPM
+Quote Post


eclipsed4utoo
RE: SQL Edit Record In ASP .NET (C#)
6 Dec, 2008 - 07:00 AM
Post #2

D.I.C Lover
Group Icon

Joined: 21 Mar, 2008
Posts: 1,170



Thanked: 117 times
Dream Kudos: 125
My Contributions
have you determined that the stored procedure works correctly?

Try running this sql statement in a query window...

SQL
EXEC [databaseName].dbo.UpdateTicket
@TicketDate = GetDATE();
, @TicketDescription = 'Some Description'
--Continue to add the other parameters


also, remove this line from the stored procedure

SQL
SET NOCOUNT ON

User is offlineProfile CardPM
+Quote Post

webwired
RE: SQL Edit Record In ASP .NET (C#)
6 Dec, 2008 - 07:34 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 3 times
Dream Kudos: 100
My Contributions
@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...
CODE

            EditRecord(int.Parse(Session["TicketID"].ToString()), ticketDateTextBox.Text, ticketDescriptionTextBox.Text,
                billableTimeDecimal, billableRateDecimal, ticketTotalDecimal, ticketPaidString, ticketProfitDecimal);


But if I put in direct values, they'll take...
CODE

            EditRecord(int.Parse(Session["TicketID"].ToString()), ticketDateTextBox.Text, "I'm Working, WooHoo",
                .5m, 75m, ticketTotalDecimal, ticketPaidString, ticketProfitDecimal);


BTW, I did have to make a correction to the StoredProcedure,... put (18, 2) after Decimal ... it was rounding...
User is offlineProfile CardPM
+Quote Post

Jayman
RE: SQL Edit Record In ASP .NET (C#)
7 Dec, 2008 - 12:49 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

webwired
RE: SQL Edit Record In ASP .NET (C#)
7 Dec, 2008 - 03:22 PM
Post #5

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 3 times
Dream Kudos: 100
My Contributions
QUOTE(Jayman @ 7 Dec, 2008 - 12:49 PM) *

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;

    protected void Page_Load(object sender, EventArgs e)
    {
        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();

        sSQL = "GetTherapistByTherapistID";
        cmdGetRecords.Parameters.Clear();
        cmdGetRecords.CommandText = sSQL;
        cmdGetRecords.CommandType = CommandType.StoredProcedure;
        cmdGetRecords.Parameters.AddWithValue("@TherapistID", therapistID);
        cmdGetRecords.Connection = cnGetRecords;
        daGetRecords.SelectCommand = cmdGetRecords;
        cnGetRecords.Open();
        rdr = null;
        rdr = cmdGetRecords.ExecuteReader();
        while (rdr.Read())
        {
            therapistPercentageDecimal = (decimal)rdr["TherapistPercentage"];
        }
        cnGetRecords.Close();

        if (ticketPaidString == "Paid")
        {
            ticketPaidYesRadioButton.Checked = true;
        }
        if (ticketPaidString == "Unpaid")
        {
            ticketPaidNoRadioButton.Checked = true;
        }
    }

    protected void EditRecord(int TicketID, string TicketDate, string TicketDescription, decimal TicketBillableTime,
        decimal TicketBillableRate, decimal TicketTotal, string TicketPaid, decimal TicketProfit)
    {
        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", TicketID);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketDate", TicketDate);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketDescription", TicketDescription);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketBillableTime", TicketBillableTime);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketBillableRate", TicketBillableRate);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketTotal", TicketTotal);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketPaid", TicketPaid);
        cmdUpdateRecord.Parameters.AddWithValue("@TicketProfit", TicketProfit);
        cmdUpdateRecord.Connection = cnUpdateRecord;
        cnUpdateRecord.Open();
        cmdUpdateRecord.ExecuteNonQuery();
        cnUpdateRecord.Close();
    }

    protected void addTicketButton_Click(object sender, EventArgs e)
    {
        int error = 0;
        string errorText = "";
        decimal billableTimeDecimal = 0m;
        decimal billableRateDecimal = 0m;
        decimal ticketTotalDecimal = 0m;
        decimal ticketProfitDecimal = 0m;
        string ticketPaidString = "";

        try
        {
            billableTimeDecimal = decimal.Parse(billableTimeTextBox.Text);
        }
        catch (FormatException)
        {
            errorText += "Billable Time must be a numeric value <br />";
            error++;
        }
        try
        {
            billableRateDecimal = decimal.Parse(billableRateTextBox.Text);
        }
        catch (FormatException)
        {
            errorText += "Billable Rate must be a numeric value <br />";
            error++;
        }
        if (error == 0 && billableTimeDecimal > 0 && billableRateDecimal > 0)
        {
            ticketTotalDecimal = billableTimeDecimal * billableRateDecimal;
            if (therapistPercentageDecimal < 100)
            {
                ticketProfitDecimal = ticketTotalDecimal - (ticketTotalDecimal * (therapistPercentageDecimal / 100));
            }
            else
            {
                ticketProfitDecimal = ticketTotalDecimal;
            }
        }
        else
        {
            errorText += "Billable Rate and Billable Time must be greater than zero <br />";
            error++;
        }
        if (ticketPaidYesRadioButton.Checked)
        {
            ticketPaidString = "Paid";
        }
        else if (ticketPaidNoRadioButton.Checked)
        {
            ticketPaidString = "Unpaid";
        }
        if (error == 0)
        {
            EditRecord(int.Parse(Session["TicketID"].ToString()), ticketDateTextBox.Text, ticketDescriptionTextBox.Text,
                billableTimeDecimal, billableRateDecimal, ticketTotalDecimal, ticketPaidString, ticketProfitDecimal);

            Server.Transfer("Tickets.aspx");
        }
        else if (error != 0)
        {
            errorLabel.Text = errorText;
        }
    }
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: SQL Edit Record In ASP .NET (C#)
7 Dec, 2008 - 05:04 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,063



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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();

            sSQL = "GetTherapistByTherapistID";
            cmdGetRecords.Parameters.Clear();
            cmdGetRecords.CommandText = sSQL;
            cmdGetRecords.CommandType = CommandType.StoredProcedure;
            cmdGetRecords.Parameters.AddWithValue("@TherapistID", therapistID);
            cmdGetRecords.Connection = cnGetRecords;
            daGetRecords.SelectCommand = cmdGetRecords;
            cnGetRecords.Open();
            rdr = null;
            rdr = cmdGetRecords.ExecuteReader();
            while (rdr.Read())
            {
                therapistPercentageDecimal = (decimal)rdr["TherapistPercentage"];
            }
            cnGetRecords.Close();

            if (ticketPaidString == "Paid")
            {
                ticketPaidYesRadioButton.Checked = true;
            }
            if (ticketPaidString == "Unpaid")
            {
                ticketPaidNoRadioButton.Checked = true;
            }
        }
    }

User is offlineProfile CardPM
+Quote Post

webwired
RE: SQL Edit Record In ASP .NET (C#)
7 Dec, 2008 - 08:19 PM
Post #7

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 3 times
Dream Kudos: 100
My Contributions
@Jayman ... Thank you very much, I get it now... Appreciate the help... It worked like a charm.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:56PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month