Home All Groups Group Topic Archive Search About

Extra Row Added to Table

Author
25 Apr 2007 5:34 PM
Ken
Whenever I run this code, I get a row added to my table in the primary field
whose text is "System.Object".  This code is intended to simply add a single
row to a single table.  Anybody see why this is happening and what I need to
do to stop the extra row?

    Private Sub btnDeleteOrder_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteOrder.Click
        Dim vbResponse As Integer
        vbResponse = MessageBox.Show("Are you sure that you want to delete "
+ tbOrd.Text + "?", "Confirm Delete", MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning)
        If vbResponse = 1 Then
            Dim strSQL As String
            Dim strDel As String

            strDel = tbOrd.Text.ToString()
            strSQL = "SELECT * FROM Orders WHERE Order = @Order"

            Dim da As New SqlDataAdapter(strSQL, cn)
            da.SelectCommand.Parameters.AddWithValue("@Order", strDel)

            Dim tbl As New DataTable("Orders")
            tbl.Columns.Add("Order", GetType(String))
            tbl.Columns.Add("UOM", GetType(String))

            da.Fill(tbl)

            Dim rowToDelete As DataRow
            rowToDelete = tbl.Rows(0)
            rowToDelete.Delete()

            Try
                da.Update(tbl)
                MessageBox.Show("Order Deleted", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                Console.WriteLine("Call to SqlDataAdapter.Update " & _
                "threw exception: {0}", ex.Message)
            End Try
        End If
    End Sub

Thanks,
Ken

Author
25 Apr 2007 8:48 PM
Ken
Sorry, I posted the wrong code.  My problem is with this code.

       Dim strNewOrder, strSQL As String
        Dim rowToInsert As DataRow
        Dim tbl As New DataTable("Orders")

        If tbOrd.Text = "" Then
            MessageBox.Show("Please enter a name for the Order.", "Invalid
Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        tbl.Columns.Add("Order", GetType(String))
        tbl.Columns.Add("ID", GetType(String))

        strNewOrder = tbOrd.Text
        strSQL = "INSERT INTO Orders " & _
                        "  (Order) VALUES " & _
                        "  (@Order)"

        Dim da As New SqlDataAdapter(strSQL, cn)
        da.SelectCommand.Parameters.AddWithValue("Order", strNewOrder)
        da.Fill(tbl)

        cn.Open()

        rowToInsert = tbl.Rows.Add(New Object(), strNewOrder)

        Dim cmdInsert As New SqlCommand(strSQL, cn)
        cmdInsert.Parameters.AddWithValue("@Order", rowToInsert("Order"))

        Try
            cmdInsert.ExecuteNonQuery()
            rowToInsert.AcceptChanges()
            MessageBox.Show("New Order Saved", "Successful Submission",
MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        btnSubmitNewRow.Enabled = True
        btnSubmitNewRow.Visible = True
        tbOrder.Focus()
        Call DisableTxtBxs()

        cn.Close()
    End Sub

AddThis Social Bookmark Button