Combobox Load Help Please

  • Thread starter Thread starter Phill
  • Start date Start date
P

Phill

I have a Pocket PC app that has a combobox populated with data from SQL
Server Mobile. This is how I'm doing it now...which works:
SqlCeCommand cmd = new SqlCeCommand("SELECT Type_ID,Type
FROM tblEquipmentType ORDER BY Type", _conn);
cmd.Connection.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
EquipmentType.Items.Add((string)rdr[1]);
}
What I want to do is load Type_ID in the hidden field (it is the primary
key). How do I change this to accomplish it?

Thanks.
 
Thanks for the reply. That is what I am trying to do. I guess I don't have
anonymous type support because it doesn't like the syntax for this:
var item = new { Type_ID = rdr[0], Type = rdr[1] };
says Type is expected. I'm using VS 2005, target is Pocket PC. Is there
another way to do it?

Thanks


Peter Duniho said:
I have a Pocket PC app that has a combobox populated with data from SQL
Server Mobile. This is how I'm doing it now...which works:
SqlCeCommand cmd = new SqlCeCommand("SELECT Type_ID,Type
FROM tblEquipmentType ORDER BY Type", _conn);
cmd.Connection.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
EquipmentType.Items.Add((string)rdr[1]);
}
What I want to do is load Type_ID in the hidden field (it is the primary
key). How do I change this to accomplish it?

I'm not exactly sure I understand the question completely. I don't know
what you mean by "hidden field". But, if what you want is to have your
ComboBox list display the Type field, but return the Type_ID field as the
value, you can add a wrapper for your result that includes both fields,
and then set the DisplayMember and ValueMember properties of the ComboBox
itself appropriately.

For example, assuming you're using C# 3.0 and so have anonymous type
support:

EquipmentType.DisplayMember = "Type";
EquipmentType.ValueMember = "Type_ID";

while (rdr.Read())
{
var item = new { Type_ID = rdr[0], Type = rdr[1] };

EquipmentType.Items.Add(item);
}

By the way, I think there's some LINQ syntax that allows you to create the
query in LINQ rather than doing the SQL stuff explicitly. Then you could
just pass the result to the AddRange() method instead of looping and
calling Add() over and over. You'd still need to set the DisplayMember
and ValueMember properties appropriately, based on whatever names the
records in the LINQ query have for those fields.

If that's not answering your question, maybe you can elaborate on what you
mean by "in the hidden field".

Pete
 
I tried that, but when I tried to go to the design view of the form it said I
could only have 1 class in the form.

Peter Duniho said:
Thanks for the reply. That is what I am trying to do. I guess I don't
have
anonymous type support because it doesn't like the syntax for this:
var item = new { Type_ID = rdr[0], Type = rdr[1] };
says Type is expected. I'm using VS 2005, target is Pocket PC. Is there
another way to do it?

Sure, just write a named class to do the same thing. The anonymous class
just allows you to not litter your code with little class declarations,
but there's no reason you can't write the class explicitly instead.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top