delete on submit an entire row error Sequence contains no elements RRS feed

  • Question

  •  I have a combobox full of Income_Names. When I click the delete button I want the entire record to be deleted.  This is what I have and I keep getting this error "sequence contains no elements" in the red.

    The tables name is Income and the Income's columns are Income_ID, Income_Name and Income_Day
    When the user selects the Income Name from the combobox and clicks delete I want the record associated with the Name to be deleted. Thank you to anyone who helps.
    Code Snippet

      Private Sub DeleteIncomesFromDatabase()
            Dim dc As New ProgramDatabaseClassesDataContext
            ' Query the database for the rows to be deleted.
            Dim IncomeToDelete As String = ComboBox3.SelectedText
            Dim deleteIncomeDetails As Income = _
                (From details In dc.Incomes _
                Where details.Income_Name = IncomeToDelete _
                Select details).Single


            Catch ex As Exception
                ' Provide for exceptions
            End Try

        End Sub

    Wednesday, September 17, 2008 7:16 PM

All replies

  • The error means that there is no record in the database matching the condition you specified.  The Single() operator throws the exception if the query returns no rows.  You could use SingleOrDefault() instead, and it would return 'Nothing' instead of throwing the exception.  You'd have to handle that case though or you'd just cause an exception with the call to DeleteOnSubmit.




    Wednesday, September 17, 2008 7:52 PM
  • Hi,


    Your query isn't returning any information.  Then, when Single executes, it throws the exception with that message. Here's what I would do:


    1. Check the database to ensure there is a record with Income_Name set to the value you think you should be getting.  Copy that value and make sure you know what all the characters are.  i.e. Sometimes you'll have spaces on the end of a field, but not realize it.


    2. Set a breakpoint and look at what IncomeToDelete gets set to.  Also, look to see if it has any spaces, which happens sometimes.  Compare the value from IncomeToDelete to what you have in the database.  If there isn't a match, that's the reason your query doesn't return what you expect.


    3. If there is a possibility of the query not returning an object, you could use SingleOrDefault instead of Single. Then put an if statement around the DeletOnSubmit and SubmitChanges calls to make sure they're only called if deleteIncomeDetails isn't null (er.. Nothing).


    On another note, design-wise, I typically load a ComboBox straight from the database and set either Tag (WinForms) or Value (ASP.NET) to the id of the database record the text goes with.  That way, queries are based on an ID from the database, rather than text comparisons that sometimes don't work because of various conditions in business logic.




    Wednesday, September 17, 2008 8:05 PM
  • Thank you Matt and Joe but the user creates the list in the combobox so I wouldn't be able to put an actual id in the code. and when I use .singleordefault I get an error that tells me that it can't be a null value. Is there a better way make sure that the Income_Name matches the combobox's selected text and deletes the entire row? Thanks again...
    Wednesday, September 17, 2008 9:05 PM
  • Hi 

    if it is possible, bind your combobox to income table, and when you want  to delete selected object get deleted Object By  (combobox.selectedItem as Income) and use deleteOnSubmit().

    but if your combobox fill by a list with user, you can execute direct query to sql with db.ExecuteQuery();

    If exists(Select * from dbo.Income where Name = @Name)
    Delete From dbo.Income where Name = @Name                  

    excuse me for my bad english.
    Thursday, September 18, 2008 9:49 AM
  • Hi thank you Ali Asghar Alimardani.I bound my combobox to the table but my code doesn't seem to be in the proper order or I'm missing something.I keep getting this error"Value of type "string" cannot be converted to 'application.Income'Here is the code I'm using and the area that the error comes up.  When I use "as Income" in place of "as string" I get the same error but on the "ComboBox3.SelectedText"

    Code Snippet

        Private Sub DeleteIncomesFromDatabase()
            Dim dc As New ProgramDatabaseClassesDataContext
            ' Query the database for the rows to be deleted.
            Dim IncomeToDelete As String
            IncomeToDelete = ComboBox3.SelectedText



    End Sub

    Tuesday, October 14, 2008 3:57 AM
  • HI


    Dim IncomeToDelete As Income
    IncomeToDelete = ComboBox3.SelectedItem as Income



    ComboBox.SelectedItem return Object and you can cast it as your object.

    comboBox3.SelectedItem as Income

    be consider that DeleteOnSubmit() Method get an object to Delete not a string.




    Tuesday, October 14, 2008 5:39 AM