Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 3rd, 2007, 08:53 PM
Registered User
 
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ComboBox Problem

I'm populating a ComboBox from a database using a stored procedure and then setting its DropDownStyle to DropDownList so that a user can only select the values I've set for it after the initial population from the stored procedure. The trouble is that I can populate it just fine if the style is set to DropDown, but then the user can just type anything under the sun (which I definitely don't want). But if I set the style to DropDownList, even after the sproc call, my database value doesn't appear in the box. I'm fairly new to C#, but figure this should be able to work?

Code below....

This is in the form load (ComboBox style and its values)*
private void ClientList_Load(object sender, EventArgs e)
{
cmbSelectType.DropDownStyle = ComboBoxStyle.DropDownList;
cmbTitle.Items.Add("Dr.");
cmbTitle.Items.Add("Mr.");
cmbTitle.Items.Add("Ms.");
cmbTitle.Items.Add("Mrs.");
...

*This is in the "Get Clients" button to pull data to title ComboBox*
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("uspGetClients", conn);
conn.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DataSet DS = new DataSet();
cmd.Parameters.Add("@SelectType", SqlDbType.VarChar, 20).Value = Convert.ToString(cmbSelectType.Text);
cmd.Parameters.Add("@SelectRegion", SqlDbType.VarChar, 20).Value = Convert.ToString(cmbSelectRegion.Text);
DataTable Client = new DataTable();
DA.Fill(Client);
int rowPosition = 0;
cmbTitle.Text = Client.Rows[rowPosition]["Title"].ToString();
 
Old January 4th, 2007, 03:12 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Take a new combobox say cmbclient.
try,
DataSet ds =new DataSet();
DA.Fill(ds,"client");
cmbclient.DataSource=ds.Tables["client"];

Bijgupt
 
Old January 6th, 2007, 01:22 PM
Registered User
 
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yep, somebody else suggested the same and that seems to work. But now how can I "unbind" that DataSource when user clicks on cmbClient so that the fixed values I set during form load appear so that the user can choose them?

Thx,
Shane







Similar Threads
Thread Thread Starter Forum Replies Last Post
Combobox problem ajit Javascript 1 December 3rd, 2006 05:07 AM
Problem in ComboBox Tayyab326 ASP.NET 1.0 and 1.1 Professional 0 November 15th, 2006 03:03 AM
c# comboBox Initialization problem ColdFusion ASP.NET 2.0 Basics 1 May 11th, 2006 12:44 PM
problem about combobox nucharee General .NET 2 October 1st, 2004 04:05 AM
ComboBox problem chacquard Access 2 September 10th, 2004 11:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.