Quite often when querying databases using data from text boxes, you can run into problems dealing with a blank string and fields containing a null.
A field in a table may contain a null which could also mean a blank field but you can run into problems when you are trying to query a condition on the field.
This example solved the problem;
|
1 2 3 |
var DupQuery = (from recs in db.tAXModelKitXref where (txtProdCode.Text == "" ? recs.ProdCode.Equals(null) : recs.ProdCode.Equals(txtProdCode.Text)) && (txtKitCode.Text == "" ? recs.KitCode.Equals(null) : recs.KitCode.Equals(txtKitCode.Text)) && recs.UniqueId != Unique select new { id = recs.UniqueId }).ToList(); |
All i’m doing in the LINQ query is checking if the textbox (a string) is blank, if it is then I pass a null to condition.
If you are planning to use a null when the field is blank, then also when you are saving data to the field you need to use the followin example;
|
1 |
Record.ProdCode = (txtProdCode.Text == "" ? null : txtProdCode.Text); |