dcsimg
Results 1 to 8 of 8

Thread: Convert null to zero

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Convert null to zero

    i have following insert code

    Code:
           Dim hotel_book As String = "insert into hotelbooking (Userid, hotel_requirement_Types, singleroom, single_no_room,doubleroom,double_no_room,checkin_date,checkout_date) " & _
                          "VALUES (" & userid & ", '" & ckchecked.Text & "' , " & Convert.ToByte(boolsing) & " ," & Int32.Parse(no_single_room.Text) & "," & Convert.ToByte(booldoub) & "," & Int32.Parse(no_double_room.Text) & " ,'" & txt_checkin.Text & "' ,'" & Me.txt_checkout.Text & "' )"
    i want that if no_single_room.Text is null and have no value it should convert it into zero other wise Int32.Parse(no_single_room.Text) in same insert statement

    Thanks
    There is no achievement without goals

  2. #2
    Member
    Join Date
    Jan 2010
    Posts
    42

    Re: Convert null to zero

    Code:
    Int32.Parse(IIf(no_single_room.Text = "", 0, no_single_room.Text))

  3. #3
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: Convert null to zero

    hay,
    Code:
    no_single_room.Text
    is empty string not null
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Convert null to zero

    Hello there,

    As a point to note, you should NOT concatenate your SQL string like that, you leave yourself open to SQL Injection attacks. You should ALWAYS use parameters. There is a link in my signature that provides more information on this topic.

    In addition to the above suggestions, you might want to look into the use of:

    Code:
    String.IsNullOrEmpty
    As well as:

    Code:
    Int32.TryParse
    Gary

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Convert null to zero

    Quote Originally Posted by Knvn View Post
    Code:
    Int32.Parse(IIf(no_single_room.Text = "", 0, no_single_room.Text))
    If no_single_room is actually Null, then trying to access a member on this object will throw an exception.

    Gary

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Convert null to zero

    You can use VAL(no_single_room.Text)
    or If(String.IsNullOrEmpty(no_single_room.Text), 0, no_single_room.Text)
    or Int32.Parse("0" & no_single_room.Text)
    or one of the many other ways.

    What I would really recommend is that you use Parameters instead of SQL string which is both more secure as well as parses the appropriate datatype correctly.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    Member
    Join Date
    Jan 2010
    Posts
    42

    Re: Convert null to zero

    Quote Originally Posted by gep13 View Post
    If no_single_room is actually Null, then trying to access a member on this object will throw an exception.

    Gary
    Ya its true, but the default value of 'Text' property of the TextBox control is 'String.Empty' and the user cannot insert 'Null' on UI.

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Convert null to zero

    Quote Originally Posted by Knvn View Post
    Ya its true, but the default value of 'Text' property of the TextBox control is 'String.Empty' and the user cannot insert 'Null' on UI.
    I guess that assumes that no_single_room is actually a UI control, but there was no mention of that by the OP.

    Gary

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width