Checking Existing Records Using Array of Data Rows

Majith
Posted by in ASP.NET category on for Intermediate level | Views : 11634 red flag

This code explains the method to check the existing database records without using for loops and improving the performance to the applications.
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.

In case if your table having one thousand records means then  the calculative time will go higher and it slow down the process.

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;
}
Conclusion:

Here we don’t used for loop and no hitting the databases, just checking the datarow length with minimal code.



Cheers,,
Page copy protected against web site content infringement by Copyscape

About the Author

Majith
Full Name: Majith Basha
Member Level:
Member Status: Member
Member Since: 7/18/2008 11:49:59 PM
Country:



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)