Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
i am using the following method to compare my forms textbox values with database values id they are same or not before updating.If they are not same i have to perform certain operations.But i have many many fields in table and i dont want to write the following code for every field.its getting very lengthy.I want to compare them using a very short code using looping..using a function etc..
Please help me..


C#
if (!String.Equals(txtMobile.Text.Trim(), objdatarow["MobileNo"].ToString().Trim(), StringComparison.Ordinal))
               {
                   mailstr.Add("MobileNo:-" + txtAddress.Text.Trim());
                   NewString.Add(txtMobile.Text.Trim());
                   old.Add(objdatarow["MobileNo"].ToString().Trim());
               }
Posted
Comments
OriginalGriff 15-Mar-14 7:02am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
I can't see anything there that could be looped - so probably you need to provide a better example?
Use the "Improve question" widget to edit your question and provide better information.
OriginalGriff 15-Mar-14 7:10am    
Down vote compensated...

If you named your textboxes the same as the names of your database fields you can do a foreach on Form.Controls and check the ID or even use FindControl.

However, I would not recommend this method. It may be a pain to do them individually but I still think you are better off doing them individually. It will be easier to find bugs if a particular field does not work and if you later have to add some specific logic for a field it would be more readable.
 
Share this answer
 
Do not comapre values on client side. Do it on server side, using stored procedure[^] (SP).

SQL
CREATE PROCEDURE UpdateWhenDiffer
    @MobileNo VARCHAR(30),
    @MyKey INT
AS
BEGIN
    UPDATE MyTable SET MobileNo = @MobileNo
    WHERE ID = @MyKey AND MobileNo<>@MobileNo

END


For further information, please see:
Modifying Data with Stored Procedures[^]
How to: Execute a Stored Procedure that Returns a Single Value[^]

The reason i suggest you to use SP is SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]
 
Share this answer
 

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