Brian Walsh

Brian Walsh

  • NA
  • 6
  • 0

Fill Listbox Using Select Statement - Business Name or Contact Name if Business Name is Blank or Null

Apr 7 2011 2:46 PM
I have a database that holds basic contact information such as business name, contact name, address and phone information. The following code is used to fill the listbox. The issue I need to resolve is this... I am using the BusinessName field to display and set the order of the listbox. The directory holds business and people contact information. For a person record the ContactName field will be filled out and the business name would be empty/null. I want the listbox to show BusinessName if it is Not Null however if the BusinessName is Null I would like the ContactName to show in the listbox.

I think I need some logic that uses the BusinessName if not null otherwise use ContactName.

Any help would be appreciated.

- Brian

string SQLConnString = "Data Source=AlphaServer\\sqlexpress;Database=CSCdb;Integrated Security=True;";
string connectionString = SQLConnString;
SqlConnection conn = new SqlConnection(connectionString);
string commandStringAll = "SELECT TypeBusiness, TypePerson, BusinessName, PhoneWork, PhoneFax, PhoneMobile, AddStreet, AddCity, AddState, AddZip, Website, Email, Category, ContactName, ContactTitle FROM tblDirectory ORDER BY BusinessName";
SqlDataAdapter dataAdapterAll = new SqlDataAdapter(commandStringAll, conn);
DataSet dsAll = new DataSet();
dataAdapterAll.Fill(dsAll, "prog");
dataTable = dsAll.Tables["prog"];
DirectorySelectLbx.ValueMember = "ID";
DirectorySelectLbx.Tag = "ID";
DirectorySelectLbx.DisplayMember = "BusinessName";
DirectorySelectLbx.DataSource = dsAll.Tables["prog"].DefaultView;
DirectorySelectLbx.SetSelected(0, true);
conn.Close();

Answers (5)