none
Exception while verifying user exist in the data base or not in Ado.net Entity Data model using Linq. RRS feed

  • Question

  • I am working on Entity Data Model and i hv created a method to insert a record into a table of sql server 2005 DB. before inserting record i m chking whether the User with the same name already exist in the table. This is a Class Library and i am refering this in Asp.net web application. When i call this Insert method from web application i m getting exception as "Sequence contains no elements".

    Here is my source code.

    public class EntityClass
    {
        public static string InsertRecord(string username, string domain, string host)
        {
            bool isExist = false;
            bool returnNull = false;
            try
            {

                SolVeEntities entity = new SolVeEntities(); //DB Entity class
                User ud = new User(); //Table

                //before inserting user into the table, verify whether User already exist
                isExist = CheckUserExist(username);

                if (!isExist)
                {
                    ud.username = username;
                    ud.domain = domain;
                    ud.host = host;
                    ud.datetime = DateTime.Now;

                    entity.Users.AddObject(ud);
                    entity.SaveChanges();
                }
                else { returnNull = true;

                }
            }
            catch (Exception ex)
            {

            }

            if (returnNull)
            return "user " + username + " already Exist";
            else
                return username + domain + host;
        }

        private static bool CheckUserExist(string uname)
        {
            bool isExist = false;
            SolVeEntities entity = new SolVeEntities();
            User u1 = entity.Users.First(u => u.username == uname);
            if (u1.username == uname)
            {
                isExist = true;
            }
            else
            { isExist = false; }

            return isExist;
        }
    }
    I am getting exception in CheckUserExist method at User u1 = entity.Users.First(u => u.username == uname);

    When i comment the checkUserExist function, record is inserting into the DB. Any suggestions/solution pls...thanks.

    • Edited by VinayGaddam Thursday, January 17, 2013 5:49 AM added more information
    Thursday, January 17, 2013 5:37 AM

Answers

  • Maybe it throws because of First() in the query? Try FirstOrDefault() - it will return null instead of exception if there is no such user in database. And sure, u1.username == uname must be replaced with null comparison after it.
    • Proposed as answer by Alexander Sun Friday, January 18, 2013 5:28 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:30 AM
    Thursday, January 17, 2013 12:01 PM
  • hi try this code

       private static bool CheckUserExist(string uname)
        {
    
            SolVeEntities entity = new SolVeEntities();
     return  entity.Users.Count(u => u.username == uname)==0?true:false;
    
    }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by Alexander Sun Friday, January 18, 2013 5:28 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:30 AM
    Thursday, January 17, 2013 12:24 PM

All replies

  • Maybe it throws because of First() in the query? Try FirstOrDefault() - it will return null instead of exception if there is no such user in database. And sure, u1.username == uname must be replaced with null comparison after it.
    • Proposed as answer by Alexander Sun Friday, January 18, 2013 5:28 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:30 AM
    Thursday, January 17, 2013 12:01 PM
  • hi try this code

       private static bool CheckUserExist(string uname)
        {
    
            SolVeEntities entity = new SolVeEntities();
     return  entity.Users.Count(u => u.username == uname)==0?true:false;
    
    }


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by Alexander Sun Friday, January 18, 2013 5:28 AM
    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:30 AM
    Thursday, January 17, 2013 12:24 PM