Results 1 to 8 of 8

Thread: NULL - Interger input - VB.X (2010)

  1. #1

    Thread Starter
    New Member masokis's Avatar
    Join Date
    Jan 2011
    Location
    www.masokis.com
    Posts
    8

    Exclamation NULL - Interger input - VB.X (2010)

    Hi.. and hello..
    could i ask you all a question? i had a problem.

    situation:
    =======
    the input box cannot grab the NULL value (blank).

    My own solution (but not solve)
    :
    ====================
    i'm try make try and catch. let's see my code..

    Code:
    Private Sub GetData()
            Dim row As Integer = DataGridView1.Rows.Count
            Dim N As Integer
            N = 0
            N = InputBox("Enter the number of order :")
            Try
                N = DBNull.Value <----------------- here the problem (maybe)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
                Dim jumlah As Decimal
                jumlah = Decimal.Round(N * txt_harga.Text).ToString("f2")
                Me.DataGridView1.Rows.Add(row, kodMakanMinum, txt_foodDrinkName.Text, N, jumlah)
        End Sub
    when i try to compile..i will get error
    Code:
    Error:Value of type 'System.DBNull' cannot be converted to 'Integer'.
    can you show me a "hack" to use null / blank ? to read it and catch the err0r ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: NULL - Interger input - VB.X (2010)

    Integer variables can only contain Integer values. You don't use DBNull at all except when transferring NULL values to or from a database using ADO.NET.

    You don't need a hack. If you were to explain what you're actually trying to achieve then we could explain the proper way to achieve it.
    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

  3. #3

    Thread Starter
    New Member masokis's Avatar
    Join Date
    Jan 2011
    Location
    www.masokis.com
    Posts
    8

    Exclamation Re: NULL - Interger input - VB.X (2010)

    Quote Originally Posted by jmcilhinney View Post
    Integer variables can only contain Integer values. You don't use DBNull at all except when transferring NULL values to or from a database using ADO.NET.

    You don't need a hack. If you were to explain what you're actually trying to achieve then we could explain the proper way to achieve it.
    Thanks for reply, jmcilhinney
    Ok, i'm try to achieve a proper input box. which only allow integer . backspace, null (empty), char or string is not allowed... but i could not configure it.. i create a function to call by a button. i has been mod my code.. now it look like below :

    Code:
    Private Sub GetDataToDGV()
            Dim row As Integer = DataGridView1.Rows.Count      
            Dim n1 As String
            Dim N As Integer       
            Do
                n1 = InputBox("Enter the number of order :")
                If n1 <> "" Then
                    MessageBox.Show(n1)
                    N = Convert.ToInt32(n1)
                Else
                    MessageBox.Show("Wrong input! Please try again!")
                End If           
            Loop Until n1 <> String.Empty       
            Dim jumlah As Decimal
            jumlah = Decimal.Round(N * txt_harga.Text).ToString("f2")
            Me.DataGridView1.Rows.Add(row, kodMakanMinum, txt_foodDrinkName.Text, N, jumlah)
        End Sub
    so.. i has settle the null input!
    but... i still has problem , when end-user input backspace, null (empty), char or string.

    here.. a screenshot :


    hurmm..

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

    Re: NULL - Interger input - VB.X (2010)

    Don't use the InputBox function at all... ever. Create a new form with the controls you want and the behaviour you want and then display it by calling ShowDialog.

    If you want a numeric TextBox for your form then follow the CodeBank link in my signature and check out my submission on the topic.

    Why would you not allow backspace? What's the user supposed to do if they hit the wrong key?
    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

    Thread Starter
    New Member masokis's Avatar
    Join Date
    Jan 2011
    Location
    www.masokis.com
    Posts
    8

    Wink Re: NULL - Interger input - VB.X (2010)

    Quote Originally Posted by jmcilhinney View Post
    Don't use the InputBox function at all... ever. Create a new form with the controls you want and the behaviour you want and then display it by calling ShowDialog.
    hurmm... i just want to minimun the form. but i try it first. but can the data form Form-A can be send to Form-B ?

    Quote Originally Posted by jmcilhinney View Post
    If you want a numeric TextBox for your form then follow the CodeBank link in my signature and check out my submission on the topic.
    http://www.vbforums.com/showthread.php?t=618778 , very nice

    Quote Originally Posted by jmcilhinney View Post
    Why would you not allow backspace? What's the user supposed to do if they hit the wrong key?
    backspace are not allow because, this program i try to build is to calculate order. so the inputbox is target for, how many set customer want to order.

    if they hit wrong key... i will loop back in if-else. until i get integer input.
    i still working on it. guide and hint from you are very appreciate

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: NULL - Interger input - VB.X (2010)

    That's very dodgy. If the user enters the wrong data they shouldn't have to hit OK to get another chance to enter the data. Also, what if they enter the wrong number? If they hit OK then that number will be accepted.

    Of course can be sent from one form to another. Form's are just objects like any other.
    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

    Thread Starter
    New Member masokis's Avatar
    Join Date
    Jan 2011
    Location
    www.masokis.com
    Posts
    8

    Exclamation Re: NULL - Interger input - VB.X (2010)

    Quote Originally Posted by jmcilhinney View Post
    That's very dodgy. If the user enters the wrong data they shouldn't have to hit OK to get another chance to enter the data.
    yeah.. but, maybe he/she make mistake and click OK. so, i need be prepare.

    Quote Originally Posted by jmcilhinney View Post
    Also, what if they enter the wrong number? If they hit OK then that number will be accepted.
    i also has already make a right-click menus to delete it. and it working



    Quote Originally Posted by jmcilhinney View Post
    Of course can be sent from one form to another. Form's are just objects like any other.
    oh,i see.. i new in visual basic development.

  8. #8
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: NULL - Interger input - VB.X (2010)

    I agree with jm, the inputbox function is not the best to use. But I wanted to point out something that was not mentioned. If you want someone to enter
    Quote Originally Posted by masokis View Post
    integer . backspace, null (empty)
    you MUST allow them to enter string data because you can not store null (empty) in an integer. You could declare an integer as nullable (integer?) but that can be more trouble than its worth. What has been done in the past was to input a string then test if it is null (empty) like this:
    Code:
    If strInput <> String.Empty Then ...
    Then if it is not, you can convert it to an integer like this:
    Code:
    bSuccess = Integer.TryParse(strInput, intValue)
    bSuccess will return TRUE if the conversion is successful, and if TRUE then intValue will contain the Integer.

    All that trouble goes away however, when you create your own form. I just thought it may be useful to know. Look into the keypress event of the textbox when you create your own "Inputbox".
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

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