TextBox Validation for Duplicates in DataBase:
This validation will be help full to you when you are checking the duplicate values from database.
Case: If there is a field, where duplicate values are not allowed into database. In this case you can know the value when you click the Save button to the database. In that case if you write this validation to the textbox_Validating event , it will set the error provider when you leave the perticuler field on the user interface.So by using of this validation, you will know imidiately about the duplicate values from database.
For this validation you can write the function in a method. And that method will be validated where you want to rais the duplicate checker.This code is verry use full and efficient way to check the duplicate values from database.
private bool RecordExists(string name)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\WORK AREA\Thalassemia\Data\thalsemia.accdb");
cn.Open();
OleDbCommand cmd = new OleDbCommand("select count(*) from Demographics where thal_Ptntname = @name", cn);
cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = xtPatientName.Text;
int recordCount = Convert.ToInt32(cmd.ExecuteScalar());
cn.Close()
return recordCount > 0;
}
Event: Call this method in TextBox_Validating event.
This validation will be help full to you when you are checking the duplicate values from database.
Case: If there is a field, where duplicate values are not allowed into database. In this case you can know the value when you click the Save button to the database. In that case if you write this validation to the textbox_Validating event , it will set the error provider when you leave the perticuler field on the user interface.So by using of this validation, you will know imidiately about the duplicate values from database.
For this validation you can write the function in a method. And that method will be validated where you want to rais the duplicate checker.This code is verry use full and efficient way to check the duplicate values from database.
private bool RecordExists(string name)
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\WORK AREA\Thalassemia\Data\thalsemia.accdb");
cn.Open();
OleDbCommand cmd = new OleDbCommand("select count(*) from Demographics where thal_Ptntname = @name", cn);
cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = xtPatientName.Text;
int recordCount = Convert.ToInt32(cmd.ExecuteScalar());
cn.Close()
return recordCount > 0;
}
Event: Call this method in TextBox_Validating event.
if (RecordExists(txtPatientName.Text))
{
errorProvide1.SetError(txtPatientName, "Duplicate value cannot be taken.");
return;
}
{
errorProvide1.SetError(txtPatientName, "Duplicate value cannot be taken.");
return;
}
else
{
errorProvide1.SetError(txtPatientName, "");
}
}
I hope this code will help you.
Along with this, it will be better if before comparison spaces & special characters are moved, and then the string is converted to lower case. Then if you compare, the matching will be more proficient.
ReplyDeleteFor ex:
varListingName = Functions.CleanListingName(varListingName);
if (dsListings.Tables[0].Rows[i]["Listing_Name"].ToString().ToLower() == varListingName.ToLower())
Do let me know, incase dis-agreeing.
Hii, Is there any way to do this using DataSet and DataTable
ReplyDelete