Your test is redundant, since you are already querying only for matching records. In this case, you will get no rows if there is no match, therefore you will never hit the else clause in the loop.
Instead, it is enough to check if there are any matching records at all. The example below shows how (note that you can put a condition straight into Any() instead of specifying Where()).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomerMaster
{
public partial class CustomerMaster : System.Web.UI.Page
{
private void AddData()
{
OperationsDataContext AddDb = new OperationsDataContext();
CustomerMast cm = new CustomerMast();
cm.CustomerCode = txtCustCde.Text;
cm.CustomerName = txtCustName.Text;
cm.CustomerAddress = txtCustAdd.Text;
cm.BrkrCode = txtBrkCode.Text;
cm.BrokerName = txtBrkName.Text.
cm.CrrCode = txtCrrCode.CarrierCode;
cm.CarrierName = txtCrrName.Text;
cm.StnCode=sm.StationCode;
cm.StationName = txtStnName.Text;
cm.VatNo=txtVatNo.Text;
cm.TinNo=txtTinNo.Text;
cm.TMCO=txtTMCONo.Text;
cm.PanNo = txtPAN.Text;
if(AddDb.CustomerMasts.Any(c=>c.CustomerCode == txtCustCde.Text))
{
Response.Write("Error! CustomerCode Exits");
}
else
{
AddDb.BrokerMasts.InsertOnSubmit(bm);
AddDb.CarrierMasts.InsertOnSubmit(crm);
AddDb.StationMasts.InsertOnSubmit(sm);
AddDb.CustomerMasts.InsertOnSubmit(cm);
AddDb.SubmitChanges();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddData();
}
}
}