Results 1 to 9 of 9

Thread: [02/03] how to send an empty text box value to database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    [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:
    1. Sub SaveCustomer(ByVal s As Object, ByVal e As EventArgs) Handles btnSave.Click
    2.  
    3.         If Page.IsValid Then
    4.  
    5.             Dim objCustomer As New BusinessLogicLayer.MyCustomers
    6.  
    7.              objCustomer.RecordId = CType(txtRecordId.Text, Long)
    8.             objCustomer.CustomerNo = CType(txtCustomerNo.Text, Long)
    9.             objCustomer.RecordDate = CType(CurrentDate.Text, DateTime)
    10.             objCustomer.Customername = txtCustomerName.Text
    11.             objCustomer.CustomerCompanyName = txtCompanyName.Text
    12.             objCustomer.CustomerBillingAddress = txtBillingAddress.Text
    13.             objCustomer.CustomerTelePhoneNo = CType(txtTelePh.Text, Long)
    14.          
    15.  
    16.            If Not objCustomer.Save() Then
    17.               lblError.Text = "Could not save Customer"
    18.  
    19.            End If
    20.         End If
    21.     End Sub 'SaveUser

    while i've declared variables in "MyCustomers" class as. here i've initialized those variables which are optional

    VB Code:
    1. Private _CustomerNo As Long = 0
    2.         Private _RecordId As Long
    3.         Private _RecDate As DateTime
    4.  
    5.         Private _Customername As String
    6.         Private _CustomerCompanyName As String = ""
    7.         Private _CustomerBillingAddress As String
    8.         Private _CustomerTelePhoneNo As Long

  2. #2
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] how to send an empty text box value to database

    Quote 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:
    1. objCustomer.CustomerNo = CType(txtCustomerNo.Text, Long)

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] how to send an empty text box value to database

    Quote 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

  6. #6
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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:
    1. CType(txtCustomerNo.Text, Long)
    Then u will get an error as "" cannot be converted to a number.


    Woka

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    244

    Re: [02/03] how to send an empty text box value to database

    Quote 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:
    1. 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!!!!

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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:
    1. Public Function ConvertToInt(Byval TextValue As String) As Integer
    2.    Dim IntValue As Integer = 0
    3.    Try
    4.        IntValue = System.Convert.ToInt32(TextValue)
    5.    Catch ex As Exception
    6.        'error, cannot be converted
    7.    End Try
    8.  
    9.    Return IntValue
    10. End Function
    Then do:
    VB Code:
    1. MyCustomer.CustomerID = ConvertToInt(txtID.Text)

    or something like that.

    woka

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    13

    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
  •  



Click Here to Expand Forum to Full Width