dcsimg
Results 1 to 9 of 9
  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Resolved [RESOLVED] Getting error when there is no data in Table.

    The following code is working fine when there is some data available in CustomersTable. But getting an error when there is no data in that table. How to deal with this error.

    Code:
    Public Function cboLoadSelectCity()
            Dim StrSql As String = "Select Distinct City from CustomersTable"
            Dim dt As New DataTable
            If Not DAL.ExecuteSql(dt, StrSql, Msg) Then MsgBox(Msg, MsgBoxStyle.Exclamation, "Error") : Return 0
            Dim ds As New DataSet
            ds.Tables.Add(dt)
            cboCity.ValueMember = "City"
            cboCity.DataSource = ds.Tables(0)
            cboCity.SelectedIndex = 0
            Return True
        End Function
    Error:
    Additional information: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.

  2. #2
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: Getting error when there is no data in Table.

    try this
    Code:
    if ds.Tables(0).rows.count > 0 then
    cboCity.SelectedIndex = 0
    End If

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    273

    Re: Getting error when there is no data in Table.

    Thanks. It's working fine.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,681

    Re: [RESOLVED] Getting error when there is no data in Table.

    You're trying to solve the wrong problem. Just get rid of the line that throws the exception because it's completely pointless anyway. All it does is select the first item in the bound list but that happens automatically anyway, so pointless.

    Speaking of pointless, why are you creating a DataSet there?

    Also, you should probably be setting the DisplayMember rather than the ValueMember.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: [RESOLVED] Getting error when there is no data in Table.

    Quote Originally Posted by jmcilhinney View Post
    You're trying to solve the wrong problem. Just get rid of the line that throws the exception because it's completely pointless anyway. All it does is select the first item in the bound list but that happens automatically anyway, so pointless.

    Speaking of pointless, why are you creating a DataSet there?

    Also, you should probably be setting the DisplayMember rather than the ValueMember.
    True, but then again he didnt ask for your opinion.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,681

    Re: [RESOLVED] Getting error when there is no data in Table.

    Quote Originally Posted by kpmc View Post
    True, but then again he didnt ask for your opinion.
    It's clear to me that everyone wants my opinion but they're just too shy to ask sometimes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: [RESOLVED] Getting error when there is no data in Table.

    Sorry, JMC, I was only trying to get you goin. Not very nice of me.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,681

    Re: [RESOLVED] Getting error when there is no data in Table.

    Quote Originally Posted by kpmc View Post
    Sorry, JMC, I was only trying to get you goin. Not very nice of me.
    Don't worry. I took it as a joke. I'm not that thin-skinned.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    418

    Re: [RESOLVED] Getting error when there is no data in Table.

    Good deal.

    I am actually trying to find work in Alice Springs , not to change the subject...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.