DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Thu, 9 Aug 2007 14:36:08 -0300,    posted on: microsoft.public.dotnet.framework.aspnet        back       

Thread Index
  1    Paulo
          2    Alexey Smirnov
          3    Paulo
          4    Alexey Smirnov
          5    Alexey Smirnov
          6    Paulo


Doubt creating a class/function?   
Hi, I have a lot of this piece of code (C# asp.net 2.0 - VS 2005) to fill a 
lot of combos:

SqlConnection mycn;
SqlDataAdapter myda;
DataSet ds;
mycn = new SqlConnection(connectionString);
myda = new SqlDataAdapter("Select CIDADE_ID, NOME_CIDADE FROM CIDADE Order 
By NOME_CIDADE", mycn);
ds = new DataSet();
myda.Fill(ds);
cboCidade.DataSource = ds.Tables[0];
cboCidade.DataTextField = 
ds.Tables[0].Columns["NOME_CIDADE"].ColumnName.ToString();
cboCidade.DataValueField = 
ds.Tables[0].Columns["CIDADE_ID"].ColumnName.ToString();
cboCidade.DataBind();

My doubt is: how can I make it a class passing parameters like ComboName, 
Fields, etc, and returning just the dataset?
Can you teach me how to centralize the code?
Date:Thu, 9 Aug 2007 14:36:08 -0300   Author:  

Re: Doubt creating a class/function?   
On Aug 9, 7:36 pm, "Paulo"  wrote:

Hi Paulo


> cboCidade.DataTextField =
> ds.Tables[0].Columns["NOME_CIDADE"].ColumnName.ToString();
> cboCidade.DataValueField =
> ds.Tables[0].Columns["CIDADE_ID"].ColumnName.ToString();


The two lines above make no sense. Keep them simple

cboCidade.DataTextField = "NOME_CIDADE";
cboCidade.DataValueField = "CIDADE_ID";

or

cboCidade.DataTextField = ds.Tables[0].Columns[0];
cboCidade.DataValueField = ds.Tables[0].Columns[1];

Based on it, you can create a function that returns a dataset or a
datatable and so, your final call could be following:

DataSet ds = getDataSet("CIDADE_ID", "NOME_CIDADE");
cboCidade.DataSource = ds;
cboCidade.DataTextField = ds.Tables[0].Columns[0];
cboCidade.DataValueField = ds.Tables[0].Columns[1];
cboCidade.DataBind();
Date:Thu, 09 Aug 2007 10:54:41 -0700   Author:  

Re: Doubt creating a class/function?   
But can you show me how to do the getDataSet ?

Thanks

"Alexey Smirnov"  escreveu na mensagem 
news:1186682081.354617.260630@g4g2000hsf.googlegroups.com...

> On Aug 9, 7:36 pm, "Paulo"  wrote:
>
> Hi Paulo
>
>> cboCidade.DataTextField =
>> ds.Tables[0].Columns["NOME_CIDADE"].ColumnName.ToString();
>> cboCidade.DataValueField =
>> ds.Tables[0].Columns["CIDADE_ID"].ColumnName.ToString();
>
> The two lines above make no sense. Keep them simple
>
> cboCidade.DataTextField = "NOME_CIDADE";
> cboCidade.DataValueField = "CIDADE_ID";
>
> or
>
> cboCidade.DataTextField = ds.Tables[0].Columns[0];
> cboCidade.DataValueField = ds.Tables[0].Columns[1];
>
> Based on it, you can create a function that returns a dataset or a
> datatable and so, your final call could be following:
>
> DataSet ds = getDataSet("CIDADE_ID", "NOME_CIDADE");
> cboCidade.DataSource = ds;
> cboCidade.DataTextField = ds.Tables[0].Columns[0];
> cboCidade.DataValueField = ds.Tables[0].Columns[1];
> cboCidade.DataBind();
> 
Date:Thu, 9 Aug 2007 15:02:41 -0300   Author:  

Re: Doubt creating a class/function?   
On Aug 9, 8:02 pm, "Paulo"  wrote:

> But can you show me how to do the getDataSet ?
>
> Thanks
>
> "Alexey Smirnov"  escreveu na mensagemnews:1186682081.354617.260630@g4g2000hsf.googlegroups.com...
>
>
>
> > On Aug 9, 7:36 pm, "Paulo"  wrote:
>
> > Hi Paulo
>
> >> cboCidade.DataTextField =
> >> ds.Tables[0].Columns["NOME_CIDADE"].ColumnName.ToString();
> >> cboCidade.DataValueField =
> >> ds.Tables[0].Columns["CIDADE_ID"].ColumnName.ToString();
>
> > The two lines above make no sense. Keep them simple
>
> > cboCidade.DataTextField = "NOME_CIDADE";
> > cboCidade.DataValueField = "CIDADE_ID";
>
> > or
>
> > cboCidade.DataTextField = ds.Tables[0].Columns[0];
> > cboCidade.DataValueField = ds.Tables[0].Columns[1];
>
> > Based on it, you can create a function that returns a dataset or a
> > datatable and so, your final call could be following:
>
> > DataSet ds = getDataSet("CIDADE_ID", "NOME_CIDADE");
> > cboCidade.DataSource = ds;
> > cboCidade.DataTextField = ds.Tables[0].Columns[0];
> > cboCidade.DataValueField = ds.Tables[0].Columns[1];
> > cboCidade.DataBind();- Hide quoted text -
>
> - Show quoted text -


you need just to move your db-code there

private DataSet getDataSet(string col1, string col2)
{
SqlConnection mycn;
SqlDataAdapter myda;
DataSet ds;
mycn = new SqlConnection(connectionString);
myda = new SqlDataAdapter("Select " + col1 + "," + col2 + " FROM
CIDADE Order By NOME_CIDADE", mycn);
ds = new DataSet();
myda.Fill(ds);
return ds;
}
Date:Thu, 09 Aug 2007 11:19:57 -0700   Author:  

Re: Doubt creating a class/function?   
On Aug 9, 7:54 pm, Alexey Smirnov  wrote:

> cboCidade.DataTextField = ds.Tables[0].Columns[0];
> cboCidade.DataValueField = ds.Tables[0].Columns[1];


Wait... I've made a mistake here

you would need to add a ColumnName property

cboCidade.DataTextField = ds.Tables[0].Columns[0].ColumnName;
cboCidade.DataValueField = ds.Tables[0].Columns[1].ColumnName;

because DataTextField and DataValueField need a string value.
Date:Thu, 09 Aug 2007 11:29:08 -0700   Author:  

Re: Doubt creating a class/function?   
Thanks Alexey, I was thinking: is there any way to write automatically a 
combo on the screen? what I mean is write procedure wich I pass all the 
parameters and then the component is generated, I dont know... so in the 
middle of the code I just call:

MakeCombo(parameters, strSQL, strConBD, etc);

and inside the procedure will be the code that makes on the fly the 
component...

Am I crazy? or can it be done?
Date:Thu, 9 Aug 2007 16:02:56 -0300   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us