|
-
Mar 10th, 2011, 10:46 PM
#1
Thread Starter
New Member
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 ?
-
Mar 11th, 2011, 12:16 AM
#2
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.
-
Mar 11th, 2011, 02:08 AM
#3
Thread Starter
New Member
Re: NULL - Interger input - VB.X (2010)
 Originally Posted by jmcilhinney
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..
-
Mar 11th, 2011, 04:17 AM
#4
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?
-
Mar 11th, 2011, 04:32 AM
#5
Thread Starter
New Member
Re: NULL - Interger input - VB.X (2010)
 Originally Posted by jmcilhinney
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 ?
 Originally Posted by jmcilhinney
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
 Originally Posted by jmcilhinney
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
-
Mar 11th, 2011, 05:58 AM
#6
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.
-
Mar 11th, 2011, 07:46 AM
#7
Thread Starter
New Member
Re: NULL - Interger input - VB.X (2010)
 Originally Posted by jmcilhinney
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.
 Originally Posted by jmcilhinney
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 

 Originally Posted by jmcilhinney
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.
-
Mar 11th, 2011, 09:15 AM
#8
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
 Originally Posted by masokis
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|