.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Checking Existing Records Data Rows

Posted By:Majith       Posted Date: September 08, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

This code explains the method to check existing records without using any loops and to improve the code performance.
 

Checking Existing Records Using Array of Data Rows:

Introduction:

This code explains the method to check the existing database records without using for loops and improving the performance to the applications.

Whenever we need to identify the duplicate records, normally we count the total number of records from dataset or any database objects and using for loop we will check routine to the records.

For Example : 

if your table having thousand records then you are doing search operation in the data table using for loops. 

This code can be used in all pages whenever we checking the existing records.


Problem Identification:

1.      Unnecessary to wallop the database

 2.      using for loops for each and every record checking


DataSet dsNew = GetDataSet();
for (int lvar = 0; lvar < dsNew.Tables[0].Rows.Count; lvar ++) {
   if (txt_Name.Text.ToString().Trim() ==  dsNew.Tables[0].Rows[lvar]["Name"].ToString().Trim())
   {                      
     Label_Error.Text = "Record Already Exists";
     return;
   }
}



Problem Solution:

  1. Avoiding the for loop 
  2. Restricting round trip process of Database

 

DataSet dsCheck = GetDataSet();
DataRow[] drName = dsCheck.Tables[0].Select("Name='" + txt_Name.Text.ToString().Trim() + "'");
if (drName.Length > 0)
{
Label_Error.Text = " Record Already Exists"; 
return;
}
Happy Coding!
 
 Subscribe to Articles

     

Further Readings:

Responses
Author: newery         Company URL: http://www.dotnetspark.com
Posted Date: September 11, 2010

use linq

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend