Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
protected void txtsub_Click(object sender, EventArgs e)
   {

       User_reg ob1 = new User_reg();

       var res = (from test in ob.User_reg select test).ToList();
       foreach (var row in res)
       {
           if (row.email != txtmail.Text)
           {

               ob1.name = txtname.Text;
               ob1.address = txtadd.Text;
               ob1.phone_num = txtphn.Text;
               ob1.stream = txtstrm.Text;
               ob1.qualification = txtqlf.Text;
               ob1.email = txtmail.Text;
               ob1.password = txtpass.Text;

                ob.User_reg.AddObject(ob1);
              }

           else
           {
               lbl.Text = "Email Id already exists";

           }

   }

           ob.SaveChanges();

           }
Posted
Updated 7-Jan-14 18:20pm
v2

Should be something like:

C#
bool found = false;
foreach (var row in res)
{
    if (row.email != txtmail.Text)
    {
        found=true;
    }

}

if (!found)
{
    ob1.name = txtname.Text;
    ob1.address = txtadd.Text;
    ob1.phone_num = txtphn.Text;
    ob1.stream = txtstrm.Text;
    ob1.qualification = txtqlf.Text;
    ob1.email = txtmail.Text;
    ob1.password = txtpass.Text;

    ob.User_reg.AddObject(ob1);
    ob.SaveChanges();
}
else
    lbl.Text = "Email Id already exists";


Otherwise you are checking every row and adding one entry for each row that doesn't match. Remember, foreach is a loop, you need to check all the rows for a duplicate and then after you've verified that there are no matches, THEN you an add it.

BTW, the loop is probably not necessary for finding if there is a duplicate email. You can use Linq for this in your select statement at the beginning, but I leave that as an exercise for you.
 
Share this answer
 
Hi
First at all you should tell me what is your DBMS because making primary key or unique key has a different solutions at each DBMS but as a general you should set unique flag for your mail field if you want repel inserting users detail with same emails. But if you want repel inserting at UI(application layer validation) your code has not true functionality you can use Linq Query for checking exist records with same email really easily.

C#
var result =  ob.User_reg.Find(u=>u.Email.ToString() == txtmail.Text);
if(result == null)
{
    //TODo: write your insert code
}
else
 lbl.Text = "Email Id already exists";


Best Regards
 
Share this answer
 
v3
Comments
Member 10488678 8-Jan-14 0:45am    
as per your code in this case new email id is not adding in the database
Aydin Homay 8-Jan-14 0:50am    
Please check again my code repel inserting exist email id not new email id.
Use trim() function to trim off extra spaces, because even white space counts. And since email id is string, it is better to use string compare functions.
 
Share this answer
 

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