0

i get two run time errors first({"Sequence contains more than one element"} ==> System.InvalidOperationException ) when there is more than once it occurred and also when i want to delete what i wrote in my textbox what should i do?

here is my code

 private AutoCompleteStringCollection GetLinqDataSourceForString(string p)
    {
        var c = new AutoCompleteStringCollection();
        c.Add( Getauto(p));
        return c;
    }

   private void txtSearch_TextChanged(object sender, EventArgs e)
    {
        txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
        txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
          txtSearch.AutoCompleteCustomSource = GetLinqDataSourceForString(txtSearch.Text);
      } 

and this one is in another layer to get info from data Base

    public string AutoUpdate(string _search)
    {
        using (var context = new Phone_BookEntities1())
        {
            var c = (from d in context.Cantacts
                     where d.Cantact1.StartsWith(_search)
                     select d.Cantact1).SingleOrDefault();
            return c;
        }
    }
5
  • Can you show Getauto(string ...)? Commented Nov 13, 2015 at 10:05
  • Probably where d.Cantact1.StartsWith(_search) is matching multiple items, hence Sequence contains more than one element. Commented Nov 13, 2015 at 10:07
  • stackoverflow.com/users/265419/james-barrass public string GetAutoupdate(string _search) { Ref_Model = new Model._Model(); return Ref_Model.AutoUpdate(_search); } #endreg Commented Nov 13, 2015 at 10:09
  • @arghya-c What should i do?? Commented Nov 13, 2015 at 10:09
  • Use FirstOrDefault() instead of SingleOrDefault(). Commented Nov 13, 2015 at 10:12

1 Answer 1

6

SingleOrDefault will throw if your sequence has more than one element. If your intention is to get the first matching contact in your table, use FirstOrDefault instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.