Link to home
Start Free TrialLog in
Avatar of ljhodgett
ljhodgett

asked on

delete row from datatable in vb.net 2005

Hi,

I have created a dataset and datatable in code using: -

            Dim row As DataRow = ds.Tables("RigsToAdd").NewRow
            row("SSDNumber_Description") = cmbRigs.Text
            row("TestRigID") = cmbRigs.SelectedValue.ToString
            ds.Tables("RigsToAdd").Rows.Add(row)

Now that I have done this I am trying to find out how I delete a row please?

Many Thanks
Lee
Avatar of Sethi
Sethi
Flag of India image

Simply use the Remove method.
ds.Tables("RigsToAdd").Rows.Remove(0)
The above code will remove the 1st row.
Avatar of ljhodgett
ljhodgett

ASKER

Hi,

it underlines the 0 and gives the error message: -

Error      1      Value of type 'Integer' cannot be converted to 'System.Data.DataRow'.      C:\Documents and Settings\lh181365\My Documents\Visual Studio 2005\Projects\WindowsApplication10\WindowsApplication10\frmAddProduct.vb      151      44      Spares_Control

regards
Lee
objDataTable.Rows.Remove(objDataTable.Rows(0))
Hi,

objdatatable is not something I have. The entire dataset is performed by code and not via the design wizards \ connections. Please find my entire code below.

Best regards
Lee

Public Class frmAddProduct
    Dim sql As String
    Dim Count_Position As Long
    Dim ds As DataSet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo error_Handler

        Dim ds As New DataSet

        Sql = "Select * from Customers"
        dbOpen()
        Dim da As New SqlClient.SqlDataAdapter(Sql, oConn)
        da.Fill(ds, "Customers")
        DataGridView1.DataSource = ds.Tables("Customers")

error_Handler:
        If Err.Number <> 0 Then
            MsgBox("An error has occured while connecting to the SQL Server. Error no: " & Err.Number & vbCrLf & vbCrLf & "Description:" & vbCrLf & Err.Description)
            dbClose()
            End
        End If

        dbClose()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        On Error GoTo error_Handler

        dbOpen()
        sql = "insert into dbo.customers values (newid(), '2', '2')"
        Dim command As New SqlClient.SqlCommand(sql, oConn)
        command.ExecuteNonQuery()


error_Handler:
        If Err.Number <> 0 Then
            MsgBox("An error has occured while connecting to the SQL Server. Error no: " & Err.Number & vbCrLf & vbCrLf & "Description:" & vbCrLf & Err.Description)
            dbClose()
            End
        End If

        MsgBox("Success")
        dbClose()


    End Sub


    Private Sub frmAddProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds = New DataSet
        Dim dt As DataTable = New DataTable("RigsToAdd")
        dt.Columns.Add("SSDNumber_Description", GetType(String))
        dt.Columns.Add("TestRigID", GetType(String))
        ds.Tables.Add(dt)

        'On Error GoTo error_Handler

        'Dim ds As New DataSet

        'sql = "SET NOCOUNT ON; SELECT TestRigID,SSDNumber, Description,CalibrationDue,bUserLock,DocNum,JobReference,RoutingStage,bPCBTester,bDriveTester, bObsolete, Identity SSDNumber + Description as SSDNumber_Description FROM Admin_SSDNumber WHERE bLogicalDelete=0 ORDER BY SSDNumber;"

        sql = "SELECT TestRigID," & _
                "SSDNumber," & _
                "Description," & _
                "CalibrationDue," & _
                "bUserLock,DocNum," & _
                "JobReference," & _
                "RoutingStage," & _
                "bPCBTester," & _
                "bDriveTester," & _
                "bObsolete," & _
                "PartIdentity, " & _
                "CASE WHEN PartIdentity IS NULL THEN " & _
                    "cast(SSDNumber as varchar) + ' ' + Description " & _
                "ELSE " & _
                    "cast(PartIdentity as varchar) + ' ' + Description " & _
                "End " & _
                "as SSDNumber_Description " & _
                "FROM Admin_SSDNumber WHERE bLogicalDelete=0 ORDER BY SSDNumber "

        dbOpen2()
        Dim da As New SqlClient.SqlDataAdapter(sql, oConn)
        da.Fill(ds, "Rigs") ' populate data set "da" with the data returned from sql command "ds" and set dataset name to "Rigs"
        DataGridView1.DataSource = ds.Tables("Rigs")

        cmbRigs.DisplayMember = "SSDNumber_Description"
        cmbRigs.ValueMember = "TestRigID"
        cmbRigs.DataSource = ds.Tables("Rigs")

error_Handler:
        If Err.Number <> 0 Then
            MsgBox("An error has occured while connecting to the SQL Server. Error no: " & Err.Number & vbCrLf & vbCrLf & "Description:" & vbCrLf & Err.Description)
            dbClose()
            End
        End If

        dbClose2()

    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        Dim found As Boolean

        found = False

        Count_Position = 0
        lstRigs.SelectedItem = 0
        Do Until Count_Position > lstRigs.Items.Count
            If lstRigs.Text = cmbRigs.Text Then
                found = True
            Else
                found = False
            End If
            lstRigs.SelectedItem = Count_Position + 1
            Count_Position = Count_Position + 1
        Loop

        If found = True Then
            MsgBox("Item already assigned to where used list", MsgBoxStyle.Exclamation, "An Input Error Has Occured")
            Exit Sub
        Else
            MsgBox("Not Found")
            Dim row As DataRow = ds.Tables("RigsToAdd").NewRow
            row("SSDNumber_Description") = cmbRigs.Text
            row("TestRigID") = cmbRigs.SelectedValue.ToString
            ds.Tables("RigsToAdd").Rows.Add(row)

            DataGridView1.DataSource = ds.Tables("RigsToAdd")

            lstRigs.DisplayMember = "SSDNumber_Description"
            lstRigs.ValueMember = "TestRigID"
            lstRigs.DataSource = ds.Tables("RigsToAdd")
        End If


    End Sub

    Private Sub lstRigs_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstRigs.DoubleClick
        MsgBox(lstRigs.SelectedValue.ToString)
    End Sub


    Private Sub cmbRigs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbRigs.SelectedIndexChanged
        MsgBox("'" & cmbRigs.Text & "'")
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click

        ds.Tables("RigsToAdd").Rows.Remove(0)


    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of Sethi
Sethi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial