Delete procedure can only delete one record

thomas008

Well-known member
Joined
Feb 17, 2009
Messages
54
Location
Belgium
Programming Experience
Beginner
I have a datable which contains my data. I display the data in a datagridview. All commands i use are stored procedures. The update and insert commands work fine. The deletecommand works fine when I delete one record and then update to the database. When delete more then one record i get:
VB.NET:
 concurency violated: deletecommand affected 0 of the expected 1 records

Here is the code where i create the parameters and the command
VB.NET:
 myConnection = New OleDbConnection
        myConnection.ConnectionString = myconnectionstring
        myCommand = New OleDbCommand
        myCommand.Connection = myConnection

        myCommand.CommandText = deletecommand
        myCommand.CommandType = CommandType.StoredProcedure

        parameter = New OleDbParameter
        parameter.ParameterName = "inroutingpointID"
        parameter.DbType = DbType.Double
        parameter.SourceColumn = "ID"
        parameter.SourceVersion = DataRowVersion.Original
        myCommand.Parameters.Add(parameter)
        'MessageBox.Show("deletecommand= " & myCommand.CommandText)
        myAdapter.DeleteCommand = myCommand

This is the stored procedure:
VB.NET:
sSQL = "CREATE PROC procRoutingpointDelete(inroutingpointID LONG) AS DELETE * FROM routingpoint WHERE ID = inroutingpointID;"

I tried to change:

VB.NET:
parameter.SourceVersion = DataRowVersion.Original

to:

VB.NET:
parameter.SourceVersion = DataRowVersion.Current

But i still get the same error.

Can anyone help me?
 
Well, you weren't kidding

I took the DB and wrote a whole new project to outline what I'm trying to explain, see the attached and feel free to ask any questions.

As a brief intro:

I dumped the PK on routingpoint, changed the routering to "source" and the goto to "destination" (goto is a reserved word in many programming languages, using it for a column name is not a good idea)
I changed the hhmmss fields to be proper dates, and decided that a date of 30-dec-1899 would be the records that were "time only" (partly because this is what access has decided is a date of 0)

I linked the datatable to VS using the dataset designer and dropped a detailed and a grid version on the form so you can see the data, and also edit and save it

I added some queries to show how you would retrieve the routing info that should be applied at any time, and the rule I decided on was:
SELECT all routing rows where the chosen(current?) time is between datemin and datemax OR the time part of the chosen time is between the times of the default (30-dec-1899) fields, and then just taken the top one row, where the rows are ordered by the datemin in DESCENDING order


This means if you have setup 200->300 between 20:00 and 21:00 every day but you then have a rule that 200 is on holiday for all of march and first day of april (from 01-mar-2009 00:00:00 to 01-apr-2009 23:59:59) so calls are to go to 600, it means that the MOST RECENT DATE is applicable
So the 01 mar 2009 overrides the 30-dec-1899 in the order, and is the one that applies.

Some other presentation logic was added to set the date time picker formats depending on if it was an every day or a specific date.

Have a play


You'll note that nowhere is there any db access code; I got the IDE to write it all for me in about 10 minutes, so i could concentrate on the interesting stuff (the UI and making the app work)
 

Attachments

  • RoutingApp.zip
    47.5 KB · Views: 23
I'm in my third and last year of higher education and i've never seen programming like this. So you can imagine i have a few questions.
First of i wonder why you always use the
VB.NET:
Me.somecode
notation.
The code would work fine without it?
Then the reason i was trying to write all of the code myself is because i want to be able to use the same code for a windows and a web application. I'm not sure if this is transferable if i use wizards?

Thank you very much for your help so far!
 
I'm in my third and last year of higher education and i've never seen programming like this.
I don't envy you :)

First of i wonder why you always use the
VB.NET:
Me.somecode
notation.
The code would work fine without it?
I don't always write Me... and that line of code commented as TODO was put there by the designer. In other places when I wanted to use the designed tableadapter, I just copy/paste the Me.

In practice I do use it when i'm working in a set of inherited classes I made myself, to distinguish between Me (this) and MyBase (the base class) in terms of where I would find the variable (unless it is Shadows, in which case it's important.. I try to avoid Shadows)

Then the reason i was trying to write all of the code myself is because i want to be able to use the same code for a windows and a web application. I'm not sure if this is transferable if i use wizards?
The forms designer is a wizard, and it definitely makes code you cannot transfer to the web. In contrast, the tableadapter code is transferable because it has no UI components. All my services use tableadapters, and they have no GUI

Thank you very much for your help so far!
Welcome
 
I don't envy you :)
When you started programming in VB was your way of coding the same as it is now?
Or did it change completely throughout the years?
I'm wondering because this is my very first project that i have to do all alone
and everything was going pretty smooth untill i reached the point of using
a database.

I also have a few more questions for you:

The updatecommand,insertcommand and deletecommand of the tableadapter are they created behind the scenes by a commandbuilder?

The forms designer is a wizard, and it definitely makes code you cannot transfer to the web. In contrast, the tableadapter code is transferable because it has no UI components. All my services use tableadapters, and they have no GUI

Does this mean i could use it in a webapplication?
Not that i'm going to. I want to understand and adapt your way of thinking.

But i still think i'm going to write all the database accesscode myself.
Is this a bad idea? What i mean is do i get any benefits or downsides with writing it myself?

I also want to say that i believe you are a very patient person and thank you
for being this way!
 
I thought I'd answered this post, but it seems to have gone missing

When you started programming in VB was your way of coding the same as it is now?
I'm not actually a VB programmer. The only time I write VB is in example projects and posts here. I'm a C# programmer but for these purposes it is easier to refer to myself as a .NET programmer, because they are essentially the same language.

Or did it change completely throughout the years?
Not really.. I try to adopt best practices from the start but there are times that I've altered my practice in response to house standards or realisation that there is a better practice than what I've been using. As an example, I've been using parameterized queries since I first learned Java JDBC back in 97, but the naming convention I apply to variables in C# is different to that from java.

The updatecommand,insertcommand and deletecommand of the tableadapter are they created behind the scenes by a commandbuilder?
They are created by something like a command builder, yes


Does this mean i could use it in a webapplication?
The code generated here, datasets and tableadapters can be used anywhere; it's merely standard VB. Nothing magical tied to Windows Forms

But i still think i'm going to write all the database accesscode myself.
Is this a bad idea?
Some people think it's good to do the low level coding, but they tend to make a bad job of it, not follow good coding/oo/sql practices and take 3 weeks to write something that doesnt work, then come moaning to us for help fixing it. The designer lets you concentrate on the important part (writing the SQL), and it arranges everything properly and with good practices, generating massive amounts of code in just a few minutes and best of all; it works.

There's an inherent hypocrisy too, in someone who insists on writing their own database code but then uses e.g. the forms designer to lay out their UI. Why don't they write all the UI code themselves too? Thousands of boring lines of setting proeprties, make a new button, set its text, location, resizing, event handlers etc etc
Take that a step further; if they want to be so hard core, why use Visual Studio with all its Intellisense aids etc at all? Why not just use notepad; after all that's real hard core programming that teaches a person the real amazing detail right? Nah; there are some levels of detail that are hidden because it just isn't worth thinking about them.

If you take a look at the code the dataset generator makes for you, you'll see it's very good. You can gain most of what you'd want to know about the db interaction by reading it, thinking "oh. That's how it works. Cool. Now I'll get on with something interesting"

What i mean is do i get any benefits or downsides with writing it myself?
Benefits; er - you'll know how dataadapters fill datatables, or how queries are run and arranged.. But I see this as logically equivalent to pausing writing your app for a month so you can study how an Intel processor works. Do you really need to know that level of detail? The time would be better invested in learning how to write good SQL queries, not how to write 25 lines of code to drag data from a db into (essentially) a 2D array.
Downsides; as noted, you'll waste a lot of your life writing boring, repetitive code that will likely be a worse job and more broken than what the designer will do, picking up a shed load of bad habits along the way and come the end of it you wont actually know anything quantitatively proportional to the amount of time you put into it

I also want to say that i believe you are a very patient person and thank you
for being this way!
Heh.. I think the jury's still out on whether I'm patient/tolerant. My boss said to me recently: "CJ; working with you would make anyone question their sanity"

This was just after I'd asked a client to define "optional" in the context of 'this CSV field is optional' [it turned out that their "optional" actually meant "mandatory"]..

In another example, when the paper spec (so I couldn't use copy/paste) said 'This field must exactly match the text "Mother's Age" I asked whether the character between the R and S in Mother's was an apostrophe, right single quote or acute accent.. I then had to explain character sets to their developers, and why a computer would treat [Mother's, Mother’s, Mother´s] as inequal. After a round of emails that culminated in their response being "We don't really understand your question" I lost patience, gave up and said "OK, I'll just send my best guess and if it errors we'll change it"
 
Well cjard you have been very helpful! I understand all the code you sent me but now i have a problem recreating it. This sub is the problem
VB.NET:
Private Sub GenericDateTimePickerValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateminDateTimePicker.ValueChanged, DatemaxDateTimePicker.ValueChanged
        If DateminDateTimePicker.Value.Date = New Date(1899, 12, 30) Then
            DateminDateTimePicker.CustomFormat = "HH:mm:ss"
        Else
            DateminDateTimePicker.CustomFormat = "dd MMM yyyy HH:mm:ss"
        End If
        If DatemaxDateTimePicker.Value.Date = New Date(1899, 12, 30) Then
            DatemaxDateTimePicker.CustomFormat = "HH:mm:ss"
        Else
            DatemaxDateTimePicker.CustomFormat = "dd MMM yyyy HH:mm:ss"
        End If
    End Sub
I'm puzzled on how the get the genericdatetimepicker to handle 2 events instead of one? Could you tell me how its done? And does it only work for the same eventtypes?
 
Well you can make any sub (with the right signature) handle any windows controls events simply by writing it in afterwards:

VB.NET:
Public Sub DumbSub(object as Sender, Args as WhateverKindArgs) Handles [B]MyControl1.EventName, MyControl2.EventName[/B]

But what I did was:

Go into designer, click one of the DTPickers, CLick Lightning bolt in the properties window, click in the ValueChanged box and literally (using the keyboard) write the word GenericDateTimePickerValueChanged and press return. Code view appears. Write code. Go back to ui designer, click OTHER DTP, lightning bolt, click ValueChanged, but use the DROPDOWN to select an existing event handler. VB just stacks the handlers up for you in code. If i'd done this for 3 other DTP the sub would have ended up like:

VB.NET:
Private Sub GenericDateTimePickerValueChanged(...) 
Handles 
DateminDateTimePicker.ValueChanged, 
DatemaxDateTimePicker.ValueChanged,
Other1DateTimePicker.ValueChanged,
Other2DateTimePicker.ValueChanged,
Other3DateTimePicker.ValueChanged
 
cjard said:
But what I did was:

Go into designer, click one of the DTPickers, CLick Lightning bolt in the properties window, click in the ValueChanged box and literally (using the keyboard) write the word GenericDateTimePickerValueChanged and press return. Code view appears. Write code. Go back to ui designer, click OTHER DTP, lightning bolt, click ValueChanged, but use the DROPDOWN to select an existing event handler. VB just stacks the handlers up for you in code.
So why didn't you select both controls in the first place, entered "GenericDateTimePickerValueChanged" for the event handler name, and there you had it ? Double work...
 
So why didn't you select both controls in the first place, entered "GenericDateTimePickerValueChanged" for the event handler name, and there you had it ? Double work...

I don't know, maybe I did.. I don't really remember because it's such a trivial part of the app design.
In the answer I gave, I'm trying to highlight that you can slap X more controls on a form and then easily link them to existing methods, but you're entirely correct:

If you have 10 controls on a form and you want to link their events all to one method, select them all and then write one event handler name in..


Welcome to JohnH and cjard's "How to use the Forms Designer 101" class :)
 
Back
Top