Syntax error: Missing operand before 'And' operator - vb.net

i am using vb.net
here is my code to filter bindingsource
i get this erro Syntax error: Missing operand before 'And' operator.
Private Function SetFilter() As String
Dim datee As String = String.Format("datee >= #{0:M/dd/yyyy}# AND datee <= #{1:M/dd/yyyy}#", _
DateTimePicker1.Value, _
DateTimePicker2.Value)
Dim client As String = If((TextBox1.Text.Length > 0), String.Format("[client] LIKE '%{0}%'", TextBox1.Text), "")
Dim ref As String = If((TextBox2.Text.Length > 0), String.Format("[REF] LIKE '%{0}%'", TextBox2.Text), "")
Return String.Format("{0} AND {1} AND {2}", datee, client, ref)
End Function
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub DateTimePicker2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker2.ValueChanged
'error here
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
SalesBindingSource.Filter = SetFilter()
End Sub

I suggest to replace your filter creating method with a more secure way to handle the content of the two textboxes and the situation in which one or both textboxes are empty
Dim client As String = If((TextBox1.Text.Length > 0), _
String.Format(" AND [client] LIKE '%{0}%'", TextBox1.Text.Replace("'", "''")),"")
Dim ref As String = If((TextBox2.Text.Length > 0), _
String.Format(" AND [REF] LIKE '%{0}%'", TextBox2.Text.Replace("'", "''"), "")
Return String.Format("{0} {1} {2}", datee, client, ref)
The replace call double a single quote inserted by your user in the textbox, the AND is directly inserted in the string for client and for ref, otherwise you get an invalid sql if one or both textboxes are empty

Related

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

DateTimePicker: the box goes back to it's original date?

Hey guys so im doing a program for a zodiac finder on vb,The program works and all but my problem is that everytime I run it the DateTimePickerBox goes back to the current date instead of the date I have entered.
Here's my unfinished code;
Public Class HoroscopeSign
Private Sub HoroscopeSign_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'DateTimePicker.Format = DateTimePickerFormat.Custom
'DateTimePicker.CustomFormat = "dd MMMM yyyy"
txtDOB.Format = DateTimePickerFormat.Short
End Sub
Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
Dim DOB As Date = txtDOB.Value
txtDOB.Text = Process(DOB)
End Sub
Public Function Process(dateOfBirth As Date) As String
Dim sign As String
Dim info As String
Dim BirthYear As Integer = dateOfBirth.Year
Dim BirthMonth As Integer = dateOfBirth.Month
Dim BirthDay As Integer = dateOfBirth.Day
If txtDOB.Value >= New Date(BirthYear, 1, 20) And txtDOB.Value <= New Date(BirthYear, 2, 18) Then
PBHoroscope.Load("E:\Aquarius.jpg")
sign = "Aquarius"
info = ""
Else txtDOB.Value >= New Date(BirthYear, 2, 19) And txtDOB.Value <= New Date(BirthYear, 3, 20) Then
PBHoroscope.Load("E:\Pisces.jpg")
sign = "Pisces"
lblSign.Text = sign
lblDescription.Text = info
End Function
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Dim a As Integer
a = MsgBox("Do you want to exit this application", vbYesNo, "Exit Application")
If a = vbYes Then
MsgBox("Thank you for using this application", vbOKOnly, "Exit Application")
End
Else
Me.Show()
End If
End Sub
Private Sub txtDOB_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtDOB.ValueChanged
Dim DOB As Date = txtDOB.Value
DOB = txtDOB.Value
End Sub
End Class
So you have declared your function Process to return a string, but you don't return anything from that function. The problem then arises when you try to assign the return value of that function to the txtDOB.Text property. You don't return anything and so the txtDOB resets itself to the current date
You should decide what string (in a date format) you want to return or better change your Process function to return a DateTime and assign that value to txtDOB.Value property. Or just change these lines to
Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
' REMOVE THIS LINE Dim DOB As Date = txtDOB.Value
' REMOVE THIS LINE txtDOB.Text = Process(DOB)
Process(txtDOB.Value)
End Sub

How to jump to a row of a DataView based on partial match search criteria?

Currently, my application uses the RowFilter property on an expression to search for a user-defined string within a DataView. Currently my code looks something like this:
Public Class MyClass
Private custView As DataView
Private Sub form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dsDataSet = <"DataAccessLayer call to SQL Stored Procedure">
custView = New DataView(dsDataSet.Tables(0))
custView.Sort = "Column Name"
Me.C1FlexGrid1.DataSource = custView
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim searchText As String = txtSearch.Text
Dim expression As String = "Column Name LIKE '" + searchText + "%'"
custView.RowFilter = expression
Me.C1FlexGrid1.DataSource = custView
End Sub
End Class
My goal is to modify the behavior of this such that instead of filtering out rows that do not meet the search results, it will keep all rows present but jump to the first instance of a partial match as the user types in the search box. If DataView.Find() supported wildcards I would be set, but unfortunately it doesn't.
The solution I've come up with is to use some iteration logic. However this is done on the object the DataView has been bound to and not the DataView itself. Although this code can be modified to do exactly that.
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
'Unselect all
Me.C1FlexGrid1.Select(-1, -1, True)
If txtSearch.Text <> "" And [column index] <> -1 Then
'Typed text
Dim s As String = txtSearch.Text.Trim.ToLower
Dim column As Int32 = [column index] + 1
'Recurse until match in first column is found
For i As Integer = 0 To Me.C1FlexGrid1.Rows.Count - 1
If C1FlexGrid1.GetData(i, column) <> Nothing Then
If C1FlexGrid1.GetData(i, column).ToString.ToLower.StartsWith(s) Then
Me.C1FlexGrid1.Select(i, column, True)
Exit Sub
End If
Else
MsgBox("Error message", vbOKOnly, "NO MATCHES")
'Reset search criteria
Call ResetSearch()
End If
Next
MsgBox("Error message", vbOKOnly, "NO MATCHES")
End If
End Sub

Invalid cast Eception unhandled by user code in vb.net

I run the site following errors occured:
System.InvalidCastException was unhandled by user code
Message="Conversion from string "kumar" to type 'Short' is not valid."
Imports System.Data.OleDb
Namespace wbdsproject
Partial Class frmprofile
Inherits System.Web.UI.Page
Dim proCon As OleDbConnection
Dim proCmd As OleDbCommand
Dim prodr As OleDbDataReader
Dim username As String
Dim userid As Int16
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
'Put user code to initialize the page here
' maillink.NavigateUrl = "mailto:saranya#gmail.com"
Dim proselqry As String
username = Session("loginuser")
lblusername.Text = username
userid = Request.QueryString("userid")
'Response.Write(userid)
proCon = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Discussion Forum\database-WBDS\SampleForum.mdb")
proCon.Open()
If Session("loginuser") = "" Then
Response.Redirect("frmlogin.aspx")
End If
proselqry = "select * from tblprofile where uid='" & userid & "'"
proCmd = New OleDbCommand(proselqry, proCon)
proCmd.ExecuteNonQuery()
prodr = proCmd.ExecuteReader
If prodr.Read Then
lbldob.Text = prodr("dob")
lblquali.Text = prodr("quali")
lblinterest.Text = prodr("interest")
lbladdress.Text = prodr("address")
maillink.Text = prodr("emailid")
maillink.NavigateUrl = "mailto:" & prodr("emailid")
lblzip.Text = prodr("zip")
lblphno.Text = prodr("phno")
End If
End Sub
Private Sub btnmodify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmodify.Click
Response.Redirect("frmregmodify.aspx?user=" & username & "&userid=" & userid & "&page=frmprofile")
End Sub
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
End Namespace
at Microsoft.VisualBasic.CompilerServices.Conversions.ToShort(String Value)
You're trying to cast a String returning function into a Int16 in this line
userid = Request.QueryString("userid")
Apparently, your return is not numeric. You can either change that from the source if you have control over it or change change the userid type to string.
I would suggest using a user defined function to test if the requesting querty string is numeric before casting to an Int16 datatype.

read single word from listbox vb

I have an order form I created in VB.NET and I have a ListBox that is populated by order. You can double click on the order and it populates the order number in the order form. The problem I'm having is that it populates the TextBox with both the order number and the persons name. How can I use a delimiter to only pull out the order number and not the name also.
Imports Business_Objects
Public Class frmSummary
Private ctrl As Controller
Dim listID As ArrayList
Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order.ID & "," & " " & order.Server)
Next
End Sub
Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
Dim result As Boolean = False
If lstOrders.Text <> "" Then
result = True
Dim frm As New OrderForm
frm.MdiParent = Me.MdiParent
frm.Show()
frm.txtOrderNo.Text = lstOrders.Text
frm.btnFetch.PerformClick()
Else
MessageBox.Show("there are no orders here to click")
End If
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
lstOrders.Items.Clear()
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order.ID & " " & order.Server)
Next
End Sub
End Class
If all of your data is being stored as a single field, or something like:
4322305 John Smith Carrots $3.00
845825 Sam White Oranges $1.25
Then you can read each record as a string, and then use split that into an array based on " " as your delimiter.
The code would look something like:
dim myArray as string() = myLongTextRecord.Split(" ")
And in that format,
textBoxName.Text = myArray[1]
You're almost there. You could use the split function, but another approach would be to add the Order object directly to the listbox and not text.
Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order)
Next
End Sub
Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
Dim result As Boolean = False
If lstOrders.Text <> "" Then
result = True
Dim frm As New OrderForm
frm.MdiParent = Me.MdiParent
frm.Show()
frm.txtOrderNo.Text = DirectCast(lstOrders.SelectedItem, Order).ID.ToString
frm.btnFetch.PerformClick()
Else
MessageBox.Show("there are no orders here to click")
End If
End Sub
You'll need to go into the Order object and override the .ToString function so that the text in the Listbox displays whatever value you want (ie. Return ID & "," & " " & Server)