Doubt creating a class/function?

P

Paulo

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?
 
A

Alexey Smirnov

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();
 
P

Paulo

But can you show me how to do the getDataSet ?

Thanks

Alexey Smirnov said:
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();
 
A

Alexey Smirnov

But can you show me how to do the getDataSet ?

Thanks

"Alexey Smirnov" <[email protected]> escreveu na mensagem

On Aug 9, 7:36 pm, "Paulo" <[email protected]> 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";

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;
}
 
A

Alexey Smirnov

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.
 
P

Paulo

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?
 

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