Addnew with combx and txtbx sync'd

frankm9639

Active member
Joined
Aug 24, 2006
Messages
25
Programming Experience
1-3
I display records using a combox and textbox. I want to clear both when I call addnew(), and then I can enter in a new record into the combox field and the textbox field. But, it only clears my textbox, it does not clear my combobox. Am I doing this right?

Combobox Datasource is set to Company table, Displaymember is set to CompanyKey, ValueMember is set to CompanyKey.

Textbox.Text is set to CompanyName.

Here is the code:
------------------------------------------------
Private Sub Form1_Load
SqlDataAdapter1.Fill(DataSet11.Company)
bmbVendors = Me.BindingContext(DataSet11, "Company")
End Sub

Private Sub buttonAdd_Click
bmbVendors.AddNew()
End Sub
------------------------------------------------
 
It's a drop-down list. I has just these bindings:
Combobox Datasource is set to Company table, Displaymember is set to CompanyKey, ValueMember is set to CompanyKey.

I want to be able to clear everything, enter in a new record, and then refresh. This would mean I have to enter in a company key into the combobox. Don't know if that's good programming practice. Can you tell how to do this? Or if in the real world it's done a different way?
 
It's a drop-down list. I has just these bindings:
Combobox Datasource is set to Company table, Displaymember is set to CompanyKey, ValueMember is set to CompanyKey.

You need also to bind SelectedValue to e.g. MyMainDataTable.CompanyKey


e.g. suppoise we have a Person table, and a Salutations table (1=Mr, 2=Mrs, 3=Miss, 4=Dr, 5=Rev)

Our Combo has bindings:
.DataSource = Salutations
.DisplayMember = "Salutation_Text"
.ValueMember = "Salutation_ID"
.SelectedValue binding is PersonDatatable.SalutationID


I want to be able to clear everything, enter in a new record, and then refresh.
You dont usually need the refresh part. Think about it; that's like closing and reopening a Word document every time you save it..


Don't know if that's good programming practice. Can you tell how to do this? Or if in the real world it's done a different way?
Normally, to blank out the box, we do .SelectedIndex = -1
Arg81 (forum member) is having some difficulty making the combo show the current value of a particular new record. I havent so far experienced it, but he says that, for his (salutation field) being null, the combo will still show something, and when he saves, even though it looks like it should be saving e.g. Mr, it actually saves nothing.. Maybe if you run into this, let me know
 
I added your line and it clears now. I click on the Add button but it doesn't save anything (this is the entire code). Funny, I must be missing something, I click on the button, it clears it, and then I type in stuff, but, I don't do anything to "save" what I just entered. Should I clear the boxes, type in stuff, and then click the add button?

PrivateSub buttonAdd_Click...
bmbVendors.AddNew()
EndSub


If I then try to select another key such as 'PA', I get this error:

An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll
Additional information: Column 'CompanyKey' is constrained to be unique. Value 'PA ' is already present.

Any ideas?
 
it sounds like you have either:

bound .SelectedValue to the Company lookup table (bind it to the OTHER table, the one with the data in it)

or:

made the CompanyKey column in the data table, to be unique

I have attached a demo. Take a look at it. Most notably, click on the Combo box on the form and look at all the properties in BOLD - they have been changed from defaults. Expand Data Bindings section too...
 

Attachments

  • LookupDataDemo.zip
    25.9 KB · Views: 25
Last edited:
it sounds like you have either:
bound .SelectedValue to the Company lookup table (bind it to the OTHER table, the one with the data in it)
or: made the CompanyKey column in the data table, to be unique
I have attached a demo. Take a look at it. Most notably, click on the Combo box on the form and look at all the properties in BOLD - they have been changed from defaults. Expand Data Bindings section too...

I'm having trouble opening it, but I'm working on it. It says the project file is missing the "VisualStudioProject". I did make the CompanyKey unique as there can only be one company such as "PA". Is this incorrect programming?

Here is my data scheme:
Key Name
PA Pacific Co
AA Atlantic Co
IA Indian Co

I want to be able to select from the list, edit the list, or delete from the list using a combobox for selection, with the company name displayed in a textbox.
 
I'm using VB.Net 2003. Are you? I can't open any designer windows, only code. If you tell me the name of the combo box I can look at the code itself. oops, it's called companyIDcombobox, nevermind.
 
oh nuts.. i failed to notice youre on .NET 1.1, the demo I posted is 2.0, it wont work for you. Sorry

I also cant really advice on 1.1 stuff because it doesnt do things the way we do them in 2.0 (no bindingsource) - not wanting to advise you incorrectly, I think someone skilled in 1.1 needs to come along at this point.. Reading the DW1 link in my signature may help somewhat
 
Check this out: I added a textbox for the CompanyKey. Then, I use the CompanyKey combobox for selecting. When I click add, I use the CompanyKey textbox. Now, everything works.

Course, I still gotta figure out how to use the combobox for both selecting and adding, and delete the redundant textbox....

(edit) I just talked to a VB6 guy who said you can't use a combobox for adding a new record in VB6. Now I'm wondering if you also cannot use the combobox this way in 1.1. Also, all of my books, including "ADO.NET" do not show an example of this.

Anyone know?
 
Last edited:
I dont see any reason why a combo cannot be used, but youre making a confusing logic here:

First you say you want to use the combo to select the company, then you say you want to use it to add a company.. Well for sure these two concepts are exclusive.. There's no way you can have a combo do both at the same time.. why?

Because when you pick an item from the list, the computer has to somehow decide between:

Do you want to navigate to the record denoted by that item?
Do you want to set a field on the current record, to the value that you picked?

I can make a combo that does one or the other at any particular time.

To make a combo that navigates, I set its datasource to the BindingSource (in 1.1 you dont have this, a sort of equivalent is the bindingcontext) and its display member to the field I want to see (company name). I set no bindings that alter data, like bindign the .Text or .SelectedValue properties
The combo navigates the data set, and for a table of 5 rows, whichever row is picked, becomes the current row (on the BindingSource)

If I want a combo that edits, I must at least bind the .Text property (if it is a DropDown combo) or the .SelectedValue (if it is a DropDownList incapable of accepting freetext, or if it is facading an entry i.e. showing me the Name but storing the ID) to the BindingSource.WhicheverField i want to update
In situations like these the combo typically gets its values from elsewhere (a lookup table of discrete [display,value] values)

We dont generally use lookup combos for things like company names, because each company name shouldnt appear that many times, unless we are talking about a limited set of suppliers of goods..
i.e. I have a sales order system, every company that buys something, goes in. I DO NOT make a lookup table of every persopn who has ever ordered off me (cause there will be thousands) but I might make a lookup table of my suppliers (there will only ever be a handful of them)


To have one combo do both selecting and adding, you would have to, in runtime, bind and unbind it from the data source. I think you need to thionk more clearly about what youre trying to achieve.. When you have a firm idea, let me know in detail and I can help.

You can, seeing as VB2005 (2.0) is free, consider upgrading to that. The upgrade cost from 2003 is also very reasonable..

And lastly, please dont think that, because VB6 and VB.NET share a few keywords, that they are analogous, or that you can take VB6 advice and apply it to .NET; in most cases you cannot because VBN is NOT an evolution of VB6 - its a whole new language that reuses an established syntax in order to garner market appeal.. :D
 
Sounds like this exercise is not a good exercise on how to learn programming. Thanks for this last post. It clears up the issue significantly. When I first wanted to do this, I thought, "Hey, why not use a combobox instead of previous/next buttons". Then, I ran into trouble with addnew. Funny, there were no examples I could find on how to do this. :)

What would you consider the next evolution from the previous/next buttons for looking at records?
 
What would you consider the next evolution from the previous/next buttons for looking at records?

It depends entirely on how many records and how they are arranged, we are talking about

If there a will only ever be less than 20 records, and space is tight, use a ComboBox

If you can afford the space, or you have up to a few hundred rows, use a Listox

If the records are hierarchical, use a TreeView

If there are millions of rows, that will be narrowed to X rows via search (0 <= x <= millions) provide a search, dump the results to a grid on a separate screen, and picking a result from the grid shows that record on the main screen


Incidentally, I still use prev/next buttons! One custoemr can have many addresses. I use them to step through all his addresses he ever had

-

For example:

When windows shuts down, you get a combo of what to do (restart, standby, hibernate etc)
Low number of choices -> combo (though I hate that Standby and ShutDown both start with S, cant use keyboard nav)

I wrote a program for reporting in this business. Reports are defined and there wil never be more than 40. A listbox is used. Picking the report from the list shows the parameters you must fill, and then you run it

The new version of this program groups reports by department, client, or internal/external. I now use a treeview

I wrote a program to manage Direct Debit submissions for potentially millions of custoemrs. One of the mandates was that "every field of every form can be used in a search for entities for that form". I scan the form, build a query from the terms I find, run it and show the results with a load of detail, in a grid on a popup window. I dont show all the data, but enough detail to allow the user to determine which result is what they want. DOubleclicking a row causes it to load in full detail in the original window. The search form is minimised so they can restor and pick another if they chose wrongly.
 
Back
Top