Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to compare gridview textbox oldvalue to newvalue before updating values in c# asp.net?
I am using this code for updating value of gridview.
C#
protected void GVStartCampaignDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
TextBox txtAmountValue = (TextBox)GVStartCampaignDetails.Rows[e.RowIndex].FindControl("txtGVSCAmountValue");

SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                SqlCommand cmdUpdateAllowtedCardNo = new SqlCommand("procUpdateAmount", con);
                cmdUpdateAllowtedCardNo.CommandType = CommandType.StoredProcedure;
                cmdUpdateAllowtedCardNo.Parameters.Add("@EmailId", SqlDbType.NVarChar).Value = EmailId;
                cmdUpdateAllowtedCardNo.Parameters.Add("@Amount", SqlDbType.Int).Value = Convert.ToInt32(txtAmountValue.Text);
cmdUpdateAllowtedCardNo.ExecuteNonQuery();
                con.Close();</pre>
GVStartCampaignDetails.EditIndex = -1;
}

how to compare txtAmountValue OldValue to NewValue before update procedure?

Please help me.

Thanks in Advance.

Ankit Agarwal
Website Developer
Posted
Updated 10-Jan-14 0:37am
v2
Comments
Karthik_Mahalingam 10-Jan-14 6:04am    
by comparing what will you do ?
[no name] 10-Jan-14 6:07am    
I want to compare from Gridview Amount Cell Value, which already filled in the cell.
When i write new value in Amount cell, so my amount should not be greater then already filled amount value.

You have to use GridViewUpdatedEventArgs.OldValues Property[^].
Quote:
Gets a dictionary that contains the original field name/value pairs for the updated record.
 
Share this answer
 
Do like this,
Add a template field with a hidden field and bind the amount value to it.
whenever you are updating the particular row, you can read the old value from that row's hidden field. and new value from the text box ,
then you can compare the both.
if both are different you can update the data else ignore it...

sample code:

C#
TextBox txtAmountValue = (TextBox)GVStartCampaignDetails.Rows[e.RowIndex].FindControl("txtGVSCAmountValue");
                HiddenField hdfldGVSCAmountValue = (HiddenField)GVStartCampaignDetails.Rows[e.RowIndex].FindControl("hdfldGVSCAmountValue");
                double oldvalue, newvalue;
                double.TryParse(txtAmountValue.Text, out newvalue);
                double.TryParse(hdfldGVSCAmountValue.Value, out oldvalue);

                if (newvalue >= oldvalue) // your custom conditions here
                {
                    // do something
                }    
 
Share this answer
 
v3
Comments
[no name] 10-Jan-14 7:27am    
Thanks buddy
Karthik_Mahalingam 10-Jan-14 7:28am    
welcome ankit :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900