Filter DataView Problem

yousuf42

Well-known member
Joined
Feb 12, 2006
Messages
101
Programming Experience
1-3
Dear all,

The code as below after filtering, gives strange data in the listbox "System.Data.DataRowView"
Here something Error I can' short out.
VB.NET:
filterview.RowFilter = "ProductID LIKE '%" & txtProductID.Text _
& "' OR ProductName LIKE '%" & txtProductID.Text & "%'"
So any one can helpme pls....
Thanks
 
It has 2 Columns

Thank You very much cjard,

But I have 2 columns(Displaymember won't accept 2 columns). I provide the complete code below. You can review and give me your best suggestion. Thanks for giving your valuable time.

VB.NET:
Public Function GetProductIDS() As DataTable
 
Dim techConnection As OleDbConnection = New OleDbConnection(strConn)
Dim techDataAdapter As OleDbDataAdapter
Dim techDataTable As DataTable
Try
Dim strSQL As String = "SELECT ProductID,ProductName FROM Products"
Dim techCommand As New OleDbCommand(strSQL, techConnection)
techDataAdapter = New OleDbDataAdapter(techCommand)
techDataTable = New DataTable
techDataAdapter.Fill(techDataTable)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return techDataTable
End Function
 
 
Private Sub FilterProductId()
Dim ProductIDs As TechManagement.DBComponents.ProductDB = New TechManagement.DBComponents.ProductDB
Dim Filterview As DataView
Filterview = New DataView(ProductIDs.GetProductIDS)
Filterview.RowFilter = "ProductID LIKE '%" & txtProductID.Text & "%' OR ProductName LIKE '%" & txtProductID.Text & "%'"
lstProducts.DataSource = Filterview
End Sub
 
Yes

Thanks cjard.

It is a picklist. The user should read productid as well productdescription and productid will be passed to another textbox in a parent form.
 
then might I suggest you use this SQL:

SELECT ProductID + "(" + ProductName + ")" as ProdIDName, ProductName FROM Products


And set your display member to the single column ProdIDName..
 
End of Statement Expected.

Thank you very much cjard,

There Blue line appears below the statement '
as ProdIDName, ProductName FROM Products" with the tip end of statement expected. is there anything wrong with space or ???.

Your help is highly appreciated.

Thanks
 
Did you paste that straight into the code?

It wont work without changing.. because " is used for strings in VB, you cant say this:

Dim str as String = "my string contains "speech marks" in the middle"


Surely you can see this error?

This is wha tyou do to put speech marks in strings:

Dim str as String = "my string contains ""speech marks"" in the middle"
 
Can you explain me more about 'Speech mark' please

Thank you very much for your reply and giving your valuable time cjard,

It would be very helpful If you could explain me about 'speech mark', I'm not that much professional in this field. I would very much appreciate If you could provide me a sample code on how could I use 'speech mark ' in my code.

Thanks
 
Ahhhhh!! again I'm wrong. But it is working..

Hi cjard,

Thank you very much. now it is working. after setting displaymember.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strSQL [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT ProductID + ""("" + ProductName + "")"" as ProdIDName,ProductName FROM Products"[/SIZE]
 

Attachments

  • error.jpg
    error.jpg
    21.3 KB · Views: 32
Last edited:
It would be very helpful If you could explain me about 'speech mark', I'm not that much professional in this field.

COmputer code is written in text. Some symbols have special meanings, like in VB the speech mark " (also called quotes, or doubleqoutes) is used to define where a string (sentence of text) starts and stoips.

Dim str as String = "Hello World"


Naturally, it thus causes difficulty when you want to use that same symbol, in the text itself. In this case, you want to send the following to the database:

SELECT "(" + field + ")" FROM table


If you use that straight in, it will confused vb, because VB will think youre trying to start and stop the string, and you end up with some illegal syntax:

Dim str as String = "SELECT "(" + field + ")" FROM table"

I underlined the bit that you want VB to see as a string, the problem is that as soon as VB hits the first " after SELECT< it will think youre trying to stop the string, then later youre trying to start it, then stop it.. Very confusing for a computer, im sure you understand.

Instead, we must use what is called and escaped character - This is a way of writing a symbol so that VB knows we want to use the symbol itself (speech mark/doublequote) and not the special meaning (start/stop a string).

In VB the escaped form of a speech mark is to double it up:


Dim str as String = "SELECT ""("" + field + "")"" FROM table"

VB knows that when you write "" in a string, you arent wishing to stop and start it, you are wishing to literally have one " in the string.

Other examples you might find as you program are:

{ in a format string
You can specify a format string and have VB put things into the placeholders:
Dim str as String = String.Format("The price of {0} is ${1:0.00} today, {2:yyyy MM dd}", "apples", 9.9941, DateTime.Now)

str will contain The price of apples is $9.99 today, 2007 03 31.
The placeholders are noted by {0}, {1:0.00} and {2:yyyy MM dd}
Note they run in order, {0, {1, {2
The bit after the colon is the formatting of that item, i.e. the number 9.9941 being formatted into 0.00 form, or the date being formatted into yyyy MM dd

What hapens though, if we want to literally write a girly bracket { followed by a 0 in a string? Same problem as before:

Dim str as String = String.Format("I want a string that literally has { followed by 0 followed by }, i.e. {0}")

This code will break because VB will think we are trying for a placeholder, but we arent. Again, this time we have to escape the { so that VB doesnt think we are trying to put placeholders in:

Dim str as String = String.Format("I want a string that literally has {{ followed by 0 followed by }}, i.e. {{0}}")

Other times, a backslash is used for escaping.. In most other languages than VB it is common place. It occurs in places in vb where vb has borrowed from those languages, such as Perl-style Regular Expressions.
RegExes are a language of their own, and very good at finding patterns in text.

RegularExpressions are all about special characters, and one for example, is the period/fullstop/dot:

.

To regex, this means "match any single character" a bit like the ? meant in old DOS

So, Dot will find anything.. How do you search for a Dot? You want to know where the first period is in a sentence. Searching using:

.

Gives you index 0, i.e. it finds the first character of the sentence and succeeds. If however, we search using:

\.

Then the first period/fullstop/dot is found.



Hopefully you now have a better understanding of the way programming languages have to work, if we want to write in the same characters that we do for everyday lives, but have those characters mean something special to the computer, then we also need to have a way of telling the computer when we dont want them as special.. just as normal, everyday characters


The following webpage might interest you:
http://blogs.msdn.com/brada/archive/2004/01/14/58851.aspx
 
Thank you very.......... much............ cjard

Thank you very.......... much............ cjard.

I never expected this much explaination. It really helped me in speech mark and also to format strings....

again thanks for your contribution in this forum and giving your valuable time.
 
Back
Top