Invoke(Sub) Error

Jeff Stiegler 466 Reputation points
2022-04-12T17:42:53.437+00:00

The Invoke line below produces an error relating to DBNULL value when there is no data in the SELECT string, i.e. WHERE clause. How can I prevent this error? I hope that I adequately described this.

 Command1.CommandText = "SELECT SUM(fosterparent.spaces) FROM fosterparent 
                               WHERE status2 = 'Approved' and county = 'LA'"

Invoke(Sub() Capacity = System.Convert.ToInt32(Command1.ExecuteScalar))
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2022-04-12T18:43:04.5+00:00

    Probably the simplest solution is:

    Command1.CommandText = "SELECT isnull(SUM(fosterparent.spaces), 0) FROM . . ."
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-04-12T19:34:04.383+00:00

    Hi
    Untested:

    Invoke(Sub() Integer.TryParse(CommandText, Capacity))
    
    0 comments No comments

  2. Jeff Stiegler 466 Reputation points
    2022-04-12T19:53:41.813+00:00

    This worked...thank you very much. 5 stars for you.

    0 comments No comments

  3. Jeff Stiegler 466 Reputation points
    2022-04-12T21:44:02.957+00:00

    LesHay,

    Your code did not work because VS saw TryParse and CommandText as undefined items.

    But thanks for your suggestion.

    0 comments No comments