|
-
Dec 7th, 2010, 09:15 PM
#1
Thread Starter
Fanatic Member
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
-
Dec 8th, 2010, 12:24 AM
#2
Member
Re: Convert null to zero
Code:
Int32.Parse(IIf(no_single_room.Text = "", 0, no_single_room.Text))
-
Dec 8th, 2010, 05:17 AM
#3
Frenzied Member
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 
-
Dec 8th, 2010, 05:43 AM
#4
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:
Gary
-
Dec 8th, 2010, 05:44 AM
#5
Re: Convert null to zero
 Originally Posted by Knvn
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
-
Dec 8th, 2010, 07:22 AM
#6
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.
-
Dec 8th, 2010, 07:25 AM
#7
Member
Re: Convert null to zero
 Originally Posted by gep13
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.
-
Dec 8th, 2010, 08:27 AM
#8
Re: Convert null to zero
 Originally Posted by Knvn
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|