Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cache DropDownList

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hi All,
Im tryin to cache a drop down list....if it isn't postback, i'm queryin the DB to populate the dropdown, and utilzing a dataset. -- I'm not married to using a dataset if there is a more efficient way of doing so. So suggestions are welcome, my main problem though...is restoring the DataTextField & DataValueField's of the DDL when I restore from cache...

Code:
		void buildStatusDDL()
		{
	 	      DropDownList1.ID = "DropDownList1";
			DropDownList1.Visible = true;
			DropDownList1.AutoPostBack = true;
			DropDownList1.EnableViewState = true;
			ListItem ListItem0 = new ListItem();
			ListItem0.Text = "None";
			ListItem0.Value = "0";
			DropDownList1.Items.Add(ListItem0);
			System.Data.DataSet dsDdl = new System.Data.DataSet();
			if(Cache["Status"]==null)
			{
	
				string mySelectQuery="select statusgroupcd, description from prtadmin.statusgroup";
				OleDbConnection myConnection = new OleDbConnection(Db2Conn);
				myConnection.Open();
				OleDbDataAdapter cmd = new OleDbDataAdapter(mySelectQuery,myConnection);
				
				cmd.Fill(dsDdl,"Table");
				
				DropDownList1.DataSource = dsDdl.Tables[0];
				DropDownList1.DataTextField = dsDdl.Tables[0].Columns["description"].ColumnName.ToString();
				DropDownList1.DataValueField = dsDdl.Tables[0].Columns["statusgroupcd"].ColumnName.ToString();
				DropDownList1.DataBind();
				
				Cache["Status"] = (DataSet)dsDdl;
				cmd.Dispose();
				myConnection.Close();
			}
			else
			{
[COLOR=#ff0000]
//How do I assign these?  If I provide the following code just populates my dropdown with
		         //"System.Data.DataRowView" -- I assume I have to specify the value and text fields, but don't know how....[/color]
				DropDownList1.DataSource = (DataSet)Cache["Status"];
				DropDownList1.DataBind();
			}
			
			//Page.Controls.Add(DropDownList1);
			PlaceHolder1.Controls.Add(DropDownList1);
		
		}

All hail the INTERWEB!
 
Try chaning the line of code that casts your Cache object to a dataset:
Code:
DropDownList1.DataSource = (DataSet)Cache["Status"];

To:
Code:
DropDownList1.DataSource = [b](dsDdl)[/b]Cache["Status"];


Jim
 
Unforunately, that give me the following error:
"...: The type or namespace name 'dsDdl' could not be found (are you missing a using directive or an assembly reference?)
"

Thank you for the response....exact code follows with the update I just made reflected
Code:
			System.Data.DataSet dsDdl = new System.Data.DataSet();
			if(Cache["Status"]==null)
			{
	
				string mySelectQuery="select statusgroupcd, description from prtadmin.statusgroup";
				OleDbConnection myConnection = new OleDbConnection(Db2Conn);
				myConnection.Open();
				OleDbDataAdapter cmd = new OleDbDataAdapter(mySelectQuery,myConnection);
				
				cmd.Fill(dsDdl,"Table");
				
				DropDownList1.DataSource = dsDdl.Tables[0];
				DropDownList1.DataTextField = dsDdl.Tables[0].Columns["description"].ColumnName.ToString();
				DropDownList1.DataValueField = dsDdl.Tables[0].Columns["statusgroupcd"].ColumnName.ToString();
				DropDownList1.DataBind();
				
				Cache["Status"] = (DataSet)dsDdl;
				cmd.Dispose();
				myConnection.Close();
			}
			else
			{
				//How do I assign these?  If I provide the following code just populates my dropdown with
				//"System.Data.DataRowView" -- I assume I have to specify the value and text fields, but don't know how....
				DropDownList1.DataSource = (dsDdl)Cache["Status"];

All hail the INTERWEB!
 
Ok, you need to declare your dataset at page level and take it out of the function:
Code:
System.Data.DataSet dsDdl = new System.Data.DataSet();

Jim
 
Er...I moved it to the top of my page..

Code:
	public class ViewStateTest : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DropDownList DropDownList1 = new DropDownList();
		public string Db2Conn = "Provider=IBMDADB2.1;Password=manager;User ID=prtadmin;Data Source=PRTPROD;Persist Security Info=True";
		System.Data.DataSet dsDdl = new System.Data.DataSet();	
...

Now I receive the following error on this...
else
{
//How do I assign these? If I provide the following code just populates my dropdown with
//"System.Data.DataRowView" -- I assume I have to specify the value and text fields, but don't know how....
DropDownList1.DataSource = (dsDdl)Cache["Status"];
DropDownList1.DataBind();
}
[/code]

..(87): 'CAMIT.ViewStateTest.dsDdl' denotes a 'field' where a 'class' was expected

Thanks for your help

All hail the INTERWEB!
 
You have to move the declaration outside of the class delcaration, ie. before
Code:
public class ViewStateTest : System.Web.UI.Page
 
hmm...Won't let me do that either
..

Code:
namespace CAM
{
	/// <summary>
	/// Summary description for ViewStateTest.
	/// </summary>
	/// 
	[COLOR=red]System[/color].Data.DataSet dsDdl = new System.Data.DataSet();	
	public class ViewStateTest : System.Web.UI.Page
	{

...(19): A namespace does not directly contain members such as fields or methods


All hail the INTERWEB!
 
Sorry, my mistake, you can't declare it there.. you need it as before.. at page level.. although I am not sure what the other error means:
..(87): 'CAMIT.ViewStateTest.dsDdl' denotes a 'field' where a 'class' was expected

 
Try adding this using statment to the top of your page:
Code:
using system.data;
 
Thanks for stickin with me....Unforunately, I already have that up there...

Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Dundas.Charting.WebControl;
...

All hail the INTERWEB!
 
Try this syntax:
Code:
DropDownList1.DataSource = ((dsDdl)(Cache("Status")));
 
Identical error :(

Code:
DropDownList1.DataSource = (([coor red]dsDdl[/color])(Cache["Status"]));

...(91): 'CAMIT.ViewStateTest.dsDdl' denotes a 'field' where a 'class' was expected

Im googlin as we speak...err type

All hail the INTERWEB!
 
humm very strange.. I have never come across that error..
I am wondering if it is specific to C#.. I do VB...hummmm
 
You say in your original code:
If I provide the following code just populates my dropdown with
//"System.Data.DataRowView" -- I assume I have to specify the value and text fields, but don't know how....
Yet, you must know how to assign them as you do this in the other part of your "If" statement:
Code:
DropDownList1.DataTextField = dsDdl.Tables[0].Columns["description"].ColumnName.ToString();
                DropDownList1.DataValueField = dsDdl.Tables[0].Columns["statusgroupcd"].ColumnName.ToString();
So, all you need to do is use your cached DataSet and apply the same code and principle.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi,
I'm not sure I follow...
Code:
DropDownList1.DataSource = (dsDdl)Cache["Status"];
...Wont even compile

I may be able to do something like...
Code:
DataSet dsDdl2 = new DataSet();
dsDdl2 = (DataSet)Cache["Status"];
                DropDownList1.DataSource = dsDdl2 .Tables[0];
                DropDownList1.DataTextField = dsDdl2 .Tables[0].Columns["description"].ColumnName.ToString();
                DropDownList1.DataValueField = dsDdl2 .Tables[0].Columns["statusgroupcd"].ColumnName.ToString();
                DropDownList1.DataBind();

However that would mean creating an entirely new DataSet...




All hail the INTERWEB!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top