binding combobox

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Edward (Edward_at_discussions.microsoft.com)
Date: 09/21/04


Date: Tue, 21 Sep 2004 06:29:05 -0700

Hello,

I'm sure a lot of you can help...

I have the following function:

****************
string sSQL = "SELECT CountryId, Country FROM Countries ORDER BY Country";
string sConnectionString = "SERVER=FGMCSQL;DATABASE=Web;USER
ID=sql123;PASSWORD=qwerty;";
SqlConnection sqlCon = new SqlConnection(sConnectionString);
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand();
sqlCom.CommandType = CommandType.Text;
sqlCom.CommandText = sSQL;
sqlCom.Connection = sqlCon;

SqlDataAdapter da = new SqlDataAdapter(sqlCom);
DataTable dt = new DataTable();
da.Fill(dt);

cboDTCountry.DataSource = dt.DefaultView;
cboDTCountry.ValueMember = "CountryId";
cboDTCountry.DisplayMember = "Country";

sqlCon.Close();

***********

Can anyone tell me why I'm getting an error when my sql string is changed to:
string sSQL = "SELECT * FROM Countries ORDER BY Country";

The error happens in this line:
cboDTCountry.ValueMember = "CountryId";

It says it cannot bind to it. I know asp.net doesn't generate this error
when this same concept is applied. Can anyone explain why using an asterisk
(*) generates this error? Must I instead select individual fields from the
database? While your reading this, I'm very open to any suggestions on
improving this simple function. Please have a shot at it!

Thanks,
Edward



Relevant Pages

  • combo box databinding
    ... string sSQL = "SELECT CountryId, Country FROM Countries ORDER BY Country"; ... SqlConnection sqlCon = new SqlConnection; ...
    (microsoft.public.dotnet.general)
  • combo box databinding
    ... string sSQL = "SELECT CountryId, Country FROM Countries ORDER BY Country"; ... SqlConnection sqlCon = new SqlConnection; ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Need help with Access SQL Query
    ... Description from Countries ORDER BY Description ... > valid 2 digit country code is entered or selected. ... I came up with this SQL query for the row source: ... > The bound column is set to number 1, ...
    (microsoft.public.access.queries)