473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Add a blank item in a data binded combo, how?

Kay
Hi all,

Could you tell me the best way to add a blank item(as first item) in a data
binded combo box? Because I think I didn't do it right and it generate an
error if the second item(after the top blank item) is selected by using
Combo.SelectedValue :

"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.

Could you guys please comment? Here's my code:

/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection

myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()

theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")

tblTable = dsDataSet.Tables("Table")

''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****

dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////

Thanks!

Kay
Feb 6 '06 #1
9 4425
You forgot to set the row's KeyField column to 0. It looks like that
the new data row's KeyField column is set to a int.MinValue which is
-2147483648.

- NuTcAsE

Feb 7 '06 #2
Kay
Hi NutcAsE & all,

Thanks for your advice! However, I've tried adding the KeyField without
success:

myRow = tblTable.NewRow
myRow("DisplayField") = ""
myRow("KeyField") = 0 '**** tried 999 & "0"
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****** error triggered here
dsDataSet = Nothing
tblTable = Nothing
myCCMSConn.Close()
gbProcessing = False
With the new line of code, it trigger the same error in THIS procedure, and
it's actually the line the set the selected item - which is the same as the
scenario I have before (i.e. where I have combo.SelectedValue = xx <---
second item).

Is it a .net bug? Or there is a way to get around it? Your advice would be
very appreciated!!

Thanks!

Kay
"NuTcAsE" <ra********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You forgot to set the row's KeyField column to 0. It looks like that
the new data row's KeyField column is set to a int.MinValue which is
-2147483648.

- NuTcAsE

Feb 7 '06 #3
Hi,

"Kay" <kk@micxsoft.com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
Hi NutcAsE & all,

Thanks for your advice! However, I've tried adding the KeyField without
success:

myRow = tblTable.NewRow
myRow("DisplayField") = ""
myRow("KeyField") = 0 '**** tried 999 & "0"
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****** error triggered here
dsDataSet = Nothing
tblTable = Nothing
myCCMSConn.Close()
gbProcessing = False
With the new line of code, it trigger the same error in THIS procedure,
and it's actually the line the set the selected item - which is the same
as the scenario I have before (i.e. where I have combo.SelectedValue = xx
<--- second item).

Is it a .net bug?
Yes. When you bind a DataTable to a ComboBox then internally a DataView is
used. The DataView crashes when it's not sorted and you used InsertAt on
the linked DataTable.
Or there is a way to get around it?
Not that i know. Maybe you can get away with not using InsertAt (Add
instead) and creating an explicit DataView and sorting it so that the empty
row is on top.
Your advice would be very appreciated!!
I don't have a good workaround, i just wanted to let you know there is a
bug.

HTH
Greetings

Thanks!

Kay
"NuTcAsE" <ra********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
You forgot to set the row's KeyField column to 0. It looks like that
the new data row's KeyField column is set to a int.MinValue which is
-2147483648.

- NuTcAsE


Feb 7 '06 #4
Actually the data table is being bound after the InsertAt function, so
the resulting data view should contain the added data row. Weird part
is it is working perfectly for me. I am guessing that there is
something wrong with the data being returned in the data table. Key,
could you take a look at the data being returned from your fill
operation and check if any Null values or suspicious data is being
returned?

Also are you using 1.1 and if yes have you applied the SP1 for .net
1.1?

- NuTcAsE

Feb 7 '06 #5
Hi,

"NuTcAsE" <ra********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Actually the data table is being bound after the InsertAt function, so
the resulting data view should contain the added data row.
True. But it's not that the row isn't added, but that there is a bug when
you use InsertAt with an unsorted DataView (also when InsertAt is called
before creating the DataView).

I too can't reproduce the problem with a simple ComboBox and SelectedValue.
The example below doesn't prove that the bug is the OP's problem, but it
does show that there is a bug with DataView and DataTable.InsertAt. I'm
sure there are other situations where it fails too, causing different
Exceptions or bad data:

DataTable dt = new DataTable("test");
dt.Columns.Add("desc", typeof(string));
dt.Rows.Add(new object[] { "A" });
dt.Rows.Add(new object[] { "B" });
dt.AcceptChanges();

DataRow dr = dt.NewRow();
dr["desc"] = "empty";
dt.Rows.InsertAt(dr, 0);

DataView dv = new DataView(dt);
//dv[1]["desc"] = "somethingelse"; // enumerating dv shows an additional row
dv[0]["desc"] = "somethingelse"; // enumerating dv crashes

// enumerating DataView
foreach( DataRowView drv in dv )
Console.WriteLine( drv["desc"] );

This happens on NET1.1SP2.

Greetings

Weird part
is it is working perfectly for me. I am guessing that there is
something wrong with the data being returned in the data table. Key,
could you take a look at the data being returned from your fill
operation and check if any Null values or suspicious data is being
returned?

Also are you using 1.1 and if yes have you applied the SP1 for .net
1.1?

- NuTcAsE

Feb 7 '06 #6
You're right... there are reports out there with simmilar bugs using
InsertAt and databinding. Funny part is I can't find any offical
notification from Microsoft that this bug exists.
Thanks,

- NuTcAsE

Feb 7 '06 #7
Kay, another option is to pass a Union statement in your SQL.

SELECT 0 AS KeyField, Null AS DisplayField
UNION
SELECT KeyField, DisplayField FROM MyTable
ORDER BY DisplayField

and then use
ListBox.Items.FindByText(DefaultText).Selected = True
or
ListBox.Items.FindByValue(0).Selected = True
to find the info you want displayed.

"Kay" wrote:
Hi all,

Could you tell me the best way to add a blank item(as first item) in a data
binded combo box? Because I think I didn't do it right and it generate an
error if the second item(after the top blank item) is selected by using
Combo.SelectedValue :

"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.

Could you guys please comment? Here's my code:

/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection

myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()

theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")

tblTable = dsDataSet.Tables("Table")

''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****

dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////

Thanks!

Kay

Feb 7 '06 #8
Kay
Hi all,

Now I modified the code and bind the combo box with a Dataview, it seems the
error is gone!!! :D
NuTsAsE I have 1.1 SP1 install so I think the error may related to binding
the combo with data table. Ok here's the update of my code, comments are
welcome!

///////////////////

theAdapter = New SqlDataAdapter(pSQL, myCCMSConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")
tblTable = dsDataSet.Tables("Table")

Dim DV As DataView = dsDataSet.Tables("Table").DefaultView
'*****

''Put a blank row at the top
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)
DV.Sort = "DisplayField" '*****

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = DV '*****
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0
///////////////////

Kay

"Kay" <kk@micxsoft.com> wrote in message
news:eA*************@TK2MSFTNGP09.phx.gbl...
Hi all,

Could you tell me the best way to add a blank item(as first item) in a
data binded combo box? Because I think I didn't do it right and it
generate an error if the second item(after the top blank item) is selected
by using Combo.SelectedValue :

"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.

Could you guys please comment? Here's my code:

/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection

myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()

theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")

tblTable = dsDataSet.Tables("Table")

''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****

dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////

Thanks!

Kay

Feb 8 '06 #9
Hi,

"Kay" <kk@micxsoft.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi all,

Now I modified the code and bind the combo box with a Dataview, it seems
the error is gone!!! :D
Even when you bind to a DataTable, the DataTable's DefaultView is used, so i
doubt that what's helping but it's the sorting that probely helps, as i
mentioned in my previous post the InsertAt-bug doesn't appear when the
DataView is sorted.

Greetings

NuTsAsE I have 1.1 SP1 install so I think the error may related to binding
the combo with data table. Ok here's the update of my code, comments are
welcome!

///////////////////

theAdapter = New SqlDataAdapter(pSQL, myCCMSConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")
tblTable = dsDataSet.Tables("Table")

Dim DV As DataView = dsDataSet.Tables("Table").DefaultView
'*****

''Put a blank row at the top
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)
DV.Sort = "DisplayField" '*****

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = DV '*****
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0
///////////////////

Kay

"Kay" <kk@micxsoft.com> wrote in message
news:eA*************@TK2MSFTNGP09.phx.gbl...
Hi all,

Could you tell me the best way to add a blank item(as first item) in a
data binded combo box? Because I think I didn't do it right and it
generate an error if the second item(after the top blank item) is
selected by using Combo.SelectedValue :

"Specified argument was out of the range of valid values.
Parameter name: '-2147483648' is not a valid value for 'index'.

Could you guys please comment? Here's my code:

/////////////////////////////////////////
Sub LoadCombo(ByVal objCombo As ComboBox, ByVal pSQL As String)
Dim dsDataSet As New DataSet
Dim tblTable As DataTable
Dim myRow As DataRow
Dim myConn As SqlConnection

myConn = New SqlConnection
myConn.ConnectionString = gsConnStr
myConn.Open()

theAdapter = New SqlDataAdapter(pSQL, myConn)
dsDataSet.Clear()
theAdapter.Fill(dsDataSet, "Table")

tblTable = dsDataSet.Tables("Table")

''Put a blank row at the top '****
myRow = tblTable.NewRow
myRow("DisplayField") = ""
tblTable.Rows.InsertAt(myRow, 0)

objCombo.DataSource = Nothing
objCombo.Items.Clear()
objCombo.DataSource = tblTable
objCombo.DisplayMember = "DisplayField"
objCombo.ValueMember = "KeyField"
objCombo.SelectedValue = 0 '****

dsDataSet = Nothing
tblTable = Nothing
myConn.Close()
/////////////////////////////////////////

Thanks!

Kay


Feb 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mal | last post by:
Sorry for repost - system added to another subject for some reason Have tried numerous ideas from the group to solve this one. It is such a simple example that it should be straightforward ! I...
6
by: Johann Blake | last post by:
I fill a table in a dataset with values that will be used by a combobox for the combobox's items. The combobox is a drop down list that only allows the user to select from the list but not enter...
3
by: Craig G | last post by:
i use the following to populate a combo in a datagrid but was wondering how i would add a blank entry to the top of the combo so that on first load it has no value selected? <asp:DropDownList...
3
by: washoetech | last post by:
I have a gridview control. In this grid view there is a column for the price of an item. Some of the prices have a dollar sign in front of it and some dont. How do I get rid of the dollar sign...
0
by: oracle | last post by:
Greetings, I have a combo box that I binded to a data set using text and tags. I want it to display the DRT.Name property and have a DRT.UnitId as a value. ...
10
by: lorirobn | last post by:
Hi, I have a form with several combo boxes, continuous form format, with record source a query off an Item Table. The fields are Category, Subcategory, and Color. I am displaying descriptions,...
5
by: Steve B. | last post by:
Without adding whitespace to the ComboBox datasource is there a way I can add a blank entry or, a reset entry, to the ComboBox dropdown Thanks Steve
0
by: mollyf | last post by:
I've got a combo box that I want to bind a collection to. I got the error "Complex Data Binding accepts as a data source either an IList or an IListSource" so I tried using the BindingSource,...
4
by: sree078 | last post by:
I've a list of items which I bound to multiple combo boxes. In the due course in the code...I had to add an item into one combo box...but I don't want it to be reflected into any other combo...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.