• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

bound combo controls

This ? is directed to An expert using bound controls.  I have a dataset which is filled once when the application loads.  On each form i use dataview objects to filter it down to one record.  The problems i am having are data bindings to checkbox, combobox, and textbox.  When i make a change to any of these controls i need the new value to take place immediately becuase these controls are tied to lots of validation code and they enable and disable certain controls values.  For example on a combo box i fill them using a data table that i create which reads parts into the boxes along with a sequence value from a table called parts.  On form load i bind the the control using the code below

 Public Sub LoadSBCombo()
        LoadPartCombo(myStock, "stock_building", "seq ASC")
        comStockBuildings.DataSource = myStock
        comStockBuildings.DisplayMember = "display"
        comStockBuildings.ValueMember = "value"

        myTable("stock_bldg_choice") = cNull(myTable("stock_bldg_choice"), "Integer")
        comStockBuildings.DataBindings.Clear()
        comStockBuildings.DataBindings.Add("SelectedValue", myQuoteDv, "stock_bldg_choice")
    End Sub

And on the event of this combo box is where i refresh the value by doing this...

 Private Sub comStockBuildings_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comStockBuildings.SelectedValueChanged
        If formLoaded And stockFlag Then
             myTable("stock_bldg_choice") = CType(comStockBuildings.SelectedValue, Integer) <== this always doesn't work integer to dataview row exception thrown
             StockBuildingSpecified()
        End If
    End Sub
 
The reason for doing this is because before i inserted that line the combobox would just keep the same selected value and i had no idea why i though the bound value would change.  I need a true bound controls experts advice because this is driving me nuts!
0
rbirnesser
Asked:
rbirnesser
  • 37
  • 21
1 Solution
 
rbirnesserAuthor Commented:
i want to use databindings because when a quote is viewed i want the values in the database to be initial selected value but then when the value is changed i need it to refresh itself because of like 3000 lines of validation code or so and i know there has got to be a easier way because i get alot of exceptions using this method
0
 
RonaldBiemansCommented:
Hi rbirnesser, Do you get this error immidiatly when you open the form ?
0
 
rbirnesserAuthor Commented:
I was hoping you would answer ronald :) and no only when i trigger the event because the event is prevented from executing on form load by using a boolean flag formLoaded!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RonaldBiemansCommented:
ok, could you try this

myTable("stock_bldg_choice") = directcast(comStockBuildings.SelectedValue, datarowview).item("value")
0
 
RonaldBiemansCommented:
also why are you doing this

'first  you set the members
        comStockBuildings.DataSource = myStock
        comStockBuildings.DisplayMember = "display"
        comStockBuildings.ValueMember = "value"


        myTable("stock_bldg_choice") = cNull(myTable("stock_bldg_choice"), "Integer")

'then you clear them add something else ??????
        comStockBuildings.DataBindings.Clear()
        comStockBuildings.DataBindings.Add("SelectedValue", myQuoteDv, "stock_bldg_choice")
0
 
rbirnesserAuthor Commented:
b/c i used that to refresh them at one time and it seemed to work but it was causing duplicate bindings so i used the clear() and im goona try that code now
0
 
rbirnesserAuthor Commented:
specified cast is not valid :(
0
 
rbirnesserAuthor Commented:
myTable is declared at form level

Dim myIndex As New Integer
Dim myTable As New Object
myIndex = BindingContext(myDs.quotes).Position
myTable = myQuoteDv.Item(myIndex)
0
 
rbirnesserAuthor Commented:
Dim myStock As New DataTable("stock_buildings") is also declared at formLevel and here is the code for LoadPartsCombo which is global in a module file

 Public Sub LoadPartCombo(ByVal myDt As DataTable, ByVal myCat As String, ByVal mySort As String)
        If myDt.Columns.Count > 0 Then
            myDt.Columns.Clear()
            myDt.Rows.Clear()
        End If

        myDt.Columns.Add("value", GetType(Integer))
        myDt.Columns.Add("display", GetType(String))
        Dim myRow As DataRowView
        Dim row As DataRow = myDt.NewRow

        Dim myPartsDv As New DataView(myDs.parts)
        myPartsDv.RowFilter = "category = '" & myCat & "' AND unavailable = False"
        myPartsDv.Sort = mySort

        For Each myRow In myPartsDv
            Dim item_name As String = cNull(myRow("item_name"), "String")
            If item_name <> String.Empty Then
                Dim newRow As DataRow = myDt.NewRow
                newRow.BeginEdit()
                newRow("value") = myRow("seq")
                newRow("display") = myRow("item_name")
                newRow.EndEdit()
                myDt.Rows.Add(newRow)
            End If
        Next
    End Sub
0
 
RonaldBiemansCommented:
I'm a
0
 
RonaldBiemansCommented:
I'm a bit confused :-)

with what do you want to fill the combobox

with mystock or with myQuoteDv

0
 
rbirnesserAuthor Commented:
myStock is a datatable declared at formlevel which i load using the LoadPartCombo which looks the seq numbers up in the parts table and filters display with the name of the stock building and the seq is the value associated with that item.  Which is stored in myQuoteDv which is bound to the quotes table.  And the column its stored in is called stock_bldg_choice.
0
 
rbirnesserAuthor Commented:
im just stumped on why a combo box bound to that field doesn't update its selected value automatically when choosen from the combo
0
 
rbirnesserAuthor Commented:
i used the data table solution instead of arrays
0
 
RonaldBiemansCommented:
what does cnull do ?

in

myTable("stock_bldg_choice") = cNull(myTable("stock_bldg_choice"), "Integer")
0
 
rbirnesserAuthor Commented:
I migrating this application to .net from a program called servoy.  The exisiting database is a mysql database which i am using the crlab mysql direct.net plugin to communicate with it.  The existing database allowed null values. so i wrote the cnull function to check for them and replace them with a certain value that is not null b/c .net is not very null value friendly.  cNull(object i want to check, type of object) so in that case if stock_bldg_choice was null it would return a 0
0
 
rbirnesserAuthor Commented:
ronald u come up with anything ?
0
 
rbirnesserAuthor Commented:
i think the problem is the databindings thats why i was storing the selected values directly to the dataview and that might be part of the issue too
0
 
rbirnesserAuthor Commented:
i sat down and thought about my logic and im gonna try a different approach tomorrow with editable cb's, combo's, and text i am gonna not use databindings! i think the data view has issues with my bindings
0
 
RonaldBiemansCommented:
I'm still working on your problem :-), I'm still trying to recreate your problem, but for now i'm still unable to.

when you put a breakpoint here

myTable("stock_bldg_choice") = CType(comStockBuildings.SelectedValue, Integer)

and if you then select comStockBuildings.SelectedValue right click and use quickwatch, what do you get.



0
 
rbirnesserAuthor Commented:
i get out of scope
0
 
RonaldBiemansCommented:
?????? That shouldn't be possible because this is in the event of the comStockBuildings. are you sure you are not checking this value from the catch of a try catch block
0
 
rbirnesserAuthor Commented:
nope
0
 
rbirnesserAuthor Commented:
if i insert a msg box after that line the value is correct like 847 or 848 and it refreshes itself just sometimes here and there it gives me the datarow integer crap
0
 
RonaldBiemansCommented:
HMMM, Your project is probably to big to mail it, is it, I hate leaving this without getting it solved :-)
0
 
rbirnesserAuthor Commented:
i haven't had the error in a while
and no my project is not that huge yet i could mail it to u
0
 
RonaldBiemansCommented:
Could you mail it to ronald dot biemans at compagnie dot be
0
 
rbirnesserAuthor Commented:
ok sent ronald biemans :)
0
 
rbirnesserAuthor Commented:
did you get the project ronald ?
0
 
RonaldBiemansCommented:
No,not yet :-)
0
 
rbirnesserAuthor Commented:
ronald.biemans@compagnie.be
0
 
rbirnesserAuthor Commented:
correct ?
0
 
rbirnesserAuthor Commented:
the project was under 3 megs zipped
0
 
RonaldBiemansCommented:
Yep, but I would have liked it not to be spelled out because spambots will pick this up :-)

Being the administrator I have no limitations on the amount I can receive :-) LoL
0
 
rbirnesserAuthor Commented:
im sorry ronald :(
0
 
rbirnesserAuthor Commented:
damn spam bots lol
0
 
RonaldBiemansCommented:
No worries rbirnesser :-)
0
 
rbirnesserAuthor Commented:
did u get it yet ? do u want me to resend it
0
 
RonaldBiemansCommented:
No sorry, I didn't get it yet, weird
0
 
rbirnesserAuthor Commented:
i sent it again
0
 
RonaldBiemansCommented:
There isn't by any change a .exe file in this zip, because our virus software will delete it instantly
0
 
rbirnesserAuthor Commented:
yes do u want me to delete the bin file
0
 
rbirnesserAuthor Commented:
sent again no .exe files just the project files itself
0
 
rbirnesserAuthor Commented:
ronald did you ever get the project ?
0
 
RonaldBiemansCommented:
HMMM, no I'm at a client today, could you send it there

Info at strategus dot be
0
 
rbirnesserAuthor Commented:
k i will send u a updated copy
0
 
RonaldBiemansCommented:
I got it, I'll have a look at it today :-)
0
 
rbirnesserAuthor Commented:
ronald i kinda think that problem went away i got the buildings tab on frmNewQuote to work rather well.  I do have another ? if u still interested in the points let me know one that might be a lil bit easier for ya too since u are the ado .net master :)  The category is sending updates
0
 
RonaldBiemansCommented:
Fire away :-)
0
 
rbirnesserAuthor Commented:
ok i have a customers table and a quotes table these are the only two tables that need to be updated.  The customers update works fine but this application is gonna have muliple users at the same time making new customers and quotes.  The customer id is pulled from a table called db_admin which has the next avaible customer id for making a new customer.  I have a global dataset that is filled at the beginning when the program is loaded if one person creates a new customer the next customer id that i have pulled at the beginning.  My ? is does the data adapter automatically refill itself if there were any changes since the initial fill if not how do i go about doing this?  Also i have a issue when i update the quotes table from frmNewQuote then go back out and back into that customer sometimes the dataview acts wierd is this normal ?  All my current update code is in the project i sent u also when to send this updates i would think only to send them when the application is exited but im not sure if thats right thanks ronald biemans :)
0
 
RonaldBiemansCommented:
Hi rbirnesser,

Just some thoughts and assumptions here, I suspect that the customerID is your primary key and that the quotes table is related by that number.

Good database design requires that the primary doesn't have a meaning and that it is a sort of autonumber field (and in the most optimum setup isn't even know to the user, although this is almost never the case).

But to answer your question the dataadapter won't automatically refresh your data, so if another user add a record it won't show up in your dataset until you refill it (this is because .net is a disconnected model), so you could refresh it at an sertain interval (not recommended), or add a refresh button so the user can load the latest data.

I was just wondering how you handle this next available customerID, what if to user add a record at the same time won't you have a duplicate key ?



0
 
rbirnesserAuthor Commented:
database design is not my fault im trying to make someone elses problems into something productive.  The customer ID is incremented by code and then reassigned to that one field in the db_admin table.  Is there anyway we can refill and just get the modified records and new ones and not refill the ones that didn't change
0
 
RonaldBiemansCommented:
If you use a sql database you could add a timestamp field to your table, this will set the time the record was last updated automatically (added/editted) you could than check this timestamp to get the changed records. If on the otherhand you use an access database then you would have to add a datetime field to your table and update that field yourself every time a record was edited

Still about the customerid, how does your program handle multiple adds at the same time by different users, how will it keep a unique id.
0
 
rbirnesserAuthor Commented:
its increments it by one.  my database is a mysql database.  and about the unique id the programmer who orginally wrote it didn't really take that into considertation and it just crashes the server when a customer makes a new customer at the same time
0
 
RonaldBiemansCommented:
I thought so, you are not going to like to hear this but a redesign of your database would be the best way, I think most of the problems you experience  is because of the poor database design (and hit the one who made the design in the first place, Lol), it seems that if you update an Identity Crisis occurs which would explain the dataview problems you experience.

so my advice would be to make the customerid an autonumber field (maybe even a GUID Field), in your programm set the autoincrementseed, and autoincrementstep of the primarykey to -1, (this will ensure that you won't get an identity crisis)

and update in this order

parent (modified/ new)
child (modified/new/deleted)
parent (deleted)

also, resolving update conflicts,

don't rely on the default behaviour of the dataadpter object for managing update conflicts

Why the default behaviour of the dataadpter object leaves the database in an inconsistent state if an update failed, because ADO.net throws an exception in the application but doesn't restore the original state of the database.

so either use dataadapter.continueUpdateOnerrors = true or wrap the update in a transaction and rollback the transaction if any conflict occurs)

0
 
rbirnesserAuthor Commented:
i knew it ronald biemans i knew it more like im gonna castrate the guy who designed it with a broken whiskey bottle
0
 
rbirnesserAuthor Commented:
also the enoumouse amount of null values is also a issue, and the quotes talbe having 467 columns to doesn't help
0
 
rbirnesserAuthor Commented:
hey ronald i think the customers table just needs a new primary key, mysql has a autonumber field but i don't see anything about a GUID field, the quotes table needs to be broken down into multiple tables and maybe a one to many relationship by the primary key would be feasable.  If you could take a look at the schema for those two tables and tell me what yout think i will close this ? b/c u have helped more then enough thank you :) also we are goin through the rest of the tables and ridding of all the null values and not allowing them to be written
0

Featured Post

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!

  • 37
  • 21
Tackle projects and never again get stuck behind a technical roadblock.
Join Now