I have a button that will validate the textbox for the service provider and the dropdown list for the locations. I have 3 tables: Locations, Service_Providers, and Service_Providers_Locations with a many-to-many relationship. But the tables that are affected are mainly Service_Providers and Service_Providers_Locations. However, upon running, the button inserts the record even if there are duplicates. What we want is to not allow duplicate record in the transaction table Service_Providers_Locations. There can be the same name of service provider, but not same location. Our code allows duplicate record insertion. We are using a three-tier approach.
protected void btnAddServiceProvider_Click(object sender, EventArgs e)
{
bll.Name = txtServiceProvider.Text;
DataTable temp = bll.ServiceProviderCheckExisting();
if (temp.Rows.Count == 0)
{
bll.IsDeleted = "n";
bll.ServiceProviderAdd();
temp = bll.ServiceProviderCheckExisting();
bll.ServiceProviderID = temp.Rows[0][0].ToString();
bll.LocationID = ddlLocations.SelectedValue;
bll.IsApproved = "y";
bll.ServiceProviderLocationAdd();
gvServiceProviders.DataSource = bll.GetServiceProviderLocations();
gvServiceProviders.DataBind();
}
else
{
bll.ServiceProviderID = temp.Rows[0][0].ToString();
temp = bll.GetServiceProviderLocationUsingServiceProviderID();
if (temp.Rows.Count == 0)
{
bll.LocationID = ddlLocations.SelectedValue;
bll.IsApproved = "y";
bll.ServiceProviderLocationAdd();
gvServiceProviders.DataSource = bll.GetServiceProviderLocations();
gvServiceProviders.DataBind();
}
else
{
for (int x = 0; x < temp.Rows.Count; x++)
{
if (ddlLocations.SelectedValue == temp.Rows[x][1].ToString())
{
lblServiceProviderError.Text = "This service provider already exists for that location.";
}
else
{
bll.LocationID = ddlLocations.SelectedValue;
bll.IsApproved = "y";
bll.ServiceProviderLocationAdd();
gvServiceProviders.DataSource = bll.GetServiceProviderLocations();
gvServiceProviders.DataBind();
}
}
}
}
}