Results 1 to 12 of 12

Thread: Dataset search/filter

  1. #1

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Dataset search/filter

    hello,
    I want to enter records into an sql database but before i do so i would like to load data from the sql database to a dataset and test if the record i want to enter is already in the database. if yes it does not add the record - preventing duplicated records...

    for example: name | grade | subject
    leo | 7A | maths

    i knw how to load the database to a dataset but cant rap my mind around the test to see if the record is already there..
    Last edited by lecfox; Feb 8th, 2012 at 09:54 PM.
    FOX Designs

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Dataset search/filter

    query the columns you want to check using the values you want to add. if it .HasRows > 0 then it means it already exists.

  3. #3

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: Dataset search/filter

    thank u for replying but umm... i dont understand what you mean.. can i see an example?
    FOX Designs

  4. #4
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Dataset search/filter

    No problem.. This is using oracle.

    Code:
    Public Sub saveNewBlockToDB()
            If checkBlockMissedFields(blockDetailsFrm) > 0 Then
                MsgBox("Please fill up the highlighted fields.", MsgBoxStyle.Exclamation)
            Else
                conn.Open()
                Dim cmd, cmd1 As New OracleCommand
    
                cmd.Connection = conn
                cmd.CommandText = "select block_id from block where map_id =" & mainMenuFrm.map_id_label.Text & "and block_name ='" & blockDetailsFrm.blockName_tb.Text & "'"
                cmd.CommandType = CommandType.Text
                cmd.ExecuteReader()
    
                'check if block exist in map
                If cmd.ExecuteReader.HasRows Then
                    MsgBox("Block name is already taken! Please choose another block name.", MsgBoxStyle.Exclamation)
                    conn.Close()
                Else
                    'insert new block
                    cmd1.Connection = conn
                    cmd1.CommandText = "insert into block(block_name, block_x_coord, block_y_coord, main_street, brgy, city, mapper1, mapper2, map_id, user_id ) " &
                                      "values('" & blockDetailsFrm.blockName_tb.Text & "', '" & blockDetailsFrm.Label1.Text & "', '" & blockDetailsFrm.Label2.Text & "', '" & blockDetailsFrm.blockStreet_tb.Text & "', '" & blockDetailsFrm.blockBrgy_tb.Text & "', '" & blockDetailsFrm.blockCity_tb.Text & "', '" & blockDetailsFrm.blockMap1_tb.Text & "', '" & blockDetailsFrm.blockMap2_tb.Text & "', '" & mainMenuFrm.map_id_label.Text & "', '" & mainMenuFrm.user_id_label.Text & "')"
                    cmd1.CommandType = CommandType.Text
                    cmd1.ExecuteReader()
                    conn.Close()
                    blockDetailsFrm.Close()
                    currentLabel.Text = UCase(blockDetailsFrm.blockName_tb.Text)
                    MsgBox("Block successfully added!", MsgBoxStyle.Information)
                End If
            End If
        End Sub
    jmcilhinney will prolly see my code and laugh at my useless a$$ for not using parameters

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataset search/filter

    You certainly should be using parameters although, in this case, that's for completeness and beyond the scope of the actual question. My main issue is that you are over-using ExecuteReader. ExecuteReader should ONLY be used when reading records, which is not happening anywhere here. In this case you should be using ExecuteScalar to get a record count to determine whether there is an existing matching record and then ExecuteNonQuery to insert the data. If you want to follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data you will see examples of both. The ExecuteScalar example uses SUM but that can easily be changed to COUNT.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Dataset search/filter

    oh yeah, i already did that on most of my code.
    is this an acceptable way of checking if a value exists? (disregard my mistakes)

  7. #7

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: Dataset search/filter

    i dont think this code is gonna work for me.. let me be more clear... i did a bulkcopy from excel to a datagrid in my application.. meaning all the data is transferred to the gridview.. i want to now check if any of the data in the grid matches with the database tht i loaded in a dataset.. then it only stores the records tht are not in the database....

    FOX Designs

  8. #8

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: Dataset search/filter

    i did everything up to the part to see if its there already..


    also i think i need some kind or looping...
    FOX Designs

  9. #9
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Dataset search/filter

    yeah it won't.. but that's how i check for duplicate items. just giving you an idea.

  10. #10

    Thread Starter
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: Dataset search/filter

    ok i did some googling and found this..

    vb Code:
    1. 'Create a dataview
    2. dim dv as new dataview
    3.  
    4. 'Associate the dataview to _oDs (Dataset table)
    5. dv.Table = _oDs.Tables("tblCapitalCategory")
    6.  
    7. Dim drv As DataRowView 'Data Row View object to query DataView object
    8.  
    9. 'Filter based on a combo box value selected
    10. dv.RowFilter = "[pkCapitalCategoryID] = " & CInt(cboComboBox.SelectedValue)
    11.  
    12. 'Retrieve my values returned in the result
    13. For Each drv In dv
    14.      _dteDateModified = drv("dteDateModified")
    15. Next

    but i really dont know how to implement this.. plz help
    FOX Designs

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dataset search/filter

    Quote Originally Posted by lecfox View Post
    also i think i need some kind or looping...
    You'll basically have to perform this process for each record. The other option would be to use a stored procedure that does the conditional stuff at the database end, which could be more efficient overall.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Addicted Member
    Join Date
    Nov 2011
    Posts
    177

    Re: Dataset search/filter

    a basic loop:
    Code:
    Dim i As Integer = 0
            For Each row As DataGridViewBand In Me.DataGridView1.Rows
                'use a select clause here instead, if the your select has rows it means its a duplicate.
                MsgBox(Me.DataGridView1.Item(1, i).Value)
                i = i + 1
            Next
    I really can't help you anymore since Im only a novice and I only know oracle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width