Results 1 to 7 of 7

Thread: [RESOLVED] System should not insert selected record/s if it's already existing in the database.

  1. #1

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Resolved [RESOLVED] System should not insert selected record/s if it's already existing in the database.

    The system should not insert selected records from my datagridviewcheckboxcolumn if it's already existed in the database.

    It's like when you're registering online. If you're username/email address already registered,it would inform you that youre username/email is already registered and the site would ask you to change your username/email to new ones.

    The difference in my system is that i have 2 datagridviewcheckbox columns not just textboxes . Parent and Child in the datagridview. What i want to happen is that once the Code has been registered as Parent it cant be registered again as Child anymore.

    I dont have a logic on that so far, i hope some could help me out here, but here's my code so far.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
    
                connectionString = ";"
                sql_connection = New SqlConnection(connectionString)
                Dim cmdText As String = "INSERT INTO Conso_Holder (CONSO_PARENT_STOCKHOLD_CODE, CONSO_CHILD_STOCKHOLD_CODE, CONSO_STOCK_NAME) VALUES (@CONSO_PARENT_STOCKHOLD_CODE, @CONSO_CHILD_STOCKHOLD_CODE, @CONSO_STOCK_NAME)"
                Dim cmdTextDelChild As String = "DELETE Conso_Holder WHERE CONSO_CHILD_STOCKHOLD_CODE = @CONSO_CHILD_STOCKHOLD_CODE"
                Dim command As New SqlCommand(cmdText, sql_connection)
                Dim command2 As New SqlCommand(cmdTextDelChild, sql_connection)
                Dim parentCode As String = ""
    
    
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    If CBool(DataGridView1.Rows(i).Cells(0).Value) = True Then
                        parentCode = DataGridView1.Rows(i).Cells(2).Value.ToString
    
                        Exit For
                    End If
                Next i
    
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
    
                    If CBool(DataGridView1.Rows(i).Cells(1).Value) = True Then
    
                        command2.Parameters.AddWithValue("@CONSO_CHILD_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
                        sql_connection.Open()
                        command2.ExecuteNonQuery()
                        sql_connection.Close()
                        command2.Parameters.Clear()
                    End If
                Next i
    
    
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
    
                    sql_connection.Open()
    
                    Dim slcString As String = " SELECT CONSO_PARENT_STOCKHOLD_CODE FROM Conso_Holder Where  CONSO_CHILD_STOCKHOLD_CODE = @CONSO_CHILD_STOCKHOLD_CODE "
    
                    Dim command4 As New SqlCommand(slcString, sql_connection)
    
                    'command4.Parameters.AddWithValue("@CONSO_PARENT_STOCKHOLD_CODE", parentCode)
                    command4.Parameters.AddWithValue("@CONSO_CHILD_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
    
                    datareadersql = command4.ExecuteReader
    
                    'If CInt(datareadersql.HasRows) = 0 Then
                    '    sql_connection.Close()
    
    
                    '    MsgBox("Records Inserted")
    
                    '    Exit For
    
                    'ElseIf CInt(datareadersql.HasRows) <> 0 Then
    
                    '    MsgBox("Records can't be inserted")
    
                    '    Exit For
    
    
                    'End If
    
    
                    sql_connection.Open()
                    Dim result As String = command4.ExecuteScalar.ToString
                    sql_connection.Close()
                    command4.Parameters.Clear()
                Next i
    
    
    
    
    
    
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    If CBool(DataGridView1.Rows(i).Cells(1).Value) = True Then
                        command.Parameters.AddWithValue("@CONSO_PARENT_STOCKHOLD_CODE", parentCode)
                        command.Parameters.AddWithValue("@CONSO_CHILD_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
                        command.Parameters.AddWithValue("@CONSO_STOCK_NAME", DataGridView1.Rows(i).Cells(3).Value)
    
                        sql_connection.Open()
                        command.ExecuteNonQuery()
                        sql_connection.Close()
                        command.Parameters.Clear()
                    End If
    
                Next i
    
    
    
    
    
                Dim hasParentCode As Boolean = False
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    If CBool(DataGridView1.Rows(i).Cells(0).Value) = True Then
                        hasParentCode = True
                        Exit For
                    End If
                Next
                If hasParentCode = False Then
                    MsgBox("Select a Parent Code first.", vbInformation, "STO Masterfile System")
                End If
    
    
                Dim hasChildCode As Boolean = False
                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    If CBool(DataGridView1.Rows(i).Cells(1).Value) = True Then
                        hasChildCode = True
                        Exit For
                    End If
                Next
                If hasChildCode = False Then
                    MsgBox("Select atleast one Child Code first.", vbInformation, "STO Masterfile System")
                End If
    
            Catch ex As Exception
                MsgBox(ex.Message)
    
            End Try
            itemlist.Clear()
    
    
        End Sub
    I tried this but still not wrking.

    Code:
      For i As Integer = 0 To DataGridView1.Rows.Count - 1
    
                    sql_connection.Open()
    
                    Dim slcString As String = " SELECT CONSO_PARENT_STOCKHOLD_CODE FROM Conso_Holder Where  CONSO_CHILD_STOCKHOLD_CODE = @CONSO_CHILD_STOCKHOLD_CODE "
    
                    Dim command4 As New SqlCommand(slcString, sql_connection)
    
                    'command4.Parameters.AddWithValue("@CONSO_PARENT_STOCKHOLD_CODE", parentCode)
                    command4.Parameters.AddWithValue("@CONSO_CHILD_STOCKHOLD_CODE", DataGridView1.Rows(i).Cells(2).Value)
    
                    datareadersql = command4.ExecuteReader
    
                    'If CInt(datareadersql.HasRows) = 0 Then
                    '    sql_connection.Close()
    
    
                    '    MsgBox("Records Inserted")
    
                    '    Exit For
    
                    'ElseIf CInt(datareadersql.HasRows) <> 0 Then
    
                    '    MsgBox("Records can't be inserted")
    
                    '    Exit For
    
    
                    'End If
    
    
                    sql_connection.Open()
                    Dim result As String = command4.ExecuteScalar.ToString
                    sql_connection.Close()
                    command4.Parameters.Clear()
                Next i
    Please review my code

    Thanks
    We live and learn

    -Jm

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: System should not insert selected record/s if it's already existing in the databa

    The logic is quite simple, you need to determine what constitutes as a row being "equal" to another row. Once you have this, there are a variety of ways to test if a row exists using your criteria. I personally would use a Stored Procedure, but you seem to be just creating all the commands in code. That's fine.

    So what I would do if I were you is create a SqlCommand object that is just a simple SELECT. The SQL of this SELECT command would have the logic to select the possibly hypothetical row that would be equal. I would then call ExecuteNonQuery on the command. This will return how many rows were affected by the query. If the result of this method call is > 0, then the record already exists.

    Your best bet is to start in the data base to create the SQL that you will use in code. If it doesn't work in the data base it isn't going to work in code. If it doesn't work in code, check if it works in the data base.

  3. #3

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Re: System should not insert selected record/s if it's already existing in the databa

    Quote Originally Posted by ForumAccount View Post
    The logic is quite simple, you need to determine what constitutes as a row being "equal" to another row. Once you have this, there are a variety of ways to test if a row exists using your criteria. I personally would use a Stored Procedure, but you seem to be just creating all the commands in code. That's fine.

    So what I would do if I were you is create a SqlCommand object that is just a simple SELECT. The SQL of this SELECT command would have the logic to select the possibly hypothetical row that would be equal. I would then call ExecuteNonQuery on the command. This will return how many rows were affected by the query. If the result of this method call is > 0, then the record already exists.

    Your best bet is to start in the data base to create the SQL that you will use in code. If it doesn't work in the data base it isn't going to work in code. If it doesn't work in code, check if it works in the data base.

    Thanks for your suggestion Will be working on it right now..
    We live and learn

    -Jm

  4. #4

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Re: System should not insert selected record/s if it's already existing in the databa

    Quote Originally Posted by ForumAccount View Post
    The logic is quite simple, you need to determine what constitutes as a row being "equal" to another row. Once you have this, there are a variety of ways to test if a row exists using your criteria. I personally would use a Stored Procedure, but you seem to be just creating all the commands in code. That's fine.

    So what I would do if I were you is create a SqlCommand object that is just a simple SELECT. The SQL of this SELECT command would have the logic to select the possibly hypothetical row that would be equal. I would then call ExecuteNonQuery on the command. This will return how many rows were affected by the query. If the result of this method call is > 0, then the record already exists.

    Your best bet is to start in the data base to create the SQL that you will use in code. If it doesn't work in the data base it isn't going to work in code. If it doesn't work in code, check if it works in the data base.

    I think I have the correct select statement. What's next sir?
    We live and learn

    -Jm

  5. #5

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Re: System should not insert selected record/s if it's already existing in the databa

    Help me. Im stocked T_T dont know what to do next T_T
    We live and learn

    -Jm

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] System should not insert selected record/s if it's already existing in

    Is this resolved? I already mentioned the next step, it would be to call ExecuteNonQuery and see how many rows are affected.

  7. #7

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Re: [RESOLVED] System should not insert selected record/s if it's already existing in

    Quote Originally Posted by ForumAccount View Post
    Is this resolved? I already mentioned the next step, it would be to call ExecuteNonQuery and see how many rows are affected.
    Yes. This has been resolved. Thank you!
    We live and learn

    -Jm

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