Solved

VB.NET Updating data SQL using listview and dataset

Posted on 2013-06-13
9
693 Views
Last Modified: 2013-11-22
Hello,

i am writing a Windows Forms application that allows users to manipulate data in a SQL 2008 database.

I use 3 listboxes and 1 listview on my form to hold the data from various tables in the database. The listboxes and listview are populated using a specific data-adapter that fill a specific dataset.

When I remove a row in the listview, i am searching the dataset for that particular row and I delete the row from the dataset.
After that I use the specific dataadapter again to send the update command to the SQL-server.
Unfortunately the data does not get updated but I do not get any errors.

I am using the following code:

        MsgBox("Really delete PI-tag: " & lstPoints.SelectedItems(0).Text & "?")
        For Each dr As DataRow In dsPoints.Tables(0).Rows
            If dr("tag_name") = lstPoints.SelectedItems(0).Text Then
                dr.Delete()
                dsPoints.AcceptChanges()
                Exit For
            End If
        Next
        For Each lstItem As ListViewItem In lstPoints.SelectedItems
            lstItem.Remove()
        Next
        pointsbuilder.GetDeleteCommand()
        adapPoints.Update(dsPoints, dsPoints.Tables(0).TableName.ToString)

Open in new window


What am I doing wrong? I've searched the internet for hours and hours but am not able to solve it.... any help would be appreciated.
0
Comment
Question by:u795696
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:sachitjain
ID: 39244638
Can you try using dsPoints.Tables(0).Rows.Remove(dr) statement instead of  dr.Delete() statement?

See this link for example of Remove method of DataRowCollection object
http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.remove.aspx

Also make sure that you are able to debug through statements inside your following condition
If dr("tag_name") = lstPoints.SelectedItems(0).Text Then
........


Might be possible for some reason that condition is never holding true
0
 

Author Comment

by:u795696
ID: 39247647
Hi,

I've tried your suggestion but that still does not remove the row from the database. When I refresh my selection the data shows again.
I am sure that the row is found and removed from the dataset because the rows.count property of the dataset show a row less after the remove command.
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 39247699
Can you display the code where your data adapter's updatecommand property is being set?
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Comment

by:u795696
ID: 39252492
In my first post, line 13 if that is what you mean...
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 39252937
No that's your update method call

I mean you need to set updatecommand property for data adapter object. Something like this:
// Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;

You could see following MSDN link for more details
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx
0
 

Author Comment

by:u795696
ID: 39252972
Ohw, that you mean :) Sorry, I'm new with working with databases via .NET

I understood that .NET has a thing called the SQLCommandBuilder that constructs all the necessary commands for you.

I've created an instance of the SQLCommandBuilder just after I filled the data-adapter wich filles the dataset that is used for the listview.

As you see at line 12 of my first post, I request the delete command from that specific SQLCommandbuilder and the next line should update my table...

At least that was what I was hoping for :-)
0
 
LVL 12

Accepted Solution

by:
sachitjain earned 100 total points
ID: 39253000
I guess you need to set updatecommand explicitly. Try doing that and let me know if you face issues.
0
 

Author Comment

by:u795696
ID: 39255552
I have created the following delete command:

        Dim delstring As String
        delstring = "Delete from tbllist_points where group_name='" & lboGroups.Text & _
                    "' and list_name='" & lboList_Area.Text & "' and list_area_name='" & lboArea.Text & "'"
        MsgBox(delstring)
        Try
            Dim ret
            Dim delCMD As SqlCommand = New SqlCommand(delstring, dbCon)
            ret = delCMD.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Open in new window


This works OK, the record gets deleted from the database.

Is this the best way to perform the deletion of a row in the database? It is not using the dataset and the dataadapter in any way.
Can you also create a delete command for the dataadapter and then use the dataadapter to delete the row instead of using the code above?

I do not really care about performance as it is a very small database and only 2 or 3 people are working in it at the same time. It is also not used very often as it is a configuration database for another program.
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 39255560
You could create InsertCommand, UpdateCommand and DeleteCommand for dataadapter object

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.deletecommand.aspx
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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