Results 1 to 9 of 9

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    328

    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
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
    328

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

    Thanks. It's working fine.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  5. #5
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  9. #9
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
  •  



Click Here to Expand Forum to Full Width