|
-
Apr 25th, 2006, 04:49 PM
#1
Thread Starter
Addicted Member
[02/03] how to send an empty text box value to database
i've created a web page taking inputs from user. There are some options for users which are optional. but when it is left empty; an error comes saying format of string is not correct. how can i do this? I've put "fieldvalidator" only for compulsory input. I'm inserting data in sql server 2000. here is my code
VB Code:
Sub SaveCustomer(ByVal s As Object, ByVal e As EventArgs) Handles btnSave.Click
If Page.IsValid Then
Dim objCustomer As New BusinessLogicLayer.MyCustomers
objCustomer.RecordId = CType(txtRecordId.Text, Long)
objCustomer.CustomerNo = CType(txtCustomerNo.Text, Long)
objCustomer.RecordDate = CType(CurrentDate.Text, DateTime)
objCustomer.Customername = txtCustomerName.Text
objCustomer.CustomerCompanyName = txtCompanyName.Text
objCustomer.CustomerBillingAddress = txtBillingAddress.Text
objCustomer.CustomerTelePhoneNo = CType(txtTelePh.Text, Long)
If Not objCustomer.Save() Then
lblError.Text = "Could not save Customer"
End If
End If
End Sub 'SaveUser
while i've declared variables in "MyCustomers" class as. here i've initialized those variables which are optional
VB Code:
Private _CustomerNo As Long = 0
Private _RecordId As Long
Private _RecDate As DateTime
Private _Customername As String
Private _CustomerCompanyName As String = ""
Private _CustomerBillingAddress As String
Private _CustomerTelePhoneNo As Long
-
Apr 25th, 2006, 05:04 PM
#2
Re: [02/03] how to send an empty text box value to database
1) use integer instead of long.
2) what field are NOT required.
3) When u get the error, which line is it on, and what is the value in the textbox?
Woka
-
Apr 25th, 2006, 05:08 PM
#3
Thread Starter
Addicted Member
Re: [02/03] how to send an empty text box value to database
 Originally Posted by Wokawidget
1) use integer instead of long.
2) what field are NOT required.
3) When u get the error, which line is it on, and what is the value in the textbox?
Woka
1. I can't use integer cuz digits required to enter may lie in between 10-15
2. All of the fileds which i have initialized here are not required.
3. it's giving error on customerNo
VB Code:
objCustomer.CustomerNo = CType(txtCustomerNo.Text, Long)
-
Apr 25th, 2006, 05:14 PM
#4
Re: [02/03] how to send an empty text box value to database
10 and 15...???
ie 10,11,12,13,14 or 15???
so, when it fails, what is txtCustomerNo.Text? is it "" by any chance?
Woka
-
Apr 25th, 2006, 05:19 PM
#5
Thread Starter
Addicted Member
Re: [02/03] how to send an empty text box value to database
 Originally Posted by Wokawidget
10 and 15...???
ie 10,11,12,13,14 or 15???
so, when it fails, what is txtCustomerNo.Text? is it "" by any chance?
Woka
well; it's 13
how can it be ""?? i've pasted code. i'm converting it to long before setting the value. in MyCustomer class; i've initialized it as '0'. then how can it be "" is user doesn't enter anything in it. i mean leave it empty
-
Apr 25th, 2006, 05:46 PM
#6
Re: [02/03] how to send an empty text box value to database
well if the number is less that 32000000 ( or something like that, can't remember the exact max value for an int, but .net int is same as vb6 long) then use an int, not a long.
also:
if the textbox contains "" and you do:
VB Code:
CType(txtCustomerNo.Text, Long)
Then u will get an error as "" cannot be converted to a number.
Woka
-
Apr 25th, 2006, 05:56 PM
#7
Thread Starter
Addicted Member
Re: [02/03] how to send an empty text box value to database
 Originally Posted by Wokawidget
well if the number is less that 32000000 ( or something like that, can't remember the exact max value for an int, but .net int is same as vb6 long) then use an int, not a long.
also:
if the textbox contains "" and you do:
VB Code:
CType(txtCustomerNo.Text, Long)
Then u will get an error as "" cannot be converted to a number.
Woka
I get an error saying
"Input string was not in a correct format"
then what should i do?? In database i've declared it as BigInt cuz only "int" doesn't store value of more than 10 digits. we see in filling the forms of web service that it allows some filed to be optional. say when filling forms for email addresses. there are many optional fields. how they do this?? plz help me. it's urgent!!!!
-
Apr 25th, 2006, 06:11 PM
#8
Re: [02/03] how to send an empty text box value to database
are you storing a number, or text...ie:
"64343539836373" is not always a number if you catch by drift...ie...if your storing an order number. lets say in the format Year, month, day:
20060723
This is not a "number", but instead a unique code, and should be stored as text in the DB.
Write a function like:
VB Code:
Public Function ConvertToInt(Byval TextValue As String) As Integer
Dim IntValue As Integer = 0
Try
IntValue = System.Convert.ToInt32(TextValue)
Catch ex As Exception
'error, cannot be converted
End Try
Return IntValue
End Function
Then do:
VB Code:
MyCustomer.CustomerID = ConvertToInt(txtID.Text)
or something like that.
woka
-
May 3rd, 2006, 01:26 PM
#9
Registered User
Re: [02/03] how to send an empty text box value to database
While using the exception approach will work, it is catching the error after it occurs.
Which is not as effecient as preventing it in the first case.
Why not just use an if statement to see if there is a value in the box before converting?
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
|