DropDown's DataSource/DataMember

  • Thread starter Thread starter John Spiegel
  • Start date Start date
J

John Spiegel

Hi all,

I think I've stared at it until it's made me stupid(er?). What am I missing
about setting the list of the dropdown to display data pulled from a table?

//...
daPrepaid.Fill(dsPrepaid, "Seller");
cboSeller.DataSource = dsPrepaid.Tables["Seller"];
cboSeller.DisplayMember = "Name";
//...

When I assign the datasource, the dropdown's Text property changes to
"System.Data.DataRowView", which is then the only item in the list as well.
What's the stupid thing I'm not seeing?

TIA,

John
 
Aren't you missing the cboSeller.ValueMember="Name"; <-- or whatever field ?

Greg
 
John,

A change is in my opinion that "Name" is in the wrong case or even not in
your select..

That because you use three words cbo (combobox) and DropDown.

When cbo is a webpage dropdownlist than it is easy.
You miss
cboSeller.DataBind();

I hope this helps?

Cor
 
Hey Greg,

I had it in, but was getting an error. The strange thing is when I do
include ValueMember, the error message indicates a problem with binding to
DisplayMember, more specifically:

Could not bind to the new disply member.
Parameter name: newDisplayMember

A less abbreviated snippet...

OleDbCommand cmdPrepaid = conPrepaid.CreateCommand();
cmdPrepaid.CommandType = CommandType.Text;
cmdPrepaid.CommandText = "SELECT * FROM PrepaidSeller";
daPrepaid = new OleDbDataAdapter();
daPrepaid.SelectCommand = cmdPrepaid;
dsPrepaid = new DataSet();
conPrepaid.Open();
try
{
daPrepaid.Fill(dsPrepaid, "Seller");

cboSeller.DataSource = dsPrepaid.Tables["Seller"];
cboSeller.DisplayMember = "EntityName";
cboSeller.ValueMember = "CustKey";
//...
}

Thanks,

John

Greg Burns said:
Aren't you missing the cboSeller.ValueMember="Name"; <-- or whatever field ?

Greg

John Spiegel said:
Hi all,

I think I've stared at it until it's made me stupid(er?). What am I
missing
about setting the list of the dropdown to display data pulled from a
table?

//...
daPrepaid.Fill(dsPrepaid, "Seller");
cboSeller.DataSource = dsPrepaid.Tables["Seller"];
cboSeller.DisplayMember = "Name";
//...

When I assign the datasource, the dropdown's Text property changes to
"System.Data.DataRowView", which is then the only item in the list as
well.
What's the stupid thing I'm not seeing?

TIA,

John
 
Hey Cor,

That's interesting. It was the case! I had used mixed case naming in
another piece a few days ago--but that was in a typed data set.

Thanks!

John
 
John,

Always the same problem and than I forgot it. You cannot use reserved SQL
words as items and AFAIK is "Name" one of them.

Cor
 
A change is in my opinion that "Name" is in the wrong case or even not in
your select..

Cor, I don't believe case-sensitivity matters. No?

If this were a web page, the dropdownlist binding would look something like
this:

ddlSeller.DataSource = dsPrepaid.Tables["Seller"];
ddlSeller.DataTextField = "Name";
ddlSeller.DataValueField = "NameID"; <-- change appropriately
ddlSeller.DataBind()

' optional...
ddlSeller.Items.Insert(0, New ListItem("-select a Name-", ""))


If it is a combobox on winform, then:
cboSeller.DataSource = dsPrepaid.Tables["Seller"];
cboSeller.DisplayMember = "Name"
cboSeller.ValueMember = "NameID"; <-- change appropriately

Personally, considering all the troubles I've had databinding with winforms
(selectedindex=-1 issues, bound controls on tabcontrols, etc.) I prefer to
just iterate the datatable and manually add the items. But that's just me.

my .02
Greg

Greg
 
Greg,

I saw that afterwards the same as you, that it could not be a webform
because that the dropdownlist has not a displaymember.

However than had John already sent a message replying you, I replied that
and after that he came with the answer that is was a case situation.

However I am still not sure if it was the case situation here or the wrong
itemname.

However the "case" is important in this situation (I have seen this to often
in this newsgroup while it is as well my own expirience). And the strange is
with that that SQL is case insensitive.

However just as far as I know.

Cor
 
Hmm.

I too was confused about winform and webform. :)

I thought you were onto something with the reserved word thing.
And the strange is with that that SQL is case insensitive.

"In SQL Server, object names can be case-sensitive, depending on the
character sort order that is installed. The sort order is chosen in SQL
Server Setup during installation. The default sort order in SQL Server
is dictionary order, case-insensitive.

If you need to change sort orders after installation, you must rebuild
your databases and reload your data."

John, try typing "execute sp_helpsort "in Query Analyzer to see what the
sort order and case-sensitivity is of your server. I know I am curious. :)

Greg
 
I'm pretty certain it was the case. The name, otherwise, DID match the
database.

I suppose the case sensitvity makes sense regardless of SQL's view since
once we get it from the database, it's no longer got anything to do with SQL
and is instead up to .NET's interpretation.
 
Sorry, I had been silly in my editing. The actual field name was
"EntityName"--which is what I was using on the .NET side as well...
 
I think you are right about it not mattering once you get it from the
database. It will matter if and when you try and write it back (if the
server is case-sensitive).

But... by not mattering I mean case-insensitive. Datatables etc are not
case-sensitive. Plus I just tried it to be sure! :)

Greg
 
My database is VFP, so I don't have the sp_help option. In VFP, object
names are always case-insensitive. I suppose it's possible that MSSQL could
cause a variation depending on how it's sensitivity was configured, but I'm
still thinking it's based on .NET's handling of the stream it gets from the
database--whatever flavor that may be.

It is a bit funny to me that a typed data set doesn't have to match case,
but the rest of it seems to.

Is it C#, perhaps? I just don't remember, but I was thinking that VB is
less rabid about its case handling.

- John

Oh and just to be clear, it is a WinForm I'm working on.
 
My database is VFP

:)

I been making too many assumptions. Sorry.

I would think a typed dataset in C# would definately be case sensitive, and
not so in VB. Afterall, a typed dataset is just a class. But I am not sure
how the rules apply when it comes to setting DisplayMember properites, etc.

Greg
 
Greg,

I tried that than as you probably almost suspected of course as well.

\\\only two comboboxes on a form.
Private Sub Form1_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Greg")
For i As Integer = 0 To 9
Dim dr As DataRow = dt.NewRow
dr(0) = i.ToString
dt.Rows.Add(dr)
Next
ComboBox1.DataSource = dt
ComboBox2.DataSource = dt
ComboBox1.DisplayMember = "Greg"
ComboBox1.DisplayMember = "greg"
End Sub
///
Combobox1 shows a table from zero to 9
Combobox2 shows a for me a table with 10 times
System.Data.DataRowView

Can you try it as well to see if we have different systems?

Cor


"Greg Burns"
 
My jaw has just hit the floor. You are (again) correct Cor.

I am seeing the same results, even when pulling from SQL (code below).

(My previous test was not actually using databinding afterall, sorry about
that)

It does appear to be case sensitive after reaching the datatable!

Since I have NEVER seen this before, I assume this is only case sensitive
when using DisplayMember and ValueMember??? (I rarely use databinding,
don't get me started) Accessing column data from the rows collection is not
case sensitive, is it?

I have never ran into this with webforms either and I use databinding with
DataTextField and DataValueField all the time. Do the same case-sensitivity
rules apply???

Greg


Dim cn As New SqlConnection("data source=.;initial
catalog=northwind;integrated security=SSPI;persist security
info=False;packet size=4096;")

'Dim cmd As New SqlCommand("SELECT CompanyName FROM shippers", cn)
Dim cmd As New SqlCommand("SELECT companyname FROM shippers", cn)


Dim dt As New DataTable
Dim da As New SqlDataAdapter(cmd)

da.Fill(dt)

ComboBox1.DataSource = dt
ComboBox2.DataSource = dt
ComboBox1.DisplayMember = "CompanyName"
ComboBox2.DisplayMember = "companyname"
 
Greg,
I have never ran into this with webforms either and I use databinding with
DataTextField and DataValueField all the time. Do the same
case-sensitivity rules apply???

You mean this one.
textbox1.DataBindings.Add(New Binding("Text", dataset1.Tables(0),
"LastName"))

Without testing now and AFAIK, Yes

Cor
 
You mean this one.
textbox1.DataBindings.Add(New Binding("Text", dataset1.Tables(0),
"LastName"))

Nah, I mean binding a DropDownList in a webform...

ddlWeek.DataSource = eTime.ApproveWeeksDB.GetApproveWeeks
ddlWeek.DataTextField = "range" ' works either way "range" or "Range" or
"rAnGe"
ddlWeek.DataValueField = "WeekEnd"
ddlWeek.DataBind()

Does not seems to be case sensitive at all here. I am sure it is probably
an entirely different architecture behind the scenes (winform databinding
versus webform databinding). Well, at least as far as binding a web
DropDownList compared to a winform ComboBox goes.

Greg
 
Back
Top