score:0

You can probably do this using a join but I've hobbled together a unit test which shows one way to this

public class TestProduct
{
    public int ManId { get; set; }
    public string ProductName { get; set; }
    public DateTime ProductionDate { get; set; }
    public string Shipper { get; set; }
    public string Distributor { get; set; }
}

[TestMethod]
public void TestSourceTable()
{
    // Set up a test list
    var list = new List<TestProduct>();
    for (int i=0;i<5;i++)
    {
        var p = new TestProduct
            {
                Distributor = "D" + i,
                ManId = i,
                ProductionDate = DateTime.Now,
                ProductName = "P" + i,
                Shipper = "S" + i
            };
        list.Add(p);
    }

    // Get an existing product
    var existingProduct = list[4];

    // Get an unknown product
    var unknownProduct = new TestProduct()
        {
            ManId = -1,
            Distributor = "",
            ProductionDate = DateTime.Now.AddDays(-1),
            ProductName = "",
            Shipper = ""
        };

     // product found
     Assert.True(list.Any(p => p == existingProduct));

     // product not found
     Assert.False(list.Any(p => p == unknownProduct));
}

score:1

I would join on ManId and then compare the rest of the values in a where clause:

bool productExists = (
    from p in mySdb.Products
    join s in mySdb.SourceProducts
      on p.ManId equals s.ManId
    where p.ProductName == s.ProductName
       && p.ProductionDate == s.ProductionDate
       && p.Shipper == s.Shipper
       && p.Distributer = s.Distributer
    select new { p.ManId, p.ProductName, p.ProductionDate, p.Shipper, p.Distributer }
    ).Any();

if (productExists)
{
    LblMessage.Text = "Acceptable product Data Inserted Sucessfully";
    InsertData();
}
else
{
    LblMessage.Text = "Invalid Product or bad Entry Please retype";
}

I've used Any() to produce an efficient EXISTS SQL query. You could use SingleOrDefault() or FirstOrDefault() instead if you actually need to use the product returned.

I also don't see anywhere that you're using your new Product's ID - you might need to add that filter to the query as well:

Product   mypro = new Product  { ... };

bool productExists = (
    from p in mySdb.Products
    where p.ManId equals mypro.ManId
    join s in mySdb.SourceProducts
      on p.ManId equals s.ManId
    ...

Related Articles