DataView, FindRows - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

DataView, FindRows

Bill HBill H Posts: 16Member
Ok, I've got 2 problems solved, (Thanks to Kainsworth, without who's help I wouldn't be this far !!), but here's my problem... I have the following:
Database name "Birdkeeper"
Table name "gentest"
OleDbConnection1
OleDbDataAdapter1
objDataSet1
DataView named "dvgentest"
Module with the following:
Friend f1 as DataForm1
Friend fs1 as frmsearch1
Friend dv as DataView
Friend ds1 as DataSet1
Fields named "Band", "Breed", and "Club"
there are 9 fields but was using these to get it working and add the others later.
Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim dvgentest As New DataView(ds1.Tables_("gentest"), "", "Band, Breed, Club", DataViewRowState.CurrentRows)

Dim foundRows() As DataRowView = dvgentest.FindRows(New Object_() {"TextBox1.Text"})

If foundRows.Length = 0 Then
Console.WriteLine("No match found.")
Else
Dim myDRV As DataRowView
For Each myDRV In foundRows
Console.WriteLine("{0}, {1}, {2}", myDRV_("Band").ToString(), myDRV("Breed").ToString(), myDRV("Club").ToString())
Next
End If

Me.Close()

End Sub
I am trying to use a textbox on a search form to find any records that match the textbox and display them on the datagrid on the bottom form.
I have been able to do this using a specific field in the search (Thanx Ged !!), but when I try to use the above code to find any field, I get the following: "Object reference not set to an instance of an object". In reading the material that was recommended to me (Thanx again Ged !), the above seems to be the way to go but I know I'm making a stupid error somewhere and am not sure what the error code is trying to tell me. I have my DataView "ApplyDefaultSort" set to "True".
Any help would be greatly appreciated.
Bill
«1

Comments

  • kainsworthkainsworth Posts: 200Member
    [b][red]This message was edited by kainsworth at 2004-10-11 14:41:3[/red][/b][hr]
    [b][red]This message was edited by kainsworth at 2004-10-11 14:39:53[/red][/b][hr]
    Bill,

    I haven't succeeded yet in coming up with a fix that slots straight into your code as it stands. If it's any help, here's one way I came up with that allows you to analyze each row of a DataTable and if it finds a match in any column it adds that row to a new DataTable. Once it has gone through all the rows of the original table, the DataGrid is refreshed with the DataTable that now holds the search matches.

    In this example:
    DSOriginal is the DataSet holding the original data.
    I hope that the commenting explains enough as to how it works.
    [code]
    ' To start with, show original table in DataGrid
    Dim DTOriginal As DataTable = DSOriginal.Tables(0)
    DataGrid1.DataSource = DTOriginal

    ' Create new EMPTY DataTable by cloning structure of original
    Dim DTCloned As DataTable = DTOriginal.Clone

    ' Import the matching DataRows into the cloned Table
    Dim drtemp As DataRow
    Dim j As Integer
    ' Enumerate through all rows
    For Each drtemp In DTOriginal.Rows
    ' and within each row iterate through all columns
    For j = 0 To DTOriginal.Columns.Count - 1
    ' If you find a match ..
    If drtemp.Item(j) = Me.txtSearch.Text Then
    ' add this row to the new DataTable
    DTCloned.ImportRow(drtemp)
    ' and stop looking along this row
    Exit For
    End If
    Next
    Next
    ' Update Grid with new table
    DataGrid1.DataSource = DTCloned[/code]


    If you replace the names as necessary with those from your database, I'm pretty sure it will produce the result you wanted, even if we've drifted off the DataView approach :-)

    Let me know how it goes.

    Ged




  • Bill HBill H Posts: 16Member
    : [b][red]This message was edited by kainsworth at 2004-10-11 14:41:3[/red][/b][hr]
    : [b][red]This message was edited by kainsworth at 2004-10-11 14:39:53[/red][/b][hr]
    : Bill,
    :
    : I haven't succeeded yet in coming up with a fix that slots straight into your code as it stands. If it's any help, here's one way I came up with that allows you to analyze each row of a DataTable and if it finds a match in any column it adds that row to a new DataTable. Once it has gone through all the rows of the original table, the DataGrid is refreshed with the DataTable that now holds the search matches.
    :
    : In this example:
    : DSOriginal is the DataSet holding the original data.
    : I hope that the commenting explains enough as to how it works.
    : [code]
    : ' To start with, show original table in DataGrid
    : Dim DTOriginal As DataTable = DSOriginal.Tables(0)
    : DataGrid1.DataSource = DTOriginal
    :
    : ' Create new EMPTY DataTable by cloning structure of original
    : Dim DTCloned As DataTable = DTOriginal.Clone
    :
    : ' Import the matching DataRows into the cloned Table
    : Dim drtemp As DataRow
    : Dim j As Integer
    : ' Enumerate through all rows
    : For Each drtemp In DTOriginal.Rows
    : ' and within each row iterate through all columns
    : For j = 0 To DTOriginal.Columns.Count - 1
    : ' If you find a match ..
    : If drtemp.Item(j) = Me.txtSearch.Text Then
    : ' add this row to the new DataTable
    : DTCloned.ImportRow(drtemp)
    : ' and stop looking along this row
    : Exit For
    : End If
    : Next
    : Next
    : ' Update Grid with new table
    : DataGrid1.DataSource = DTCloned[/code]
    :
    :
    : If you replace the names as necessary with those from your database, I'm pretty sure it will produce the result you wanted, even if we've drifted off the DataView approach :-)
    :
    : Let me know how it goes.
    :
    : Ged
    :
    :
    :
    :
    :

  • Bill HBill H Posts: 16Member
    : : [b][red]This message was edited by kainsworth at 2004-10-11 14:41:3[/red][/b][hr]
    : : [b][red]This message was edited by kainsworth at 2004-10-11 14:39:53[/red][/b][hr]
    : : Bill,
    : :
    : : I haven't succeeded yet in coming up with a fix that slots straight into your code as it stands. If it's any help, here's one way I came up with that allows you to analyze each row of a DataTable and if it finds a match in any column it adds that row to a new DataTable. Once it has gone through all the rows of the original table, the DataGrid is refreshed with the DataTable that now holds the search matches.
    : :
    : : In this example:
    : : DSOriginal is the DataSet holding the original data.
    : : I hope that the commenting explains enough as to how it works.
    : : [code]
    : : ' To start with, show original table in DataGrid
    : : Dim DTOriginal As DataTable = DSOriginal.Tables(0)
    : : DataGrid1.DataSource = DTOriginal
    : :
    : : ' Create new EMPTY DataTable by cloning structure of original
    : : Dim DTCloned As DataTable = DTOriginal.Clone
    : :
    : : ' Import the matching DataRows into the cloned Table
    : : Dim drtemp As DataRow
    : : Dim j As Integer
    : : ' Enumerate through all rows
    : : For Each drtemp In DTOriginal.Rows
    : : ' and within each row iterate through all columns
    : : For j = 0 To DTOriginal.Columns.Count - 1
    : : ' If you find a match ..
    : : If drtemp.Item(j) = Me.txtSearch.Text Then
    : : ' add this row to the new DataTable
    : : DTCloned.ImportRow(drtemp)
    : : ' and stop looking along this row
    : : Exit For
    : : End If
    : : Next
    : : Next
    : : ' Update Grid with new table
    : : DataGrid1.DataSource = DTCloned[/code]
    : :
    : :
    : : If you replace the names as necessary with those from your database, I'm pretty sure it will produce the result you wanted, even if we've drifted off the DataView approach :-)
    : :
    : : Let me know how it goes.
    : :
    : : Ged
    : :
    : :
    : : Here is what I have:
    : : Dim DTOriginal As DataTable = ds1.Tables("gentest")
    grdgentest.DataSource = DTOriginal
    Dim DTCloned As DataTable = DTOriginal
    Dim drtemp As DataRow
    Dim j As Integer
    For Each drtemp In DTOriginal.Rows
    For j = 0 To DTOriginal.Columns.Count - 1
    If drtemp.Item(j) = fs1.textbox1.Text Then
    DTCloned.ImportRow(drtemp)
    Exit For
    End If
    Next
    Next
    grdgentest.DataSource = DTCloned
    I made a change in the first line as it kept giving me an error message. Here is what I had to do in the module also:
    Friend f1 As DataForm1
    Friend f2 As DataForm2
    Friend fs2 As frmsearch2
    Friend fs1 As frmsearch1
    Friend dv As DataView
    Friend ds1 As New DataSet
    Friend grdgentest As New DataGrid
    It now runs up until the For Each line and then gives the error message
    "Object reference not set to an instance of an object". I think I need to add something more to the module. I also got that message on the first line until I Friended grdgentest as a new datagrid, that's why I think I need to add something there for the For Each line. Maybe I need to reference f1 in here somewhere ? I'm plugging away at it a piece at a time......but not quite there yet.
    Bill
    : :
    :
    :

  • kainsworthkainsworth Posts: 200Member
    I would think the key to your "missing object" problem is the often encountered one where you have declared a variable to hold your search form:
    [code]
    Dim fs1 as frmSearch1
    [/code]
    but you haven't completed the instance of a frmSearch1 object (ie. instantiated it) with:
    [code]
    fs1 = New frmSearch1
    ' or of course, you could just use:
    Dim fs1 as New frmSearch1
    [/code]
    Pop that into the mixer and see if it throws out the result you want.

    Ged






  • Bill HBill H Posts: 16Member
    : I would think the key to your "missing object" problem is the often encountered one where you have declared a variable to hold your search form:
    : [code]
    : Dim fs1 as frmSearch1
    : [/code]
    : but you haven't completed the instance of a frmSearch1 object (ie. instantiated it) with:
    : [code]
    : fs1 = New frmSearch1
    : ' or of course, you could just use:
    : Dim fs1 as New frmSearch1
    : [/code]
    : Pop that into the mixer and see if it throws out the result you want.
    :
    : Ged
    :
    : I wish I could say that did it, but I'm still getting the same error message on the For Each line.
    Bill
    :
    :
    :
    :
    :

  • kainsworthkainsworth Posts: 200Member
    One little syntax I did notice in the code you posted was that you had:
    [code]
    Dim DTCloned As DataTable = DTOriginal
    'and not

    Dim DTCloned As DataTable = DTOriginal.Clone
    [/code]
    ie. You left off the ".Clone" method.

    I'm not saying that this is the cause of the Object Instance error - I think you'll get a different exception - but I do think you probably need to make the edit anyway.

    Looking back over my (working) example and your (non-working) one, I start to wonder if the key to your problem is the DataTable named DTOriginal. One thing you ould try is to check that DTOriginal has been correctly created and populated. Something like:
    [code]
    Debug.WriteLine(DTOriginal.Rows.Count)
    Debug.WriteLine(DTOriginal.Columns.Count)
    [/code]
    would do this.
    I'm not holding my breath that this will point us to the cause, but at least it will be one more thing we can eliminate.

    Ged
  • Bill HBill H Posts: 16Member
    : One little syntax I did notice in the code you posted was that you had:
    : [code]
    : Dim DTCloned As DataTable = DTOriginal
    : 'and not
    :
    : Dim DTCloned As DataTable = DTOriginal.Clone
    : [/code]
    : ie. You left off the ".Clone" method.
    :
    : I'm not saying that this is the cause of the Object Instance error - I think you'll get a different exception - but I do think you probably need to make the edit anyway.
    :
    : Looking back over my (working) example and your (non-working) one, I start to wonder if the key to your problem is the DataTable named DTOriginal. One thing you ould try is to check that DTOriginal has been correctly created and populated. Something like:
    : [code]
    : Debug.WriteLine(DTOriginal.Rows.Count)
    : Debug.WriteLine(DTOriginal.Columns.Count)
    : [/code]
    : would do this.
    : I'm not holding my breath that this will point us to the cause, but at least it will be one more thing we can eliminate.
    :
    : Ged
    :
    I feel bad that you have spent so much time on this...if it becomes a chore instead of a challenge, let me know.
    Two things have happened.....before I put in the debug code it gave me the same error message on the line I missed Clone on.
    Dim DTCloned as DataTable = DTOriginal.Clone
    After that I plugged the Debug code in right after
    Dim DTOriginal As DataTable = ds1.Tables("gentest")
    and I get the same error message on the first line of Debug code
    Object reference not set to an instance of an object, so I tend to agree with you that DTOriginal could very well be the culprit, or at least, what it is being Dim'ed as, unless I plugged the Debug code in the wrong place?
    Bill
  • kainsworthkainsworth Posts: 200Member
    : I feel bad that you have spent so much time on this...if it becomes a chore instead of a challenge, let me know.
    : Two things have happened.....before I put in the debug code it gave me the same error message on the line I missed Clone on.
    : Dim DTCloned as DataTable = DTOriginal.Clone
    : After that I plugged the Debug code in right after
    : Dim DTOriginal As DataTable = ds1.Tables("gentest")
    : and I get the same error message on the first line of Debug code
    : Object reference not set to an instance of an object, so I tend to agree with you that DTOriginal could very well be the culprit, or at least, what it is being Dim'ed as, unless I plugged the Debug code in the wrong place?
    : Bill
    :
    ====================================================================

    If your DataSet/Underlying database has only one Table in it, try this small edit and see if it gets you past the problem line:
    [code]

    ' Change this:
    Dim DTOriginal As DataTable = ds1.Tables("gentest")
    ' to this:
    Dim DTOriginal As DataTable = ds1.Tables(0)
    [/code]
    If your dataset/database has more than one table and "gentest" isn't the first table, then change the zero to the correct number to reflect the table's position in the DataSet.




  • Bill HBill H Posts: 16Member
    : : I feel bad that you have spent so much time on this...if it becomes a chore instead of a challenge, let me know.
    : : Two things have happened.....before I put in the debug code it gave me the same error message on the line I missed Clone on.
    : : Dim DTCloned as DataTable = DTOriginal.Clone
    : : After that I plugged the Debug code in right after
    : : Dim DTOriginal As DataTable = ds1.Tables("gentest")
    : : and I get the same error message on the first line of Debug code
    : : Object reference not set to an instance of an object, so I tend to agree with you that DTOriginal could very well be the culprit, or at least, what it is being Dim'ed as, unless I plugged the Debug code in the wrong place?
    : : Bill
    : :
    : ====================================================================
    :
    : If your DataSet/Underlying database has only one Table in it, try this small edit and see if it gets you past the problem line:
    : [code]
    :
    : ' Change this:
    : Dim DTOriginal As DataTable = ds1.Tables("gentest")
    : ' to this:
    : Dim DTOriginal As DataTable = ds1.Tables(0)
    : [/code]
    : If your dataset/database has more than one table and "gentest" isn't the first table, then change the zero to the correct number to reflect the table's position in the DataSet.
    :
    :
    : I tried the Tables(0) originally and that's when I changed it to the table name, it came up with "Cannot find table 0". gentest is the first table in the database of 3 tables. I also tried (I'm not sure why) changing it to 1 and 2, and it can't find them either. It doesn't give me an error when it is "gentest", the name of the first table in the database. I'm thinking of starting a whole new project just to see if maybe something got skewed along the way with all the changes. The project I'm currently using is one I made up just to get things working before I plugged it into my real project. I,m only using 1 dataset and datagrid per form/table.
    Bill
    :
    :

  • kainsworthkainsworth Posts: 200Member
    : : : I feel bad that you have spent so much time on this...if it becomes a chore instead of a challenge, let me know.

    Don't worry. It won't become a chore. Not promising it won't defeat me, but I like the challenge, so I'm game to keep going as long as you are. :-)


    #####################################################################

    : : I tried the Tables(0) originally and that's when I changed it to the table name, it came up with "Cannot find table 0". gentest is the first table in the database of 3 tables. I also tried (I'm not sure why) changing it to 1 and 2, and it can't find them either. It doesn't give me an error when it is "gentest", the name of the first table in the database. I'm thinking of starting a whole new project just to see if maybe something got skewed along the way with all the changes. The project I'm currently using is one I made up just to get things working before I plugged it into my real project. I,m only using 1 dataset and datagrid per form/table.
    : Bill

    ####################################################################
    Can't imagine why it won't find a Table(0), I thought all DataSets had to have at least one Table, but I'm not enough of an ADO expert to know what the absence of Table(0) means.
    If it was me, the next thing I'd do is swap out the Access database - I mean, use the current code (or one version of it) but change the connection, etc, details and pull data from a completely different database... obviously changing all the necessary parts of the original code. The object of the exercise to see if by some weird chance, it's the database or DataTable itself that's the root of the problem.
    The theory being that the code as I posted it up to you works with a database of mine.

    This is what Optimists call "Logical analysis of the problem" and what realists call "Clutching at straws" ! :-)

    Ged



«1
Sign In or Register to comment.