|
-
Dec 23rd, 2009, 06:14 AM
#1
Thread Starter
New Member
Problem with combobox binding
I have a problem with a combobox and no page on the Internet has been able to help me so far. So I hope that one of you can.
I have a form (with information about clients) with textboxes binded to a dataview. One of the columns in the dataview contains a number that tells me what kind of client it is. I don't my users want that my users have to fill in a number in a textbox, but I want them to choose a item from a combobox. De selected value should be the value that is actually saved in the database.
This is my code:
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
VerbindingMaken()
cmd.Connection = cnnVraagbaak
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "relatie.proc_OpvragenRelatiesoort"
daRelatie.SelectCommand = cmd
daRelatie.Fill(dsRelatie, "Relatiesoort")
cbo.DataSource = dsRelatie.Tables("Relatiesoort")
cbo.ValueMember = dsRelatie.Tables("Relatiesoort").Columns(0).ToStri ng
cbo.DisplayMember = dsRelatie.Tables("Relatiesoort").Columns(1).ToStri ng
cbo.DataBindings.Add("SelectedValue", dsRelatie.Tables("Relaties"), "Relatiesoort")
De Datasource is the information in my combobox. With Databindings I'm trying to bind it to my dataset (or view, that doesn't work either) with clients. Every time I get a NullReferenceExeption. The moment I remove the line cbo.DataSource = dsRelatie.Tables("Relatiesoort"), I don't get an error, but it doesn't work either.
That dataset is filled (I've checked) and also the derived view. The column Relatiesoort exists.
What do I have to do to make it possible for my users to choose an item from a combobox and save the selected value to the underlying dataset?
-
Dec 23rd, 2009, 07:47 AM
#2
Addicted Member
Re: Problem with combobox binding
Seems to me that you have a error in the third parameter in cbo.databindings.add. You are using the table name.
the line Must be :
cbo.DataBindings.Add("SelectedValue", dsRelatie.Tables("Relaties"), "YourFieldName")
where "YoutFieldName" is the name of your database field that is the source by SelectedValue property
-
Dec 23rd, 2009, 11:47 AM
#3
Thread Starter
New Member
Re: Problem with combobox binding
That's what I did Christian. Relatiesoort is a database field in the table Relaties and should be filled with Relatiesoortnummer from the tabel Relatiesoort.
It's a bit confusing I know.
-
Dec 23rd, 2009, 03:38 PM
#4
Addicted Member
Re: Problem with combobox binding
What is the cbo.SelectedItem Property value after cbo.DataBindings.Add(...) ?
That value must not be Nothing. if the value is nothing, meaning that the the database field Relatiesoort value was not found in the table Relatiesoort
Can you said what instruction raise the NullReference exception ?
-
Dec 24th, 2009, 04:24 AM
#5
Thread Starter
New Member
Re: Problem with combobox binding
The value of cbo.SelectedItem is nothing when the field of the first record in the database table is NULL. When I fill in a value in that field the SelectedItem is {System.Data.DataRowView}
I don't get a NullReference exception anymore, which is strange, 'cause the code hasn't changed. The problem now is that the first record on the form has the SelectedItem of the field of the underlying record, but every record has that same selection in the combobox. I gave the first 4 records different values, but on the form the combobox has the same text for all the four records. Also, it refuses to save at all. Also the other fields on the form won't save changes because of this combobox.
-
Dec 24th, 2009, 07:34 AM
#6
Addicted Member
Re: Problem with combobox binding
you must to check what type of field you are binding. Generally the field value type must be the same that the lookup field type. For instance:
Table 1 Table 2
Id Integer ------> RelatedId Integer
RelatedDescription String
-
Dec 24th, 2009, 01:22 PM
#7
Thread Starter
New Member
Re: Problem with combobox binding
Found it! I filled the combobox with items after I initiated the currency manager.
The only problem I have now is that the dataset won't save anymore. If I change the contents of one of the controls (combobox or otherwise), the values aren't saved in de database.
Any ideas?
Last edited by EllenHeijmans; Dec 24th, 2009 at 01:46 PM.
-
Dec 24th, 2009, 01:47 PM
#8
Thread Starter
New Member
Re: Problem with combobox binding
Ah, solved that one as well. I had the two tables in one dataset. After putting each one in it's own dataset saving wasn't a problem anymore.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|