SelectedValue problem

praetor

Member
Joined
Feb 10, 2005
Messages
10
Programming Experience
5-10
I'm using a combobox with the property DropDownStyle set to DropDownList. The combo is data-bound to a dataset with two colums: ID, Name and it contains the following records:
ID Name
1 A
2 B
3 C
5 D

The DisplayMember property is mapped to "Name" column and the ValueMember property is mapped to "Id" column.
Everytime I try to set the SelectedValue of the combo to the value 1, the execution crashes showing the following error message:
Specified argument was out of range of valid values. Parameter name: '-2147483648' is not a valid value for 'index'.
The is no problem setting the SelectedValue to 2 or 5 or anything else. I have also a second combo with exactly the same parameters and data-binding on the form, and the is no problem with it.
Any ideas?
 
Last edited:
Hmm I have had problems with bound combo boxes before, but only with negitive numbers.

How are you calling the value? ( 1 , or "1")

TPM
 
Yes they can be quirky. Try inserting a new row at the start, something like this:

VB.NET:
dim r as datarow = yourdatatable.newrow
r.item("ID")=-1
r.item("Name")="-Select-"
yourdataset.rows.insertat(r,0)

You may find the combo1.selectedvalue=1 then works.
 
TPM said:
Yes they can be quirky. Try inserting a new row at the start, something like this:

VB.NET:
dim r as datarow = yourdatatable.newrow
r.item("ID")=-1
r.item("Name")="-Select-"
yourdataset.rows.insertat(r,0)

You may find the combo1.selectedvalue=1 then works.

I was doing basically the same thing, but I was setting r.item("ID") to DBNull.Value. I tried it with -1, but still no success.
This is the whole code:
VB.NET:
	m_SqlCommand.CommandText = "SELECT tbl.Name, tbl.Id FROM tbl;"
	m_DataAdapter.SelectCommand = m_SqlCommand
	m_DataAdapter.Fill(m_DataSetSys, "data")
	row = m_DataSetSys.Tables("data").NewRow()
	row.Item("Id") = DBNull.Value
	row.Item("Nazov") = ""
	m_DataSetSys.Tables("data").Rows.InsertAt(row, 0)
	With Combo1
	  .DataSource = m_DataSetSys.Tables("data")
	  .DisplayMember = "Name"
	  .ValueMember = "Id"
	End With
	Combo1.SelectedValue = 1
 
You don't have a selected index change event for combo1 do you? I've had that problem before. It counts binding as a change of index. Your code looks fine to me...

TPM
 
No, I don't have any event handler for the combo defined. I found out the following: if I delete the row with Id=5 from the database table, everything works fine. If I then add a new row to the table, the problem occurs again no matter what kind of data it contains.
 
Yeah, it's an int. I tried to delete the whole table and recreate it. After I'd inserted the first three rows, everything was fine. After inserting the fourth row, the lousy error came up again.
 
Have you tried moving the selectedvalue=1 to another event? Say an onclick of a button. It might be trying to select the value before it's finished binding... It's a long shot, but you never know.
 
Hmm well I've tried to replicate your problem in one of my projects, and it worked... Go figue.
Looking over your code though I notice a couple of things you could try. When I bind combo boxs I usually use a datatable (ie not a table in a dataset). Also your table name is data, which although not expressly reserved is the name of a main namespace.
These probably won't fix it, but you never know..

Actually something I just thought of, is your ID field a Key?
 
I have a dozen comboboxes bound like this accross the whole project, but this is the only one causing trouble. I tried to use a datatable with no success - here's the code:
VB.NET:
Dim tmp as new DataTable
m_SqlCommand.CommandText = "SELECT tbl.Name, tbl.Id FROM tbl;"
m_DataAdapter.SelectCommand = m_SqlCommand
m_DataAdapter.Fill(tmp)
row = tmp.NewRow()
row.Item("Id") = DBNull.Value
row.Item("Nazov") = ""
tmp.Rows.InsertAt(row, 0)
With Combo1
  .DataSource = tmp
  .DisplayMember = "Name"
  .ValueMember = "Id"
End With
Combo1.SelectedValue = 1
And yeah, the ID field is a primary key.
 
I found also another thing. If I change the statement
tmp.Rows.InsertAt(row, 0)
to
tmp.Rows.InsertAt(row, 1)
everything works fine, i.e. every position is OK except for the topmost one.
 
praetor said:
I found also another thing. If I change the statement
tmp.Rows.InsertAt(row, 0)
to
tmp.Rows.InsertAt(row, 1)
everything works fine, i.e. every position is OK except for the topmost one.

With tmp.Rows.InsertAt(row, 0) is it this row that won't show up? Or the First Data?

I had a similar 'feature' where I had to insert my -select- at the end.
 
Back
Top