Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I know it must so simple but I am missing something silly which I couldn't figure out.
So Here I have search button with Txtbox1 and FromDate and ToDate

Either Txtbox1 should be filled or FromDate and ToDate (both dates) should be filled. Then only Search code should proceed and display the result. Otherwise it should pop up saying enter search criteria.

Can anyone here help me if this silly logic with if else in VB.NET?


Moved from comment

VB
If Not (txtSInvoice IsNot String.Empty) Or Not (txtFromDate IsNot String.Empty AndAlso txtToDate IsNot String.Empty) Then

ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Hello Search", True)
Else
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Please enter Criteria for Search", True)

End If

I tried this but it doesn't work. I don't know about VB. But in C# it does what you said "txt1 or (frmDate and Todate)"
Posted
Updated 13-Jan-15 10:29am
v2
Comments
Maciej Los 13-Jan-15 16:21pm    
What have you tried?
sudevsu 13-Jan-15 16:25pm    
If Not (txtSInvoice IsNot String.Empty) Or Not (txtFromDate IsNot String.Empty AndAlso txtToDate IsNot String.Empty) Then

ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Hello Search", True)
Else
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Please enter Criteria for Search", True)

End If
sudevsu 13-Jan-15 16:28pm    
I tried this also
If (txtSInvoice.Text.Length > 0) Or (txtFromDate IsNot String.Empty AndAlso txtToDate IsNot String.Empty) Then
Tomas Takac 13-Jan-15 16:46pm    
The logic looks fine. Try using String.IsNullOrEmpty[^] or even String.IsNullOrWhiteSpace[^] methods to check for empty values.
sudevsu 14-Jan-15 9:39am    
Thank you Tom. This is below code that I changed and it works fine now.

If (txtSInvoice.Text.Length > 0) Or Not (String.IsNullOrEmpty(txtFromDate.Text) AndAlso String.IsNullOrEmpty(txtToDate.Text)) Then

ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Hello Search", True)
Else
ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Please enter Criteria for Search", True)

Quote:
it should pop


Is that what your users want? I wouldn't.


Better to have the Search button disabled until criteria have been entered.

I'm not familiar enough with what you're doing, but how about something like:
searchbutton.Enabled = Txtbox1.Length>0 or ( FromDate... and ToDate... )
 
Share this answer
 
Comments
PIEBALDconsult 13-Jan-15 16:30pm    
I think you have too many nots in there.
sudevsu 13-Jan-15 16:32pm    
Yeah if I don't say Not still it goes in so I tried placing a not
Hey, don't be lazy! Try it: If...Then...Else Statement (Visual Basic)[^]

In pseudo-code:
If TextBox is not empty then
   Procceed
Else
   DisplayMessage
End If
 
Share this answer
 
Comments
sudevsu 13-Jan-15 16:30pm    
I try the same way but it doesn't work.
If (txtSInvoice.Text.Length > 0) Or (txtFromDate IsNot String.Empty AndAlso txtToDate IsNot String.Empty) Then

If I don't give any criteria, still search code proceeds instead of display message.
sudevsu 13-Jan-15 16:35pm    
Nothing is there to be lazy in here. But I just don't get the point of two conditions in one if with Or and And in VB.NET.
hi sudevsu,
Marciej has a good point, you should step through your code and test the values independently so that you get a good idea on how to use Boolean operators correctly (Hint - first NOT is a problem).

regs ron O.
 
Share this answer
 
Based on Tomas advice. I made this working. Thanks to all the guys who helped in this.


VB
If (txtSInvoice.Text.Length > 0) Or Not (String.IsNullOrEmpty(txtFromDate.Text) AndAlso String.IsNullOrEmpty(txtToDate.Text)) Then

              ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Hello Search", True)
          Else
              ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.GetType(), "alert", "Please enter Criteria for Search", True)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900