Click here to Skip to main content
15,033,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to insert values in a sql database using textbox. The ID field is the primary key. Now If the user inputs duplicate ID value there will be a messagebox displaying error alert and if the value is not duplicate then it will insert data to database. The problem is when the Id is already in database it is showing the error but when the new ID is entered the data is not stored to database. Kindly help!
The Code is as follows.....
C#
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;

var chk = from c in AddDb.CustomerMasts where c.CustomerCode == txtCustCde.Text select c;
            foreach (CustomerMast c in chk)
            {
                if (c.CustomerCode == txtCustCde.Text)
                {
                    Response.Write("Error! CustomerCode Exits");

                }
                else if(c.CustomerCode != txtCustCde.Text)
                {
                    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();
            
    }
    }
}
Posted
Updated 4-Jun-15 20:10pm
v2
Comments
Sinisa Hajnal 5-Jun-15 2:14am
   
Not enough data. Post InsertOnSubmit code. Before that check the following:
- values of bm, crm and sm (they are used, but not shown in the example above)
- values of cm
- connection string / database context - is everything OK
- put TRY..CATCH block around submits, see if you get the error
- on the page add a label into which you will write the message, don't write directly on the page. Alternatively, create javascript function that calls alert dialog with the message and call that.

1 solution

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()).

C#
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();
        }
    }
}
   
Comments
Member 11341510 5-Jun-15 13:44pm
   
thank you! the soln worked. But it is still not clear to me why it is skipping the condition when textbox.text is not the same as the chk variable. I am a new to c# coding. If could please explain it.
Staffan Bruun 8-Jun-15 2:23am
   
The following line in your original code, because of the 'where' clause, already selects only those lines with a matching customer code:

var chk = from c in AddDb.CustomerMasts where c.CustomerCode ==
txtCustCde.Text select c;

If there is no matching customer code, 'chk' will not contain any rows. In this case, the 'foreach' block is not executed. Remember that the code inside 'foreach' is run once for every row in the collection you specify; if there are no rows, it is not run at all.

Because the 'else' block of the 'if' statement is inside the 'foreach' block, it is also never executed.
Member 11341510 8-Jun-15 9:45am
   
Thank u Staffan! it is now clear to me. Your answer was of great help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900