BindingSource.EndEdit - Not updating underlying data source

D

Dustin Davis

I'm having problems updating a dataset. I call the following method to
add a new record:

Me.drvZone = Me.ZonesBindingSource.AddNew()

Then when I'm done filling in the form, I call:

Me.ZonesBindingSource.EndEdit()
Me.ZonesTableAdapter.Update(Me.DsZones)
Me.DsZones.AcceptChanges()

The problem is, even after this, DsZones is not updated at all. The data
still seems to be in the ZonesBindingSource somehow though. I'm
confused. Can anyone help?

Thanks,
Dustin
 
E

Earl

Try it like this:

Me.ZonesBindingSource.ResetCurrentItem()
Me.ZonesBindingSource.EndEdit()

I'd tell you what ResetCurrentItem() does, but you can read it for yourself
:=)
 
D

Dustin Davis

Darn, still no luck :(

I watching in the debugger and I see the following:

Before AddNew():
DsZones.Tables(0).Rows.Count = 4
ZonesBindingSource.Count = 4

After AddNew():
DsZones.Tables(0).Rows.Count = 4
ZonesBindingSource.Count = 5

After EndEdit(), & Update():
DsZones.Tables(0).Rows.Count = 4
ZonesBindingSource.Count = 5

So somehow I can't figure out how to update DsZones with my binding source.
 
D

Dustin Davis

I'm not sure why, but if I put the following two lines before EndEdit()
it saves the data to the database:

Me.ZonesBindingSource.AddNew()
Me.ZonesBindingSource.CancelEdit()
 
R

RobinS

That's a kludge, and I wouldn't do it just because it works.

Can you check the RowState of the new record and see if
it is, indeed, set as an Insert and not an Update?

Is dsZones a strongly typed dataset that you designed
with the DataSet Designer?

Is the ZonesTableAdapter one that you set up, or did it
come from the DataSet Designer, too? Does it actually
have the update logic for Inserting records built in?

If you change the values in a row and call Update on the
table adapter, do they get updated?

Is the primary key of the table included in the dataset?

Robin S.
-------------------------------------
 
D

Dustin Davis

That's a kludge, and I wouldn't do it just because it works.Right, I was just hoping it might help someone help me :)
Can you check the RowState of the new record and see if
it is, indeed, set as an Insert and not an Update?
Before and after I call the EndEdit, the RowState says "Detached{1}"

Is this a clue?
Is dsZones a strongly typed dataset that you designed
with the DataSet Designer?
Yes it is.
Is the ZonesTableAdapter one that you set up, or did it
come from the DataSet Designer, too?

It came from the DataSet Designer, I think...
Does it actually
have the update logic for Inserting records built in?
Yes, I believe so.
If you change the values in a row and call Update on the
table adapter, do they get updated?
Um, that's another problem I haven't figured out yet. I'm doing this
elsewhere and I tend to get this error often:

Concurrency violation: the UpdateCommand affected 0 of the expected 1
records.
Is the primary key of the table included in the dataset?
Yes it is.
 
R

RobinS

What is me.drvZone? What are you binding the dataset to? Can you post
some more code?

Robin S.
-----------------------------
 
D

Dustin Davis

me.drvZone is a datarowview
Private drvZone As DataRowView

I fill the table adapter on form load
' Fill Zones Table Adapter
Try
frmMain.DbConnect()
Me.ZonesTableAdapter.Connection = frmMain.Cnn
Me.ZonesTableAdapter.Fill(Me.DsZones.zones, ProcessID)
Me.ZonesBindingSource.DataSource = Me.DsZones
Me.bmZones = Me.BindingContext(DsZones, "zones")
Catch ex As Exception
MsgBox(ex.Message)
End Try

Here is the routine that basically does everything that is not working.
The else portion adds the new row, while the if portion should end the
edit and update the database.
 
R

RobinS

What is bmZones?

What is this about?
Me.ZonesTableAdapter.Fill(Me.DsZones.Zones, ProcessID)
When I used strongly typed dataset, the Fill only takes one argument.

Do you have Option Strict on?

I don't understand the use of the BIndingContext. Are you using
..Net 1.1 and VS2003, or .Net 2.0 and VS2005?

Robin S.
----------------------
 
D

Dustin Davis

What is bmZones?bmZones is a BindingManagerBase that I tried and couldn't get to work
either.
What is this about?
Me.ZonesTableAdapter.Fill(Me.DsZones.Zones, ProcessID)
When I used strongly typed dataset, the Fill only takes one argument.
My SQL statement to fill the dataset contains "WHERE process_id =
@process_id" so I have to pass in a process id
Do you have Option Strict on?
Nope

I don't understand the use of the BIndingContext. Are you using
.Net 1.1 and VS2003, or .Net 2.0 and VS2005?
VS2005


I think I may have found a suitable solution that is not so kludgy.

When I'm adding, before the update I call:
Me.DsZones.zones.Rows.Add(drvZone.Row)
Me.ZonesBindingSource.CancelEdit()

Then call the Update method. This seems to work for me.

Thanks for all your help!
 
R

RobinS

Addnew adds the row to the dataset; CancelEdit will cancel any changes
made, so I wouldn't use that, even if it accidentally fixes your
problem.
If you use that repeatedly just because you can't get your stuff to
work, you will pay the price down the road.

If you repost the whole thing (but without the > in front), I'll paste
it into VS and figure out how to fix it.

If you tell me what you're trying to do, I'll post some code that shows
you how to do that.

I suspect you have a form that is bound to your strongly typed dataset
and you are trying to figure out how to add the row to the dataset.

Robin S.
------------------------------
 
D

Dustin Davis

There are active x controls involved so I don't think you can just paste
this in to VS. For what it is worth, here is all the code to this form.
I'm open to criticism on any of it:

Public Class frmZones

#Region "Class Variables"
Friend ProcessID As Integer
Private drvZone As DataRowView
Private drZone As dsZones.zonesRow
Private TemplateFile As String
Private dsIndexFields As DataSet
Private bmZones As BindingManagerBase

' Button text for adding & editing barcode zones
Const SaveButtonText As String = "Save Zone"
Const NewButtonText As String = "New Zone"
Const EditButtonText As String = "Edit Zone"
Const CancelButtonText As String = "Cancel"
#End Region

#Region "Form Events"
Public Sub New(ByVal ProcessID As Integer)
' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Me.ProcessID = ProcessID
End Sub

Private Sub frmZones_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Set list box to owner drawn type
Me.lstZones.DrawMode = DrawMode.OwnerDrawFixed
AddHandler Me.lstZones.DrawItem, AddressOf Me.lstZones_DrawItem

' Fill Zones Table Adapter
Try
frmMain.DbConnect()
Me.ZonesTableAdapter.Connection = frmMain.Cnn
Me.ZonesTableAdapter.Fill(Me.DsZones.zones, ProcessID)
Me.ZonesBindingSource.DataSource = Me.DsZones
Me.bmZones = Me.BindingContext(DsZones, "zones")
Catch ex As Exception
MsgBox(ex.Message)
End Try

' Form settings
Me.Text = Application.ProductName & ": " & Me.Text
Me.WindowState = FormWindowState.Maximized

' Initialize Scanner
Me.IkScan1.ScanInitialize(Handle.ToInt32, 1, 0, "1.0",
"Microfilm Service Corp", "Barcode Ripper", "Barcode Ripper")

' Set display rectangle colors
With Me.IkDisp1
.RectColor1 = Color.Red
.RectColor2 = Color.LightBlue
End With

' Load KF fields
dsIndexFields = frmMain.GetIndexFields(ProcessID)
If Not Me.dsIndexFields Is Nothing Then
Dim dr As DataRow
dr = Me.dsIndexFields.Tables("ProjectDef").NewRow()
dr.Item("fieldnum") = 0
dr.Item("displayname") = "(No Associated Field)"
Me.dsIndexFields.Tables("ProjectDef").Rows.InsertAt(dr, 0)
With Me.cboKfField
.DataSource = Me.dsIndexFields.Tables("ProjectDef")
.ValueMember = "fieldnum"
.DisplayMember = "displayname"
End With
Else
Dim kfc As New Collection
kfc.Add("N/A", 0)
Me.cboKfField.DataBindings.Clear()
Me.cboKfField.DataSource = kfc
Me.cboKfField.SelectedItem = 0
End If

Me.lstZones_SelectedIndexChanged(sender, e)
EnableZoneFields(False)

' Tool tips
ToolTip1.SetToolTip(Me.btnSaveTemplate, "This will save the
currenlty loaded image for this process")

' Load template
TemplateFile = IO.Path.Combine(Application.StartupPath,
"Templates\" & Me.ProcessID & ".tif")
If IO.File.Exists(TemplateFile) Then
Me.LoadImage(TemplateFile)
End If
Me.ZonesTableAdapter.Update(Me.DsZones)
End Sub

Private Sub frmZones_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.IkScan1.ScanTerminate()
frmMain.DbDisconnect()
End Sub
#End Region

#Region "Owner-drawn List Box"
Private Sub lstZones_DrawItem(ByVal sender As Object, ByVal e As
DrawItemEventArgs)
Dim Color As Brush = Brushes.White

' The following method should generally be called before drawing.
' It is actually superfluous here, since the subsequent drawing
' will completely cover the area of interest.
e.DrawBackground()

'The system provides the context
'into which the owner custom-draws the required graphics.
'The context into which to draw is e.graphics.
'The index of the item to be painted is e.Index.
'The painting should be done into the area described by e.Bounds.
If e.Index >= 0 Then
Try
If e.Index < DsZones.Tables(0).Rows.Count AndAlso
DsZones.Tables(0).Rows(e.Index).RowState <> DataRowState.Deleted Then
If e.BackColor.ToString.Contains("Highlight") Then
If Not
IsDBNull(DsZones.Tables(0).Rows(e.Index).Item("split_zone")) AndAlso
DsZones.Tables(0).Rows(e.Index).Item("split_zone") Then
Color = Brushes.DarkRed
Else
Color = Brushes.Black
End If
Else
If Not
IsDBNull(DsZones.Tables(0).Rows(e.Index).Item("split_zone")) AndAlso
DsZones.Tables(0).Rows(e.Index).Item("split_zone") Then
Color = Brushes.Yellow
Else
Color = Brushes.White
End If
End If

e.Graphics.DrawString(DsZones.Tables(0).Rows(e.Index).Item("name").ToString,
lstZones.Font, Color, e.Bounds.X, e.Bounds.Y)
End If
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & e.Index.ToString,
MsgBoxStyle.Information)
End Try
End If
e.DrawFocusRectangle()
End Sub
#End Region

#Region "Template Buttons"
Private Sub btnSaveTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSaveTemplate.Click
If Me.IkDisp1.ImgHandle Then
If Not
IO.Directory.Exists(IO.Path.GetDirectoryName(TemplateFile)) Then
Try

IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(TemplateFile))
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Exit Sub
End Try
End If

Me.IkFile1.ImgHandle = Me.IkDisp1.ImgHandle
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkFile1.FileName = TemplateFile

Me.IkFile1.SaveFile(IMGKIT6Lib.SaveFileConstants.ikSaveTIFFNoncompressed)
End If
End Sub

Private Sub btnDeleteTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnDeleteTemplate.Click
Dim DeleteTemplate As New dlgDeleteTemplate
If DeleteTemplate.ShowDialog = Windows.Forms.DialogResult.OK Then
' Delete the template image
If IO.File.Exists(TemplateFile) Then
Try
IO.File.Delete(TemplateFile)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Exit Sub
End Try
End If
Me.IkDisp1.Display(IMGKIT6Lib.DispModeConstants.ikClear)
Me.IkDisp1.Refresh()
If DeleteTemplate.DeleteZones Then
While Me.ZonesBindingSource.Count > 0
Me.ZonesBindingSource.RemoveCurrent()
Me.ZonesTableAdapter.Update(Me.DsZones)
End While
End If
End If
End Sub

Private Sub btnReloadTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnReloadTemplate.Click
If IO.File.Exists(TemplateFile) Then
Me.LoadImage(TemplateFile)
Else
MsgBox("A saved template image does not exist for this
process.", MsgBoxStyle.Information)
End If
End Sub
#End Region

#Region "Image Buttons & Scanning"
Private Sub btnLoadImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnLoadImage.Click
Me.IkFile1.FilePath =
My.Computer.FileSystem.SpecialDirectories.MyDocuments
If Me.IkFile1.OpenFileDlg() Then
Me.LoadImage(Me.IkFile1.FileName)
End If
End Sub

Private Sub btnScanImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnScanImage.Click
Dim Ret As Boolean
Dim Index(10) As Integer
Try
Me.IkScan1.UiMode =
IMGKIT6Lib.ScanUIModeConstants.ikScanUICLOSE
Ret = IkScan1.ScanExec(Index(0))
IkScan1.ScanTerminate()
Catch ex As Exception
MsgBox("Error scanning document: " & ex.Message,
MsgBoxStyle.Critical)
End Try
End Sub

Private Sub btnSelectScanner_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSelectScanner.Click
Me.IkScan1.ScanSelect()
End Sub

Private Sub btnScanSettings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnScanSettings.Click

End Sub

Private Sub IkScan1_AfterScan(ByVal sender As Object, ByVal e As
AxIMGKIT6Lib._IIkScanEvents_AfterScanEvent) Handles IkScan1.AfterScan
Dim Filename As String

Me.IkFile1.ImgHandle = e.dibHandle
Me.IkCommon1.ImgHandle = IkFile1.ImgHandle
Me.IkCommon1.GetImageType()

Filename = "scan.tif"
IkFile1.FileName = IO.Path.Combine(IO.Path.GetTempPath, Filename)
If Not
IkFile1.SaveFile(IMGKIT6Lib.SaveFileConstants.ikSaveTIFFNoncompressed) Then
MsgBox("Could not save scanned file...")
End If
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkCommon1.ImgHandle = Me.IkFile1.ImgHandle
Me.IkCommon1.GetImageType()
With Me.IkDisp1
.ImgHandle = Me.IkFile1.ImgHandle
.Display(IMGKIT6Lib.DispModeConstants.ikScale)
.ScrollBar = True
.ScrollDir = IMGKIT6Lib.ScrollDirConstants.ikBottomRight

.ScrollDrag = False
.RectDraw = True
End With
End Sub
#End Region

#Region "Image Display"
Private Sub IkDisp1_MouseUpImage(ByVal sender As Object, ByVal e As
AxIMGKIT6Lib._IIkDispEvents_MouseUpImageEvent) Handles IkDisp1.MouseUpImage
If Me.txtZoneName.Enabled Then
With Me.IkDisp1
Me.txtTop.Text = .RectTop
Me.txtLeft.Text = .RectLeft
Me.txtBottom.Text = .RectBottom
Me.txtRight.Text = .RectRight
End With

' Give each text box focus or else it won't save changes
With Me
.txtTop.Focus()
.txtLeft.Focus()
.txtBottom.Focus()
.txtRight.Focus()
End With
Else
Try
With Me.IkDisp1
.RectTop = Me.txtTop.Text
.RectLeft = Me.txtLeft.Text
.RectBottom = Me.txtBottom.Text
.RectRight = Me.txtRight.Text
End With
Catch ex As Exception

End Try
End If
End Sub

Private Function LoadImage(ByVal FileName As String) As Boolean
Try
Me.IkFile1.FileName = FileName
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkCommon1.ImgHandle = Me.IkFile1.ImgHandle
Me.IkCommon1.GetImageType()
With Me.IkDisp1
.ImgHandle = Me.IkFile1.ImgHandle
.Display(IMGKIT6Lib.DispModeConstants.ikScale)
.ScrollBar = True
.ScrollDir = IMGKIT6Lib.ScrollDirConstants.ikBottomRight

.ScrollDrag = False
.RectDraw = True
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Return False
End Try

' Set the first box
IkDisp1_MouseUpImage(Nothing, Nothing)

Return True
End Function
#End Region

#Region "Coordinate Changes"
Private Sub txtTop_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txtTop.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectTop, Me.txtTop)
End Sub

Private Sub txtLeft_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtLeft.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectLeft, Me.txtLeft)
End Sub

Private Sub txtBottom_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtBottom.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectBottom, Me.txtBottom)
End Sub

Private Sub txtRight_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtRight.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectRight, Me.txtRight)
End Sub

Private Sub ChangeCoordinate(ByRef IkCoord As Integer, ByRef
TextBox As Windows.Forms.TextBox)
Try
IkCoord = TextBox.Text
Catch ex As Exception
'MsgBox(ex.Message)
End Try
End Sub
#End Region

#Region "Zones Buttons & Fields"
Private Sub btnNewSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnNewSave.Click
If Me.btnNewSave.Text = frmZones.SaveButtonText Then
If Not IsNumeric(Me.txtTop.Text) OrElse Not
IsNumeric(Me.txtLeft.Text) OrElse Not IsNumeric(Me.txtBottom.Text)
OrElse Not IsNumeric(Me.txtRight.Text) Then
MsgBox("Coordinates are missing or invalid. Please
select highlight the barcode zone, or enter numeric coordinates
manually.", MsgBoxStyle.Information)
Exit Sub
ElseIf txtZoneName.Text.Length = 0 Then
MsgBox("You must give this zone a name before saving
it.", MsgBoxStyle.Information)
Me.txtZoneName.Focus()
Exit Sub
Else
Try
If Not drvZone Is Nothing Then
Me.DsZones.zones.Rows.Add(drvZone.Row)
Me.ZonesBindingSource.CancelEdit()
Else
Me.ZonesBindingSource.EndEdit()
End If

Me.ZonesTableAdapter.Update(Me.DsZones.zones)
Me.DsZones.AcceptChanges()
' Debug purposes
' Me.ZonesTableAdapter.Fill(DsZones.zones, ProcessID)
Catch ex As Exception
MsgBox("Error Saving Changes: " & ex.Message,
MsgBoxStyle.Critical)
End Try
End If
Me.EnableZoneFields(False)
Else
Try
Me.drvZone = Me.ZonesBindingSource.AddNew()

drvZone.Item("processes_id") = ProcessID
drvZone.Item("split_zone") = 0
drvZone.Item("kf_field_id") = 0

Me.EnableZoneFields()
Catch ex As Exception
MsgBox("Error adding new zone: " & ex.Message,
MsgBoxStyle.Exclamation)
End Try
End If
End Sub

Private Sub btnEditCancel_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEditCancel.Click
If Me.btnEditCancel.Text = frmZones.EditButtonText Then
Me.EnableZoneFields()
Else
Me.EnableZoneFields(False)
Me.ZonesBindingSource.CancelEdit()
End If
Me.drvZone = Nothing
End Sub

Private Sub lstZones_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstZones.SelectedIndexChanged
If Me.lstZones.SelectedIndex >= 0 And Me.lstZones.SelectedIndex
< bmZones.Count Then
bmZones.Position = Me.lstZones.SelectedIndex
End If
'If Me.lstZones.SelectedIndex >= 0 Then
' Try
' Me.txtTop.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("top").ToString
' Me.txtLeft.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("left").ToString
' Me.txtBottom.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("bottom").ToString
' Me.txtRight.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("right").ToString
' Me.txtZoneName.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("name").ToString
' If
IsDBNull(CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("kf_field_id")) Then
' Me.cboKfField.Text = "N/A"
' Else
' Me.cboKfField.SelectedValue =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("kf_field_id")
' End If
' Catch ex As Exception
' MsgBox("Error updating fields: " & ex.Message,
MsgBoxStyle.Exclamation)
' End Try
'End If
End Sub

Private Sub EnableZoneFields(Optional ByVal Enable As Boolean = True)
Try
Me.btnEditCancel.Enabled = True
Me.lstZones.Enabled = Not Enable
Me.txtTop.Enabled = Enable
Me.txtLeft.Enabled = Enable
Me.txtBottom.Enabled = Enable
Me.txtRight.Enabled = Enable
Me.txtZoneName.Enabled = Enable

If Me.dsIndexFields Is Nothing Then
Me.cboKfField.Enabled = False
Else
Me.cboKfField.Enabled = Enable
End If


If Enable Then
Me.btnNewSave.Text = frmZones.SaveButtonText
Me.btnEditCancel.Text = frmZones.CancelButtonText
Else
Me.btnNewSave.Text = frmZones.NewButtonText
Me.btnEditCancel.Text = frmZones.EditButtonText

' If an existing zone is not selected then we can't edit it
If Me.lstZones.Items.Count = 0 Then
Me.btnEditCancel.Enabled = False
End If
End If
Catch ex As Exception
MsgBox("Error enabling fields: " & ex.Message,
MsgBoxStyle.Exclamation)
End Try
End Sub
#End Region

#Region "Right-click Menu Actions"
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Delete.Click
If MsgBox("Are you sure you want to delete the selected zone?",
MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Try
Me.ZonesBindingSource.RemoveCurrent()
Me.ZonesTableAdapter.Update(Me.DsZones)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End If
End Sub

Private Sub TestZoneToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TestZoneToolStripMenuItem.Click
Dim Barcode As SoftekBarcode
Dim nBarCode As Short
Dim strBarcode As String

Barcode = New SoftekBarcode()

frmMain.SetBCPrefs(Me.ProcessID, Barcode)
' Set area to scan
Barcode.SetScanRect(Me.txtLeft.Text, Me.txtTop.Text,
Me.txtRight.Text, Me.txtBottom.Text, 0)

nBarCode = Barcode.ScanBarCode(Me.IkFile1.FileName)

' Or read the barcode from an image in a PictureBox
' bm = New Bitmap(Me.PictureBox1.Image)
' nBarCode = barcode.ScanBarCodeFromBitmap(bm.GetHbitmap())

If (nBarCode < 0) Then
MsgBox("Error reading barcode")
ElseIf (nBarCode = 0) Then
MsgBox("No barcode found on this image")
Else
strBarcode = Barcode.GetBarString(1)
MsgBox("Barcode Found: " & strBarcode)
End If
End Sub

Private Sub
SplitMultipageTIFsWithThisZoneToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SplitMultipageTIFsWithThisZoneToolStripMenuItem.Click
For i As Integer = 0 To DsZones.Tables(0).Rows.Count - 1
Me.drvZone = Me.ZonesBindingSource.Item(i)
If i = Me.lstZones.SelectedIndex Then
drvZone.Item("split_zone") = 1
Else
drvZone.Item("split_zone") = 0
End If
Next

Try
Me.ZonesBindingSource.EndEdit()
Me.ZonesTableAdapter.Update(Me.DsZones)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try

Me.lstZones.Refresh()
End Sub
#End Region

End Class
 
R

RobinS

You're very brave to post your code. ;-)

**VB Options**

Do you have Option Strict on? This should be set in Visual Studio as
a default for everything you do. You can set this as your default in
Tools/Options/Projects and Solutions/VB Defaults.

You can set it for one project by double-clicking on My Project
and going to the Compile tab. There are dropdowns about halfway
down the screen.

You *REALLY* need to do this. This will make some things really clear,
as I have noted below in your code. Otherwise, you are relying on
the compiler to convert your stuff, and if it does it wrong, it
will take you *forever* to figure out what's wrong.

**Other Info**

You don't need all the instances of "Me." unless there
are two of the object and you need to discern between them (like
the same object is defined and used in the base class maybe).

You don't need the BindingContext, and I don't even know what
a BindingManagerBase is. You should be able to do what you need
to do with the BindingSource.

**Strongly Typed Datasets**

The beauty of strongly typed datasets is that they open up a bunch
of shortcuts for you to help you code, and to improve performance.

Instead of dsZones.Tables(0), you can access the Zones
table directly using dsZones.Zones.

Debug.Print "Row Count = " & dsZones.Zones.Count

You can get a row of the table like this:

Dim rw as ZonesDataSet.ZonesRow = _
CType(dsZones.Zones.Rows(e.Index), ZonesDataSet.ZonesRow)

And then use all of the capabilities of the DataRow. For example:

dsZones.Tables(0).Row(e.Index) can be replaced by rw

Then you can access
rw.RowState
rw.Split_Zone --> this gives you the [split_zone] column of that row
rw.IsSplit_ZoneNull --> this tells you if that columns is null
rw.SetSplit_ZoneNull --> this will set that column to null

This would save you a lot of typing, and maybe make it easier to read
your lstZones_DrawItem routine.

rw.RowState

Accessing columns as Item("columnname") is inefficient. If you
use a strongly typed dataset, it defines pointers to the columns
that are defined as datacolumn, and this is much faster:

Debug.Print(rw.Split_Zone.ToString)

**Info?**

Also, I'm not sure yet that I have enough information. Here are
my assumptions about your design; feel free to correct me.

Your strongly typed dataset is called ZonesDataSet when defined,
and dsZones is an instance of that, probably added to the form
at design time.

You have textboxes for coordinates that they can fill in,
and then they have to specify a zone name for those coordinates,
or they can select an existing zone. If they fill in the
coordinates and specify a zone name, you are going to add
it to the Zones dataset and listbox.

The top/left/bottom/right fields are in the zones table.

I have marked up your code, and will e-mail it back to you.
Search for double single-quotes to find my changes/comments.

Basically the problem is your add isn't right, nor is your
edit. You need to use the BindingSource to handle all of this
stuff, and it will automatically update your dataset when you
call EndEdit. Those changes will be saved to the database when
you call the table adapter's Update method.

When you add, you call myBindingSource.AddNew, which creates
a new DataRowView that you can access by using
myBindingSource.Current().
Then you can change the fields in the DRV, putting in your data,
call EndEdit to save the changes to the dataset, then call
the table adapter's update method to save the changes to the
database.

For your change method, you are not moving the data back
into the record at all. Maybe those textboxes are databound?
(I hope not.)

Anyway, hope it helps. Let me know if you have any q's.
For everyone else, I'm not posting the code here because
it's so long, but if you want to see it, ask and I will.

Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top