Reputation: 39
Good day.
I am a beginner in vb.net and new in stackoverflow , I am encountering a problem in Filter
method...
Below is my code:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim zCondition As String = ""
If txtrid.Text.Length > 0 Then zCondition = zCondition & "receiptID='" & txtrid.Text & "',"
If txtsid.Text.Length > 0 Then zCondition = zCondition & "stkID='" & txtsid.Text & "',"
If txtsname.Text.Length > 0 Then zCondition = zCondition & "stkName='" & txtsname.Text & "',"
If txtsprice.Text.Length > 0 Then zCondition = zCondition & "stkPrice='" & txtsprice.Text & "',"
If txtsquantity.Text.Length > 0 Then zCondition = zCondition & "quantity='" & txtsquantity.Text & "',"
If txtdate.Text.Length > 0 Then zCondition = zCondition & "dateTime='" & txtdate.Text & "',"
If txtcid.Text.Length > 0 Then zCondition = zCondition & "custID='" & txtcid.Text & "',"
zCondition = zCondition.Substring(0, zCondition.Length - 1)
Dim dv As DataView
dv = New DataView(DsSales1.Tables(0), zCondition, "type Desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
When I execute and test the code, it's give me a error Cannot find column type.
...
It seems this line is having error:
dv = New DataView(DsSales1.Tables(0), zCondition, "type Desc", DataViewRowState.CurrentRows)
Can anyone help me solve this problem and explain to me what's the problem I am having? Thank you.
And sorry for my bad English :)
Upvotes: 1
Views: 786
Reputation: 39
This is what I have done:)
And sorry for I forgot to mention that I had also change the **,**
to ** AND **
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim zCondition As String = ""
If txtrid.Text.Length > 0 Then zCondition = zCondition & "receiptID='" & txtrid.Text & "' AND "
If txtsid.Text.Length > 0 Then zCondition = zCondition & "stkID='" & txtsid.Text & "' AND "
If txtsname.Text.Length > 0 Then zCondition = zCondition & "stkName='" & txtsname.Text & "' AND "
If txtsprice.Text.Length > 0 Then zCondition = zCondition & "stkPrice='" & txtsprice.Text & "' AND "
If txtsquantity.Text.Length > 0 Then zCondition = zCondition & "quantity='" & txtsquantity.Text & "' AND "
If txtdate.Text.Length > 0 Then zCondition = zCondition & "dateTime='" & txtdate.Text & "' AND "
If txtcid.Text.Length > 0 Then zCondition = zCondition & "custID='" & txtcid.Text & "' AND "
zCondition = zCondition.Substring(0, zCondition.Length - 5)
Dim dv As DataView
dv = New DataView(DsSales1.Tables(0), zCondition, "receiptID Desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
Upvotes: 0
Reputation: 19404
Your condition is totally wrong. This is how you do it
Dim cond As New StringBuilder(1000)
cond.Append("1 = 1") ' now, you will never add extra 'AND', 'OR' etc. 1 is always 1
' if any text box below is missing data it will not hurt anything using this technique
' NOTE spaces in front of 'AND'
If txtrid.Text.Length > 0 Then cond.Append(" AND receiptID='" & txtrid.Text & "'") ' removed commas
If txtsid.Text.Length > 0 Then cond.Append(" AND stkID='" & txtsid.Text & "'")
If txtsname.Text.Length > 0 Then cond.Append(" AND stkName='" & txtsname.Text & "'")
If txtsprice.Text.Length > 0 Then cond.Append(" AND stkPrice='" & txtsprice.Text & "'")
If txtsquantity.Text.Length > 0 Then cond.Append(" AND quantity='" & txtsquantity.Text & "'")
If txtdate.Text.Length > 0 Then cond.Append(" AND dateTime='" & txtdate.Text & "'")
If txtcid.Text.Length > 0 Then cond.Append(" AND custID='" & txtcid.Text & "'")
Dim dv As New DataView(DsSales1.Tables(0), cond.ToString(), "COL_NAME Desc", DataViewRowState.CurrentRows)
Your error most likely caused by your order by
. You probably don't have column "type"
Another question to you is txtsid
, txtsprice
- are this strings? If they are not - you need to remove single quotes in your condition.
Upvotes: 1
Reputation: 8004
In your case filter can be easier than what I am seeing here...
' Create a DataView
Dim dv As New DataView(DsSales1.Tables(0))
' Filter by an expression.
dv.RowFilter = zCondition
Also in your checks you can do: zCondition &=
to add strings... On another note get rid of **,**
after your condition when adding on another string, use **AND**
Your if's can come down to this as well...
If txtrid.Text.Length > 0 Then zCondition &= "receiptID='" & txtrid.Text & "'"
If txtsid.Text.Length > 0 AndAlso txtrid.Text.Length > 0 Then zCondition &= "AND stkID='" & txtsid.Text & "'" Else zCondition &= "stkID='" & txtsid.Text & "'"
If txtsname.Text.Length > 0 AndAlso txtsid.Text.Length > 0 Then zCondition &= "AND stkName='" & txtsname.Text & "'" Else zCondition &= "stkName='" & txtsname.Text & "'"
MrCodexer
Upvotes: 0