Showing posts with label foreach. Show all posts
Showing posts with label foreach. Show all posts

Wednesday, March 21, 2012

Dynamicly setting events

I have the following code and I'm trying to set an event dynamicly in the foreach statement...

SqlConnection

conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();

conn.Open();

da.Fill(ds);

conn.Close();

Table table =newTable();

foreach (DataRow drin ds.Tables[0].Rows)

{

TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++)

{

TableCell tc =newTableCell();

tc.Text = dr.ItemArray.GetValue(i).ToString();

tc.BackColor =

Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE")

{

tc.BackColor =

Color.BlueViolet;

}

tr.Cells.Add(tc);

}

table.Rows.Add(tr);

}

foreach (TableRow tablerowin table.Rows)

{

TableCell newtc =newTableCell();Button mybutton =newButton();

newtc.Controls.Add(mybutton);

mybutton.Text =

"details";

//set the event here

tablerow.Cells.Add(newtc);

}

Panel1.Controls.Add(table);

DataBind();

I am assuming you have added a button dynamically, and you want to create a handler for this button.

First you need to create a function to do what you want, e.g.

Sub myButtonClicked (sender, args)

....

End sub

Then you need to tell asp.net that this is the function to run when the button is clicked. In your code, before you add the button to the tablecell, write:

AddHandler myButton.click, AddressOg myButtonClicked

A little warning though - this may not work. This is because you have to create any dynamic controls everytime the page load. Have a read ofthis page which should help you.

|||

thanks for the response.

The solution that I have found is to add the eventhandler and specify what you want to do init. In this case redirect towww.google.com :

protected

void Page_Load(object sender,EventArgs e)

{

SqlConnection conn =newSqlConnection(ConfigurationManager.AppSettings["BvtQueueConnectString2"]);SqlCommand select =newSqlCommand("select top 100 b.jobid as [Job ID], bq.timesubmitted as [Time Submitted], b.timereceived as [Time Received], bq.requestid as [Request ID], bq.timecompleted as [Time Completed], bq.buginfoid as [Bug Info ID], e.eventid as [Event ID], bq.closingeventid as [Closing Event ID], et.eventtype as [Event Type], e.eventtime as [State Start Time], l.twolettercode as [Language], p.projectname as [Project Name], p.packagedesignation as [Package Designation] from bvtjobs b inner join bvtrequestsnew bq on b.jobid = bq.jobid inner join eventlog e on bq.requestid = e.bvtrequestid inner join eventtypes et on e.eventtype = et.eventtypeid inner join languages l on bq.targetlanguage = l.langid inner join projects p on bq.targetplatform = p.projectid order by b.jobid asc", conn);SqlDataAdapter da =newSqlDataAdapter(select);DataSet ds =newDataSet();

conn.Open();

da.Fill(ds);

conn.Close();

Table table =newTable();

foreach (DataRow drin ds.Tables[0].Rows)

{

TableRow tr =newTableRow();for (int i = 0; i < dr.ItemArray.Length; i++)

{

TableCell tc =newTableCell();

tc.Text = dr.ItemArray.GetValue(i).ToString();

tc.BackColor =

Color.White;if (dr.ItemArray.GetValue(8).ToString() =="COMPLETE")

{

tc.BackColor =

Color.BlueViolet;

}

tr.Cells.Add(tc);

}

table.Rows.Add(tr);

}

foreach (TableRow tablerowin table.Rows)

{

TableCell newtc =newTableCell();Button mybutton =newButton();

newtc.Controls.Add(mybutton);

mybutton.Text =

"details";

mybutton.Click +=

newEventHandler(mybutton_Click);

tablerow.Cells.Add(newtc);

}

Panel1.Controls.Add(table);

DataBind();

}

void mybutton_Click(object sender,EventArgs e)

{

Response.Redirect(

"http://www.google.com");

}

Friday, March 9, 2012

Dynamically changing task name displayed within ForEach Loop Container

Does anyone know how to change the task name displayed within a ForEach Loop Container (or of the ForEach Loop Container task itself) based on a variable. I am pretty familiar with setting variable values during task execution and using expressions to alter task properties based on variables. I have tried using an expression to alter the value of the Name property of the ForEach Loop Container but the name of the ForEach Loop Container does not change during execution. Since the color of the various tasks change during execution, I would think that the task names could be changed as well.

What? Why would you want to do this? The fact that the colors change is just a pretty *debug feature* and means nothing in the execution of the package.|||I know that. I have a loop that executes through a few hundred iterations and runs for 30 minutes or so. I'd like to review how far in the process it is while it runs.|||Can you write a counter to a log file or something?|||

You can create checkpoints or if that does not fit your needs; you could write to a file or table on each iteration an the look at that. if you are interacting with a DB you may use a DB profiler to monitor the activity. Or try to loop only a few times until you get confident with the package result.

Rafael Salas

|||

No, you can't change the task name during execution.

You can log to the log output window though and only log the increment count, that would be the equivalent.

|||Thanks to everyone for the alternative suggestions. Thanks to Kirk for the definitive answer.

Dynamically Change SSIS For Each Loop container

Hello,

I would like to modify "Files" attribute of the Foreach Loop of type File

Enumerator. This attribute is used to set the mask (for example *.txt) to

specify which files to include in the selection. I need to be able to change

this mask dynamically depending on package global variable. Is this possible?

Thank you!

Michael

Use the Expressions property, and create an expression for FileSpec property that references the global variable.|||

More details on How To get to the Extressions Property -

Open the ForEach Loop Editor by double clicking ForEach Loop Container.
Select Collection on left.
Click on the + sign on Expressions
Select FileSpec for Property and On Expression select the Global Variable Name. (which holds the file property such as *.txt)

Thanks,
Loonysan