patterncsharpMinor
Could I have any performance issues if I do ID check while using TextChanged in textbox every time user type a character?
Viewed 0 times
textchangedwhileissuesuseranyeverycouldtimecharactertype
Problem
I had an idea of checking if Entry ID already exists in database. If it doesn't there is no point of removing non existing entry.
So when user types then each time new character is added I am opening or closing connection to my database file. This is my concern.
But what if there might be hundredths of entries? Could that cause performance problems? If so, how can I avoid this?
Here is my code:
```
namespace Password_Manager
{
public partial class DeleteEntryWindow : Form
{
string user, pass, filePath;
// Initializing MainWindow form.
MainWindow mainWindow;
public DeleteEntryWindow()
{
InitializeComponent();
// Creating an EventHandler for textbox when text is chaned.
txtEntryID.TextChanged += new EventHandler(ValidateInput);
}
public DeleteEntryWindow(MainWindow viaParameter,
string user, string pass, string filePath)
: this()
{
mainWindow = viaParameter;
this.user = user;
this.pass = pass;
this.filePath = filePath;
}
private void ValidateInput(object sender, EventArgs e)
{
int intNumber = 0;
// Checking if input is empty, not a number, or is less than 1.
if (!string.IsNullOrEmpty(txtEntryID.Text) &&
int.TryParse(txtEntryID.Text, out intNumber) &&
intNumber > 0)
{
lblMessage.Text = "Entry ID is valid.";
lblMessage.ForeColor = Color.Green;
btnDeleteEntry.Enabled = true;
}
else
{
lblMessage.Text = "You must enter Entry ID number!";
lblMessage.ForeColor = Color.IndianRed;
btnDeleteEntry.Enabled = false;
}
if (!DoesIDExist(intNumber, filePath))
{
lblMessage.Text = "Entry ID doesn't exist!";
So when user types then each time new character is added I am opening or closing connection to my database file. This is my concern.
But what if there might be hundredths of entries? Could that cause performance problems? If so, how can I avoid this?
Here is my code:
```
namespace Password_Manager
{
public partial class DeleteEntryWindow : Form
{
string user, pass, filePath;
// Initializing MainWindow form.
MainWindow mainWindow;
public DeleteEntryWindow()
{
InitializeComponent();
// Creating an EventHandler for textbox when text is chaned.
txtEntryID.TextChanged += new EventHandler(ValidateInput);
}
public DeleteEntryWindow(MainWindow viaParameter,
string user, string pass, string filePath)
: this()
{
mainWindow = viaParameter;
this.user = user;
this.pass = pass;
this.filePath = filePath;
}
private void ValidateInput(object sender, EventArgs e)
{
int intNumber = 0;
// Checking if input is empty, not a number, or is less than 1.
if (!string.IsNullOrEmpty(txtEntryID.Text) &&
int.TryParse(txtEntryID.Text, out intNumber) &&
intNumber > 0)
{
lblMessage.Text = "Entry ID is valid.";
lblMessage.ForeColor = Color.Green;
btnDeleteEntry.Enabled = true;
}
else
{
lblMessage.Text = "You must enter Entry ID number!";
lblMessage.ForeColor = Color.IndianRed;
btnDeleteEntry.Enabled = false;
}
if (!DoesIDExist(intNumber, filePath))
{
lblMessage.Text = "Entry ID doesn't exist!";
Solution
Setting up a connection to a database on every handle of the event is going to be really slow. You would be best running an initial call to the database that retrieves the Id's stored within it and caches the results inside your code. Then, when you perform your validation, just do a look up inside this cache. That will be a much faster check. Since you are using integer Id values, you could optimize this further by storing the Id results in an array that you can sort. You can then use
Note: Using this approach, your validation check will not necessarily be working on the latest set of Id values (if they can be updated separately). In that case, either you will need to run a refresh on the database regularly, or change your validation to be a slower check that is performed after the user has finished changing the text (not on each change).
Array.BinarySearch(...) to run a faster lookup.Note: Using this approach, your validation check will not necessarily be working on the latest set of Id values (if they can be updated separately). In that case, either you will need to run a refresh on the database regularly, or change your validation to be a slower check that is performed after the user has finished changing the text (not on each change).
Context
StackExchange Code Review Q#6568, answer score: 3
Revisions (0)
No revisions yet.