I have a grid which allows a user to enter a new Grid item. If the grid item exists or the tb
is empty it displays the error. Here's the code I came up with.
Can anybody think of a cleaner or more efficient way of doing so? Thinking about it now, I guess I could have done it all in one function, and placed InsertTrack()
in the else condition of DoesTrackExist
and register the script in the if
statement.
private void InsertTrack()
{
if (DoesTrackExist(txtEventTracks.Text))
{
using (EMSEntities db = new EMSEntities())
{
EventTrack newEventTrack = new EventTrack()
{
TrackName = txtEventTracks.Text
};
db.EventTracks.Add(newEventTrack);
db.SaveChanges();
}
}
else
{
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Error", "alert('Track name cannot be duplicate or empty')", true);
}
}
private bool DoesTrackExist(string txtValue)
{
using (EMSEntities db = new EMSEntities())
{
var context = from x in db.EventTracks
where x.TrackName.Contains(txtEventTracks.Text)
select x;
if (context.Any() || txtEventTracks.Text == "")
{
return false;
}
else
{
return true;
}
}
}