I need to check if an input is valid prior to updating my db.
I have created a bool method to check if it is valid.
private bool validProject(string ProjectID)
{
using (AXEntitiesDEV db = new AXEntitiesDEV())
{
return db.PROJTABLEs.Any(c => c.PROJID==ProjectID);
}
}
and am checking it in my update method.
protected void Insert(object sender, EventArgs e)
{
using (GPSE2Entities entities = new GPSE2Entities())
{
TextBox txtEditTime = (TextBox)gvDailyGPS.FooterRow.FindControl("txtFooterTime");
DropDownList ddlEventDateOnly = (DropDownList)gvDailyGPS.FooterRow.FindControl("ddlFooterDateOnly");
DateTime EDT = DateTime.Now;
TextBox txtAddProjectID = (TextBox)gvDailyGPS.FooterRow.FindControl("txtAddProjectID");
validProject(txtAddProjectID.Text);
DailyGPSTable newLOB = new DailyGPSTable
{
EventDateTime = EDT,
Project = txtAddProjectID.Text,
};
entities.DailyGPSTables.Add(newLOB);
{
entities.SaveChanges();
BindGrid();
}
entities.SaveChanges();
}
}
I tried catching and reporting the error but it allows invalid input through when false.
try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";
addErr.Text = "Invalid Project ID.";
}
What's the best way I can catch and report the error prior to trying the insert?