Combo box refresh

D

Doug

Hi

I have a combo box (A) that populates a following combo box (B) based on a
selection. The selection from the first combo box (A) initiates an
OleDbDataAdapter routine that extracts the values for the second combo box
(B) from a database.

However, when I choose a value from the first combo box (A), the second
combo box (B) populates as required, but if i change my mind about the
selection from the first combo box (A) , the second box (B) still retains
the values that were populated from the first selection from the first combo
box (A).

I would obviously like to be able to make the second combo box (B) dynamic
and to refresh based on the change in combo box (A).

Any assistance appreciated.

Doug.
 
R

Richard Blewett [DevelopMentor]

Doug said:
Hi

I have a combo box (A) that populates a following combo box (B) based on a
selection. The selection from the first combo box (A) initiates an
OleDbDataAdapter routine that extracts the values for the second combo box
(B) from a database.

However, when I choose a value from the first combo box (A), the second
combo box (B) populates as required, but if i change my mind about the
selection from the first combo box (A) , the second box (B) still retains
the values that were populated from the first selection from the first
combo box (A).

I would obviously like to be able to make the second combo box (B) dynamic
and to refresh based on the change in combo box (A).

Any assistance appreciated.

Doug.

How are you triggering the fill of combobox B? Are you sure you're not just
forgetting to clear out the old items before adding the new ones?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
D

Doug

Most probably you are correct - but can you explain how i could clear the
old items out?

I am filling the combo box with ...

private void PopulateDatasetList(string libname)

{

string server = "";

SAS.Workspace ws = null;

try

{

server = consumer.AssignedServer;

ws = consumer.Workspace(server) as SAS.Workspace;

}

catch (Exception ex)

{

throw new System.Exception("ISASTaskConsumer.Workspace is not usable!",ex);

}

if (server.Length>0 && ws!=null)

{

ADODB.Recordset adorecordset = new ADODB.RecordsetClass();

ADODB.Connection adoconnect = new ADODB.ConnectionClass();

try

{

adoconnect.Open("Provider=sas.iomprovider.1; SAS Workspace ID=" +

ws.UniqueIdentifier, "", "", 0);

string selectclause = "select memname, memtype from sashelp.vmember where
libname='" + libname + " ' and memtype = 'DATA'";

adorecordset.Open( selectclause, adoconnect,

ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly,
(int) ADODB.CommandTypeEnum.adCmdText);

OleDbDataAdapter da = new OleDbDataAdapter();

da.Fill(dsVMember, adorecordset, "vmember"); }

catch

{ }

finally

{ adoconnect.Close(); }}}



and then using

private void cmbMembers_SelectedIndexChanged(object sender,

System.EventArgs e)

{
members=((DataRowView)cmbMembers.SelectedItem).Row.ItemArray[0].ToString();
}
 
R

Richard Blewett [DevelopMentor]

Doug said:
Most probably you are correct - but can you explain how i could clear the
old items out?

I am filling the combo box with ...

private void PopulateDatasetList(string libname)

{

string server = "";

SAS.Workspace ws = null;

try

{

server = consumer.AssignedServer;

ws = consumer.Workspace(server) as SAS.Workspace;

}

catch (Exception ex)

{

throw new System.Exception("ISASTaskConsumer.Workspace is not
usable!",ex);

}

if (server.Length>0 && ws!=null)

{

ADODB.Recordset adorecordset = new ADODB.RecordsetClass();

ADODB.Connection adoconnect = new ADODB.ConnectionClass();

try

{

adoconnect.Open("Provider=sas.iomprovider.1; SAS Workspace ID=" +

ws.UniqueIdentifier, "", "", 0);

string selectclause = "select memname, memtype from sashelp.vmember where
libname='" + libname + " ' and memtype = 'DATA'";

adorecordset.Open( selectclause, adoconnect,

ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly,
(int) ADODB.CommandTypeEnum.adCmdText);

OleDbDataAdapter da = new OleDbDataAdapter();

da.Fill(dsVMember, adorecordset, "vmember"); }

catch

{ }

finally

{ adoconnect.Close(); }}}



and then using

private void cmbMembers_SelectedIndexChanged(object sender,

System.EventArgs e)

{
members=((DataRowView)cmbMembers.SelectedItem).Row.ItemArray[0].ToString();
}


How are you triggering the fill of combobox B? Are you sure you're not
just forgetting to clear out the old items before adding the new ones?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Your sample code is incomplete.

In PopulateDatasetList how is this populating the combobox? is it databound?

In the event handler what is "members"?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
D

Doug

Richard,

Thanks for you help.And sorry for the long post. I dont know which parts
are the essential bits.


Actually I am still struggling with this code - i didnt write the logic and
it seems to be very confusing to someone with my limited understanding of
c#.

the full program is below:

//---------------------------------------------------------------

// A modal dialog that returns values to a class used by SAS EG

// ---------------------------------------------------------------

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.OleDb;

using SAS.EG.AddIns;

namespace Sausag

{

/// <summary

/// Summary description for CharacterizeDataForm.

/// </summary

public class SausagForm : System.Windows.Forms.Form

{

/// <summary

/// Required designer variable.

/// </summary

private System.ComponentModel.Container components = null;

private System.Windows.Forms.Label lblLib;

private System.Windows.Forms.Button btnOK;

private System.Windows.Forms.Button btnCancel;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.ComboBox cmbServers;

private System.Windows.Forms.ComboBox cmbLibs;



#region Properties for this add-in task

/// <summary

/// ISASTaskConsumer, our hook to the host application for services

/// </summary

public ISASTaskConsumer Consumer

{

set

{

consumer = value;

}

}

private ISASTaskConsumer consumer = null;

public string Library

{

set

{

library = value;

}

get

{

return library;

}

}

private string library;

public bool IncludeCharts

{

set

{

bCharts = value;

}

get

{

return bCharts;

}

}

private bool bCharts = false;

public bool AllMembers

{

set

{

bAllData = value;

}

get

{

return bAllData;

}

}

private bool bAllData = false;

public string Members

{

set

{

members = value;

}

get

{

return members;

}

}

private string members;

public string Catobs

{

set

{

catobs = value;

}

get

{

return catobs;

}

}

private string catobs = "10";

#endregion



private string currServer = "";

private System.Windows.Forms.Label label3;

private System.Windows.Forms.TextBox txtCatobs;

private System.Windows.Forms.ComboBox cmbMembers;

private System.Data.DataTable dataTable1;

private System.Data.DataColumn dataColumn1;

private System.Data.DataColumn dataColumn2;

private System.Data.DataSet dsVMember;

private System.Windows.Forms.Label lblData;

public SausagForm()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

}

/// <summary

/// Clean up any resources being used.

/// </summary

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary

private void InitializeComponent()

{

this.lblLib = new System.Windows.Forms.Label();

this.btnOK = new System.Windows.Forms.Button();

this.btnCancel = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.cmbServers = new System.Windows.Forms.ComboBox();

this.cmbLibs = new System.Windows.Forms.ComboBox();

this.lblData = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.txtCatobs = new System.Windows.Forms.TextBox();

this.cmbMembers = new System.Windows.Forms.ComboBox();

this.dsVMember = new System.Data.DataSet();

this.dataTable1 = new System.Data.DataTable();

this.dataColumn1 = new System.Data.DataColumn();

this.dataColumn2 = new System.Data.DataColumn();

((System.ComponentModel.ISupportInitialize)(this.dsVMember)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();

this.SuspendLayout();

//

// lblLib

//

this.lblLib.FlatStyle = System.Windows.Forms.FlatStyle.System;

this.lblLib.Location = new System.Drawing.Point(5, 44);

this.lblLib.Name = "lblLib";

this.lblLib.Size = new System.Drawing.Size(166, 23);

this.lblLib.TabIndex = 2;

this.lblLib.Text = "Select a library to analyze:";

//

// btnOK

//

this.btnOK.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK; this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System; this.btnOK.Location = new System.Drawing.Point(225, 311); this.btnOK.Name = "btnOK"; this.btnOK.TabIndex = 11; this.btnOK.Text = "OK"; // // btnCancel // this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System; this.btnCancel.Location = new System.Drawing.Point(310, 311); this.btnCancel.Name = "btnCancel"; this.btnCancel.TabIndex = 12; this.btnCancel.Text = "Cancel"; // // label1 // this.label1.Location = new System.Drawing.Point(5, 12); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(167, 23); this.label1.TabIndex = 0; this.label1.Text = "Select a SAS server to use:"; // // cmbServers // this.cmbServers.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.cmbServers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbServers.Location = new System.Drawing.Point(179, 11); this.cmbServers.Name = "cmbServers"; this.cmbServers.Size = new System.Drawing.Size(201, 21); this.cmbServers.TabIndex = 1; this.cmbServers.SelectedIndexChanged += new System.EventHandler(this.cmbServers_SelectedIndexChanged); // // cmbLibs // this.cmbLibs.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.cmbLibs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbLibs.Location = new System.Drawing.Point(177, 42); this.cmbLibs.Name = "cmbLibs"; this.cmbLibs.Size = new System.Drawing.Size(201, 21); this.cmbLibs.TabIndex = 2; this.cmbLibs.SelectedIndexChanged += new System.EventHandler(this.cmbLibs_SelectedIndexChanged); // // lblData // this.lblData.Location = new System.Drawing.Point(5, 82); this.lblData.Name = "lblData"; this.lblData.Size = new System.Drawing.Size(172, 16); this.lblData.TabIndex = 5; this.lblData.Text = "Selected data:"; // // label3 // this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.label3.Location = new System.Drawing.Point(5, 213); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(221, 16); this.label3.TabIndex = 9; this.label3.Text = "Max records to print in each data set:"; // // txtCatobs // this.txtCatobs.Location = new System.Drawing.Point(292, 211); this.txtCatobs.Name = "txtCatobs"; this.txtCatobs.Size = new System.Drawing.Size(70, 20); this.txtCatobs.TabIndex = 4; this.txtCatobs.Text = "10"; this.txtCatobs.TextChanged += new System.EventHandler(this.txtCatobs_TextChanged); // // cmbMembers // this.cmbMembers.DataSource = this.dsVMember; this.cmbMembers.DisplayMember = "vmember.memname"; this.cmbMembers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbMembers.Location = new System.Drawing.Point(178, 79); this.cmbMembers.Name = "cmbMembers"; this.cmbMembers.Size = new System.Drawing.Size(201, 21); this.cmbMembers.TabIndex = 3; this.cmbMembers.SelectedIndexChanged += new System.EventHandler(this.cmbMembers_SelectedIndexChanged); // // dsVMember // this.dsVMember.DataSetName = "data"; this.dsVMember.Locale = new System.Globalization.CultureInfo("en-AU"); this.dsVMember.Tables.AddRange(new System.Data.DataTable[] { this.dataTable1}); // // dataTable1 // this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] { this.dataColumn1, this.dataColumn2}); this.dataTable1.TableName = "vmember"; // // dataColumn1 // this.dataColumn1.ColumnName = "memname"; // // dataColumn2 // this.dataColumn2.ColumnName = "memtype"; // // SausagForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(395, 341); this.Controls.Add(this.cmbMembers); this.Controls.Add(this.txtCatobs); this.Controls.Add(this.label3); this.Controls.Add(this.lblData); this.Controls.Add(this.cmbLibs); this.Controls.Add(this.cmbServers); this.Controls.Add(this.label1); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnOK); this.Controls.Add(this.lblLib); this.MaximizeBox = false; this.MinimizeBox = false; this.MinimumSize = new System.Drawing.Size(403, 368); this.Name = "SausagForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Characterize Data"; this.Load += new System.EventHandler(this.CharacterizeDataForm_Load); ((System.ComponentModel.ISupportInitialize)(this.dsVMember)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit(); this.ResumeLayout(false); } #endregion private void CharacterizeDataForm_Load(object sender, System.EventArgs e) { if (consumer == null) throw new System.Exception("Must set ISASTaskConsumer as Consumer property!"); // get the list of available servers from the application string[] servers; consumer.Servers(out servers); if (servers.Length == 0) throw new System.Exception("No servers are available!"); // populate the servers combobox cmbServers.Items.AddRange(servers); if (consumer.AssignedServer.Length0) cmbServers.SelectedItem = consumer.AssignedServer; currServer = cmbServers.SelectedItem.ToString(); PopulateLibraryList(); txtCatobs.Text = catobs; // UpdateControls(); } private void PopulateLibraryList() { Cursor c = Cursor.Current; Cursor.Current = Cursors.WaitCursor; cmbLibs.Items.Clear(); string[] libs; consumer.Libraries(cmbServers.SelectedItem.ToString(), out libs); if (libs.Length == 0) { Cursor.Current = c; throw new System.Exception("No libraries are available!"); } cmbLibs.Items.AddRange(libs); if (cmbLibs.Items.Contains(library)) cmbLibs.SelectedItem = library; else cmbLibs.SelectedIndex = 0; Cursor.Current = c; } // uses the Workspace property of the ISASTaskConsumer // and the SAS IOM OLEDB Provider to connect to // the SAS session and query for additional information // about the SAS data private void PopulateDatasetList(string libname) { string server = ""; SAS.Workspace ws = null; try { server = consumer.AssignedServer; ws = consumer.Workspace(server) as SAS.Workspace; } catch (Exception ex) { throw new System.Exception("ISASTaskConsumer.Workspace is not usable!",ex); } if (server.Length0 && ws!=null) { ADODB.Recordset adorecordset = new ADODB.RecordsetClass(); ADODB.Connection adoconnect = new ADODB.ConnectionClass(); try { adoconnect.Open("Provider=sas.iomprovider.1; SAS Workspace ID=" + ws.UniqueIdentifier, "", "", 0); string selectclause = "select memname, memtype from sashelp.vmember where libname='" + libname + " ' and memtype = 'DATA'"; adorecordset.Open( selectclause, adoconnect, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, (int) ADODB.CommandTypeEnum.adCmdText); OleDbDataAdapter da = new OleDbDataAdapter(); da.Fill(dsVMember, adorecordset, "vmember"); } catch { } finally { adoconnect.Close(); } } } private void cmbServers_SelectedIndexChanged(object sender, System.EventArgs e) { try { PopulateLibraryList(); currServer = cmbServers.SelectedItem.ToString(); } catch { MessageBox.Show(string.Format("There was an error trying to connect to \"{0}\".", cmbServers.SelectedItem),"Connection error"); cmbServers.SelectedItem = currServer; } } private void cmbLibs_SelectedIndexChanged(object sender, System.EventArgse) { try { library = cmbLibs.SelectedItem.ToString(); PopulateDatasetList(library); } catch { MessageBox.Show(string.Format("There was an error trying toaccess\"{0}\".", cmbLibs.SelectedItem),"Connection error"); cmbLibs.SelectedItem = library; } } private void OnCurrentDataCellChanged(object sender, System.EventArgs e) { try { DataSet ds = cmbMembers.DataSource as DataSet; members = cmbServers.SelectedIndex.ToString(); } catch { } } private void txtCatobs_TextChanged(object sender, System.EventArgs e) { try { // catobs = Convert.ToInt32(txtCatobs.Text); catobs = Convert.ToString(txtCatobs.Text); } catch { } } private void cmbMembers_SelectedIndexChanged(object sender, System.EventArgs e) { members=((DataRowView)cmbMembers.SelectedItem).Row.ItemArray[0].ToString(); } } } //"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot codot uk wrote in message news:[email protected]... "Doug" <[email protected] wrote in message news:[email protected]... Most probably you are correct - but can you explain how i could clear the old items out? I am filling the combo box with ... private void PopulateDatasetList(string libname) { string server = ""; SAS.Workspace ws = null; try { server = consumer.AssignedServer; ws = consumer.Workspace(server) as SAS.Workspace; } catch (Exception ex) { throw new System.Exception("ISASTaskConsumer.Workspace is not usable!",ex); } if (server.Length0 && ws!=null) { ADODB.Recordset adorecordset = new ADODB.RecordsetClass(); ADODB.Connection adoconnect = new ADODB.ConnectionClass(); try { adoconnect.Open("Provider=sas.iomprovider.1; SAS Workspace ID=" + ws.UniqueIdentifier, "", "", 0); string selectclause = "select memname, memtype from sashelp.vmember where libname='" + libname + " ' and memtype = 'DATA'"; adorecordset.Open( selectclause, adoconnect, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, (int) ADODB.CommandTypeEnum.adCmdText); OleDbDataAdapter da = new OleDbDataAdapter(); da.Fill(dsVMember, adorecordset, "vmember"); } catch { } finally { adoconnect.Close(); }}} and then using private void cmbMembers_SelectedIndexChanged(object sender, System.EventArgs e) { members=((DataRowView)cmbMembers.SelectedItem).Row.ItemArray[0].ToString(); } How are you triggering the fill of combobox B? Are you sure you're not just forgetting to clear out the old items before adding the new ones? Regards Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk Your sample code is incomplete. In PopulateDatasetList how is this populating the combobox? is itdatabound? In the event handler what is "members"? Regards Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
 
R

Richard Blewett [DevelopMentor]

Doug said:
Richard,

Thanks for you help.And sorry for the long post. I dont know which parts
are the essential bits.


Actually I am still struggling with this code - i didnt write the logic
and
it seems to be very confusing to someone with my limited understanding of
c#.

<snip>

Put a breakpoint in the eventhandler

cmbLibs_SelectedIndexChanged

and see what happenes when you change the library. The code swallows alot of
exceptions so what is probably happening is one is being thrown and the code
slitently ignores it

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
G

Guest

YES thats the way it is as long as ya r using DataSource property.

i had situition like this previously.

wat i hav done is that create a custom control (nullable comboBox) in which
has 2 controls (1 text box and 1 drop down in the same location but only 1
visible at any time).

its straightfoward and solved the prob from the root up.

(sry, this is not my working comp, i cudnt find the program, otherwise, i
cud hav post some sample code here)

hope this helps
 
D

Doug

Thanks Richard, but this code is run in an external application as a class
library.

The program that runs the library does not appear to allow debugging. Hence
the difficulty in finding errors.

I put a messagebox.show("library ="+library); in that part of the code and
the library definition works well. Could it be that the ado connection is
not refreshing to allow the dataset to be replenished?

Doug

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:%[email protected]...
 

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

Top