sir i m working on a project and i want to pass a value in query string and redirect it to next page .....ok
now on next page i want to make a check on this
if the value of querystring is equal to a particular value then a message show....
i have done upto here
now the prob comes if the values is not matched the code written in else is not working
i m writting my code:-----
if(request.param.count>0) then
label1.visible="true"
label1.text="hello"
else
label1.visible=false
Something like the following will test your QueryString to a string value.
If (Request.QueryString("MyString") == "hello") Then
label1.Text = "Hello";
label1.Visible = true;
Else
label1.Visible = false;
End If
In your code, you had "'s around the true and false values. You don't need these.
Related
in VB.NET I have TextBox with masked time input such as
__:__
Before I save this to SQL database I have a conversion to iso format like:
Dim MyTime As DateTime = DateTime.ParseExact(txtMyTime.Text, "HH:mm", Nothing)
Dim isoMyTime As String = MyTime.ToString("HH:mm:ss")
Now my question is how to write to database Null value if this text box is empty without time value input?
not sure if you are stuck on SQL, or to figure out wether the textbox has been given a value.
when you write to database, you define the columns that you want to write to:
insert into Table1(Forename, Surname)
values(John, Jehnson)
if you want the surname to be null, simply do not mention it:
insert into Table1(Forename)
values(John)
Make sure your column is designed to allow nulls.
to figure out if the textbox has a value, simply Check if the values matches a actual valid format
solution 1
you can achieve this with a Regex:
[0-9]{2}:[0-9]{2}
this regex makes sure that there are 2 numbers before and after the : sign.
solution 2
You can also replace the textbox with 2 numericupdown controls.
solution 3
another option is to try to parse it manually.
dim ISvalid as boolean = true
if textbox1.contains(":")
if split(textbox1, ":").count = 2
if split(textbox1, ":").first.length = 2 andalso split(textbox1, ":").last.length = 2
if IsNumeric(split(textbox1, ":").first) andalso isnumeric(split(textbox1, ":").last
' this is a valid value
else
isvalid = false
end if
else
isvalid = false
end if
else
isvalid = false
end if
else
isvalid = false
end if
if isvalid then
Dim MyTime As DateTime = DateTime.ParseExact(txtMyTime.Text, "HH:mm", Nothing)
Dim isoMyTime As String = MyTime.ToString("HH:mm:ss")
end if
I have this string abcd, and I want to replace the a with [a|b] and the b with [c|d]
I try many ways to do it, like
Dim varString As String = "abcd"
varString = varString.Replace("a", "[a|b]")
varString = varString.Replace("b", "[c|d]")
The result I get is
[a|[c|d]][c|d]cd
Instead I want it like this
[a|b][c|d]cd
The problem is every time I use the replace function it backs to change the values I already replaced before so I replaced a with [a|b] but then when I do my second command to replace the b it changes the b in [a|b] that I just changed and I don't want this.
I tried to use StringBuilder but it gives the same result.
Please advise me,
I solved the problem by making an array in this way
Dim NewCommand As String = "abcd"
For i = 0 To LikeCommand.Length - 1
If LikeCommand(i) = "a" Then
NewCommand += "[a|b]"
ElseIf LikeCommand(i) = "b" Then
NewCommand += "[c|d]"
Else
NewCommand += LikeCommand(i)
End If
Next
LikeCommand = NewCommand
Or just switch the logic up. But obviously I'm thinking you're using a basic example for a more complex question.
dim varString as string = "abcd"
varString = varString.Replace("b" ,"[c|d]")
varString = varString.Replace("a" ,"[a|b]")
That would get you the desired results.
i have been assigned to write a program which saves data about films.We are required to use windows forms in vb.Im trying to validate the data entry for the text box.The text entered must be numbers and must be 4 characters long.The problem im trying to solve is that once it has looked at the value i want the program to allow the user to re-enter a value into the text box but instead it keeps looking at the text box with nothing in then says that it is not in the correct format , without letting the user enter a correct value.here is the code , just for the validation so far:
Dim PassValidate As Boolean = False
ReadFilmData()
NOFilms = NOFilms + 1
ReDim Preserve Film(NOFilms)
'Validate before it is saved to the file'
Do
If IsNumeric(Me.txtFilmID.Text) = True Then
If Me.txtFilmID.TextLength = 0 Then
'Presence check'
MsgBox("You are required to provide a value for the film ID")
ElseIf Me.txtFilmID.TextLength > 4 Then
'Length Check'
MsgBox("The film ID must be 4 numbers long")
ElseIf Me.txtFilmID.TextLength = 4 Then
PassValidate = True
End If
End If
If IsNumeric(Me.txtFilmID.Text) = False Then
MsgBox("The film ID must be a set of numbers ")
End If
If PassValidate = False Then
txtFilmID.Text = ""
MsgBox("Re-enter the value for Film ID")
Call SaveFilmData()
End If
If PassValidate = True Then
Film(NOFilms).FilmID = txtFilmID.Text
End If
Loop Until PassValidate = True
you should check out the validation events in WinForms. They are there to avoid the very problem you are describing.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Try removing the Do Loop, there does not seem to be a need for the loop.
I've seen plenty of questions here about strings that are equal returning as unequal, but trust me to not get that problem.
I have this function.
Protected Sub ChkValidStockCode()
If Not (Voucher.ValidStockCode = "") Then
Dim validcount As Int32 = 0
Dim validproduct As String = Product.GetProductNameByCode(Voucher.ValidStockCode)
For Each rpi As RepeaterItem In rptCart.Items
Dim ProductID As HyperLink = CType(rpi.FindControl("hlProductID"), HyperLink)
Dim ProductName As HyperLink = CType(rpi.FindControl("hlProductName"), HyperLink)
If (String.Compare(Voucher.ValidStockCode.ToString(), ProductID.ToString())) Then
validcount = validcount + 1
End If
Next
If validcount = 0 Then
txtVoucher.Text = "Sorry, this voucher is only valid when purchasing a " & validproduct
failed = True
Exit Sub
End If
End If
End Sub
It's supposed to compare the two strings and increment a validcount integer if they're equal, and then tell you off if it gets to the end of the repeater without finding any matches.
The variables in this test are LT00004 (Voucher.ValidStockCode) and SP08076 (ProductID.ToString())
I have run the code several times, outputting the different strings as the result and can confirm they are what they should be, but when I try to compare them (and I expect validCount to be 0), they return as a match.
What did I do to screw this up?
You probably want String.Equals() and not String.Compare(). Compare is used to order things and not test for equality. What's happening is String.Compare is returning a non-zero number so the condition is being satisfied. The reason for that is because in VB "0" is False but any non-zero number evaluates to true. There's a whole history behind why that's the case but I digress.
I am taking a VBA class and am completely stuck on this problem. We CANNOT use the masked text box, which would solve this problem. Instead the professor actually wants me to learn the code, can you imagine?
All kidding aside, the user needs to enter a gas price into a text box, then hit calculate to receive the total cost of the trip. There is much more to the interface but will spare you the details. If a user enters anything else number than a positive number with one decimal place, it should return an error. I have figured out 0 or 0000 as well as a negative number such as -3.45. Now I have to get any text or special characters to give me an error as well as something like 34.56.12.45. You never know, a user may feel the need to type in their IP address. The key to the assignment is that I catch all probable user errors.
Here is what I've written for the calculation as well as catch the errors. I have tried the Try/Catch statements as well. Nothing worked but I got the first two parts of the IF statement to work yet always failing on the last IF part until it gets to the calculation.
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim Mileage As Decimal
Dim Miles As Decimal
Dim GasPrice As Decimal
Dim Cost As Decimal
If CDec(txtbxGasPrice.Text) = 0 Then
MessageBox.Show("Please enter a positive dollar amount")
txtbxGasPrice.Text = String.Empty
End If
If CDec(txtbxGasPrice.Text) < 0 Then
MessageBox.Show("Please enter a positive dollar amount")
txtbxGasPrice.Text = String.Empty
End If
If Cost = CDec((Miles / Mileage) * GasPrice) Then
Miles = CDec(lblTMiles.Text)
Mileage = CDec(lblMileage.Text)
GasPrice = CDec(txtbxGasPrice.Text)
lblTotalCost.Text = Cost.ToString("C2")
End If
If CBool(txtbxGasPrice.Text = "") Then
MsgBox("You must enter a dollar amount")
End If
*If Not IsNumeric(txtbxGasPrice.Text) Then
MessageBox.Show("Please enter a positive dollar amount")
txtbxGasPrice.Text = String.Empty*
End If
End Sub
'I have placed this at the top, in the middle, at the bottom but no luck. What am I missing?
Appreciate your thoughts - Lauren
This one seems to meet your criteria and pass David's tests:
Function IsValid(txt As String) As Boolean
If Not IsNumeric(txt) Then
Exit Function
End If
If Len(txt) < 2 Then
Exit Function
End If
If Not Mid(txt, Len(txt) - 1, 1) = "." Then
Exit Function
End If
If Not txt > 0 Then
Exit Function
End If
IsValid = True
End Function
This seems like a perfect application of regular expressions, but could be out of scope for this problem, maybe even better though vb has a Decimal.TryParse(or parse) that will take a string and try to parse it to a decimal.
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx
on a side not I'm not 100% sure how it acts with xx.xx.xx but I'm betting it will fail and help your problem
I'm going to expand on CSgoose's idea to use RegEx, it seems a lot more reliable. I am very, very green when it comes to using RegEx but I try to work on Q's here at SO, so this may not be the optimal pattern to match, but a function like this seems to do the trick when I test a few value.
0/0.0/0.00 = False
-1.5 = False
1.5 = True
5.45 = False
Steve = False
Steve.6 = False
Steve6.58 = False
6.574.2 = False
A value that evaluates to 0 will return false. Negative values return false. Value must have a decimal component, be comprised of any # of digits (this can be tweaked if you want to limit it, eg., to ##.# format, etc.). Matches full text only, so things like IP addresses won't return true, etc.
NOTE This is VBA, but should be easily adaptable for your purposes)
Sub YourSub()
If Not IsMatch(CStr(txtbxGasPrice.Text)) Then
MsgBox "Please ensure that the value you enter is a positive dollar amount, to 1 decimal place!", vbCritical, "Invalid Gas Price Value!"
End IF
End Sub
This function requires enabling reference to Microsoft VBScript Regular Expressions 5.5, or you could use late-binding.
Function IsMatch(str As String) As Boolean
'Tests for a positive numeric value, formatted 0.0 with a mandatory decimal component
' exact match only
Dim re As RegExp
Dim allMatches As MatchCollection
Dim retVal As Boolean
retVal = False 'by default
If Not IsNumeric(str) Then GoTo EarlyExit 'ignore any non-numeric value
Set re = New RegExp
re.Pattern = "\d*\.[0-9]"
Set allMatches = re.Execute(str)
If allMatches.Count = 1 Then
'If there are multiple matches, then I think safe to say it's not a match,
' make sure it's a full string match
If str > 0 Then
retVal = (allMatches(0) = str)
End If
End If
EarlyExit:
Set re = Nothing
IsMatch = retVal
End Function
Update to force, for example, ##.# format, you could do
re.Pattern = "[1-9]?\d\.\d"