?
Solved

delete row from datatable in vb.net 2005

Posted on 2007-08-01
6
Medium Priority
?
4,913 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:ljhodgett
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 19610002
Simply use the Remove method.
ds.Tables("RigsToAdd").Rows.Remove(0)
0
 
LVL 18

Expert Comment

by:Sethi
ID: 19610005
The above code will remove the 1st row.
0
 

Author Comment

by:ljhodgett
ID: 19610085
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Sethi
ID: 19610129
objDataTable.Rows.Remove(objDataTable.Rows(0))
0
 

Author Comment

by:ljhodgett
ID: 19610167
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
0
 
LVL 18

Accepted Solution

by:
Sethi earned 2000 total points
ID: 19610192
ds.Tables("RigsToAdd").Rows.Remove(ds.Tables("RigsToAdd").Rows(0))
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question