Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I get an error call "Child list for field Customers cannot be created." after query execution specially after changing my sql queries to parameters instead of sql injection queries , here is the code

//////UPDATES\\\\\\\

Sorry for not providing full info however here are the needed updates: the error appear only if i used the customer query , which is the use type an sql statement to a text box called SQLText.Text and it points to the below line
DataGridViewReports.DataMember = "Customers"


while debugging here is the value of sqlQry = "UPDATE Customers SET ShortCode = '0000' WHERE (ServiceName='Block ALL ADs' AND SupplierName='Block Ads - All Vendors' AND SupplierFeedbackDate='.... / .... / .....' AND Supplier_Feedback='.....');"

to be precise : there is no errors at all in case i used the queries from check boxes

What I have tried:

Private Sub btn_custom_query_Click(sender As Object, e As EventArgs) Handles btn_custom_query.Click
    Try
        If cnnOLEDB.State = ConnectionState.Open Then
            cnnOLEDB.Close()
        End If
        cnnOLEDB.ConnectionString = strConnectionString
        cnnOLEDB.Open()
        Dim sqlQry As String = SQLText.Text
        'Only add "WHERE" if at least one of the boxes is checked
        sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked Or ServiceName.Checked Or DateAdded.Checked Or SupplierFeedbackDate.Checked, " WHERE ", "")
        'Only add the parameter for Supplier if Supplier box is checked
        If SupplierName.Checked Then
            If cbo_Supplier.Text = vbNullString Then
                sqlQry &= "SupplierName IS NOT NULL"
            Else
                sqlQry &= "SupplierName = @cbo_Supplier"
            End If
        End If
        If Supplier_Feedback.Checked Then
            If cbo_Feedback.Text = vbNullString Then
                'Only add "AND" if Supplier box was checked (i.e. we already have a filter)
                sqlQry &= IIf(SupplierName.Checked, " AND ", "")
                sqlQry &= "Supplier_Feedback IS NOT NULL"
            Else
                'Only add "AND" if Supplier box was checked (i.e. we already have a filter)
                sqlQry &= IIf(SupplierName.Checked, " AND ", "")
                sqlQry &= "Supplier_Feedback = @cbo_Feedback"
            End If
        End If
        If Reason.Checked Then
            If cbo_Reason.Text = vbNullString Then
                sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked, " AND ", "")
                sqlQry &= "Reason IS NOT NULL"
            Else
                'Only add " AND " if we have a previous filter in place already
                'I.e. one or both of Supplier and/or Action is checked
                sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked, " AND ", "")
                sqlQry &= "Reason = @cbo_Reason"
            End If
        End If
        If ServiceName.Checked Then
            If TXTServiceName.Text = vbNullString Then
                'I.e. one or both of Supplier and/or Action is checked
                sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked, " AND ", "")
                sqlQry &= "ServiceName IS NOT NULL"
            Else
                'Only add " AND " if we have a previous filter in place already
                'I.e. one or both of Supplier and/or Action is checked
                sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked, " AND ", "")
                sqlQry &= "ServiceName LIKE %@TXTServiceName%"
            End If
        End If
        If DateAdded.Checked Then
            'Only add " AND " if we have a previous filter in place already
            'I.e. one or both of Supplier and/or Action is checked
            sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked Or ServiceName.Checked, " AND ", "")
            sqlQry &= "DateAdded Between @cbo_DateSent And @cbo_DateSentTo"
        End If
        If SupplierFeedbackDate.Checked Then
            'Only add " AND " if we have a previous filter in place already
            'I.e. one or both of Supplier and/or Action is checked
            sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked Or ServiceName.Checked Or DateAdded.Checked, " AND ", "")
            sqlQry &= "SupplierFeedbackDate Between @cbo_FeedbackDate And @cbo_FeedbackDateTo"
        End If
        SQLText.Text = sqlQry

        Dim ds As DataSet = New DataSet
        Dim da As New SqlDataAdapter(sqlQRY, cnnOLEDB)
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

        da.SelectCommand.Parameters.AddWithValue("@cbo_Supplier", cbo_Supplier.Text)
        da.SelectCommand.Parameters.AddWithValue("@cbo_Feedback", cbo_Feedback.Text)
        da.SelectCommand.Parameters.AddWithValue("@cbo_Reason", cbo_Reason.Text)
        da.SelectCommand.Parameters.AddWithValue("@TXTServiceName", TXTServiceName.Text)
        da.SelectCommand.Parameters.AddWithValue("@cbo_DateSent", cbo_DateSent.Value)
        da.SelectCommand.Parameters.AddWithValue("@cbo_DateSentTo", cbo_DateSentTo.Value)
        da.SelectCommand.Parameters.AddWithValue("@cbo_FeedbackDate", cbo_FeedbackDate.Value)
        da.SelectCommand.Parameters.AddWithValue("@cbo_FeedbackDateTo", cbo_FeedbackDateTo.Value)

        da.Fill(ds, "Customers")
        DataGridViewReports.DataSource = ds
        DataGridViewReports.DataMember = "Customers"
        lbl_RowCount.Text = DataGridViewReports.RowCount
    Catch ex As SqlException
        MessageBox.Show(ex.Message)
    End Try
    cnnOLEDB.Close()
End Sub
Posted
Updated 1-Oct-19 2:42am
v4
Comments
phil.o 1-Oct-19 5:15am    
What is the actual value of the variable sqlQry?
BassamKassem 1-Oct-19 7:25am    
question has been updated
ZurdoDev 1-Oct-19 10:42am    
The error is not a sql error but a c# error. You have your dataset setup wrong or are not filling it with the proper sql.
BassamKassem 1-Oct-19 11:37am    
am afraid it is a VB error and it is related to filling the data-grid by the modified records
ZurdoDev 1-Oct-19 11:43am    
Exactly. But everyone is suggesting you take a look at your sql statement when the problem lies in your code.

We can't help you based on that info: we have no idea what the base query you are appending to is, and that's probably relevant.

I'd start by using the debugger: find out what exactly is in sqlQry when you create the SqlDataAdapter, and what exactly is in each parameter you pass over. At a guess - base on the error message text - you aren't retrieving from the right table or your main query is bad.

Then start looking at your data and see what rows it should return.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
BassamKassem 1-Oct-19 7:25am    
Done sir
OriginalGriff 1-Oct-19 7:29am    
And? What did the debugger show you?
BassamKassem 1-Oct-19 7:39am    
IT gave me the same error message "Child list for field Customers cannot be created."

and the variable sqlQry = "UPDATE Customers SET ShortCode = '0000' WHERE (ServiceName='Block ALL ADs' AND SupplierName='Block Ads - All Vendors' AND SupplierFeedbackDate='.... / .... / .....' AND Supplier_Feedback='.....');"
OriginalGriff 1-Oct-19 7:46am    
You are going to have to learn how to use the debugger.
The idea is that you stop your code running before the exception is thrown, and use the debugger to look at your data and find out exactly what it contains - not what you think it contains, not what you started off with: what it actually contains, what string you are actually sending to SQL for example.

Running in the debugger doesn't solve your problem: it lets you gather information to work out how to solve your problem. So use it to find out exactly what is happening, and you / we can work out a solution.
BassamKassem 1-Oct-19 7:58am    
I used it and that's what i noticed

during the debugger , I noticed that when i use a normal SELECT statement it returns no error and worked fine however when I user UPDATE statement it through the mentioned error and couldn't fill the DataGridView by the modified records.
My best guess is...
If you're adding condition to your query, you need to add parameters too:

VB.NET
    SQLText.Text = sqlQry

    Dim ds As DataSet = New DataSet
    Dim da As New SqlDataAdapter(sqlQRY, cnnOLEDB)
    Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

    da.SelectCommand.Parameters.AddWithValue("@cbo_Supplier", cbo_Supplier.Text)
    da.SelectCommand.Parameters.AddWithValue("@cbo_Feedback", cbo_Feedback.Text)
    da.SelectCommand.Parameters.AddWithValue("@cbo_Reason", cbo_Reason.Text)
    da.SelectCommand.Parameters.AddWithValue("@TXTServiceName", TXTServiceName.Text)
    da.SelectCommand.Parameters.AddWithValue("@cbo_DateSent", cbo_DateSent.Value)
    da.SelectCommand.Parameters.AddWithValue("@cbo_DateSentTo", cbo_DateSentTo.Value)
    If SupplierFeedbackDate.Checked Then
        'Only add " AND " if we have a previous filter in place already
        'I.e. one or both of Supplier and/or Action is checked
        sqlQry &= IIf(SupplierName.Checked Or Supplier_Feedback.Checked Or Reason.Checked Or ServiceName.Checked Or DateAdded.Checked, " AND ", "")
        sqlQry &= "SupplierFeedbackDate Between @cbo_FeedbackDate And @cbo_FeedbackDateTo"
    da.SelectCommand.Parameters.AddWithValue("@cbo_FeedbackDate", cbo_FeedbackDate.Value)
    da.SelectCommand.Parameters.AddWithValue("@cbo_FeedbackDateTo", cbo_FeedbackDateTo.Value)
    End If

    da.Fill(ds, "Customers")
    DataGridViewReports.DataSource = ds
    DataGridViewReports.DataMember = "Customers"
    lbl_RowCount.Text = DataGridViewReports.RowCount
Catch ex As SqlException
    MessageBox.Show(ex.Message)
End Try
cnnOLEDB.Close()


I'd strongly suggest to debug the programme and check out what string sqlQry variable holds.
 
Share this answer
 
Comments
BassamKassem 1-Oct-19 7:24am    
Done and question updated including sqlQry outcome
Maciej Los 1-Oct-19 7:31am    
This changes nothing. As you see, i pleaced if statement in a different part of code. All you need to do is to debug your programme.
Good luck!
BassamKassem 1-Oct-19 7:56am    
during the debugger , I noticed that when i use a normal SELECT statement it returns no error and worked fine however when I user UPDATE statement it through the mentioned error and couldn't fill the DataGridView by the modified records.
Maciej Los 1-Oct-19 8:10am    
Because update statement returns the number of rows afected instead of data. To use update statement, you need to call ExecuteNonQuery method. To show data, use SELECT + ExecuteReader().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900