.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

SQL-transaction for foreach/for loop

Posted By:      Posted Date: September 27, 2010    Points: 0   Category :ASP.Net

i am making a web application in which at one time i have to insert the vales into three table..

first table will generate scope_identity,

then in foreach loop values are inserted in 2nd table with ref of scope identity

and then same with the 3rd table

problem is i dont know how to put transaction in foreach loop......

please help me.......

here is my dummy code please help me....

SqlConnection con = new SqlConnection("server=xxxx;uid=xxxx;password=xxx;database=xxxx;connect timeout=3600");
        SqlCommand cmd = new SqlCommand("insert into test(a,b,c,d,e,f,g,h) values ('a','b','c','d','e','f','g','h');select scope_identity()", con);
        decimal idd = (decimal)cmd.ExecuteScalar();

        for (int i = 0; i < 15; i++)
            SqlCommand cmd1 = new SqlCommand("insert into test1(a,b,c,d,e,f,g,h) values ('"+idd.ToString()+"','b','c','d','e','f','g','h')", con);
        for (int i = 0; i < 15; i++)
            SqlCommand cmd2 = new SqlCommand("insert into test2(a,b,c,d,e,f,g,h) values ('&

View Complete Post

More Related Resource Links

Error on 23rd Loop of Foreach

Hi, I have a MAster Package callling 8 Dimension Package and 5 fact table packages. Fact table Packages are in for eachloop. They have to loop 24 times during FirstRun Each fact table load more than 500000 rows in every loop, and out of 5, two package loads more than million rows in every loop. I got the following error message when the package was looping for the 23rd time. One of the five Fact table package failed with following error DTS primeoutput failed returned error code 0xC02020C4 SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on component "Staging Table "(1) returned error code 0xC02020C4. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure. After when I ran package explicitly, it ran well Please help on what to be done to avoid this error and what does this error mean?. I am Using SQL Server 2008, i5 Processor, 3.42GB RAm, DefaultBufferMaxRows = 10000 and Default BufferSize 10MB, EngineTHread=10 Thanks, Anuja

ForEach Loop Container Configuration Question

So here is my question/situation. I have a table that contains a list of all of our servers/instances for my organization. I have a script that I'd like to execute against all of the servers in that list and I was thinking that I should be able to use this container to pull the name/instance from the table, execute the script, store the results in a local table. However, I'm not quite sure on how to attack this meaning not sure which configuration that I would need to use to get me going down the right direction. I've looked around and found alot of documentation on how to enumerate multiple files and folders, but nothing really for what I'm trying to do. Any help or direction would be greatly appreciated.Big Ern

SSIS 2005 - Foreach loop container - Stopping the loop after processing X number of file?

I need to stop the Foreach loop container from processing more files in the folder than desired. Scenario:  If I only want 1 files processed, i need to stop the loop after it finds 1 file.  No matter what the contraints or tests or variables I set, the loop processes all the files in the folder.  This is bad because I need the value of the 1st mapped variable and not the last one it finds. I have tried counting records and setting variables used in the contraints but to no avail  Nothing seems to stop the loop. Please advise

How to go to next iteration of ForEach Loop container on task failure

I'm using a ForEach Loop container to iterate through a set of files and import/validate the data.  Within each loop, the first thing that happens is that I fill a variable from a table and then use a script task to make sure that the rest of the package should be run for this particular file.  If it should continue, I set Dts.TaskResult = Dts.Results.Success, and if it shouldn't, I set it to Dts.Results.Failure.  My question is, how do I get it to exit the current iteration of the loop and continue to the next iteration?  The way it is now, the package just stops completely.  I set the FailPackageOnFailure property to False for the script task, but that doesn't make a difference.  I also tried pointing a failure precedence contstraint bacl to the loop container, but that's not allowed.  How can I make it just continue in the loop?   Thanks.

Using Foreach loop Container in SSIS 2005 package and scheduling the package using SQL job on 64-bit

I've an SSIS package 2005 which uses a for each loop container, this package runs fine when I run it on the local machine . My server is 64 bit SQL 2005 and I 've successfully deployed my package on the server both to the File system and SQL server. I've also set Run64bitruntime to false in my pacakge. Now I need schedule the package using SQL job. Since Microsoft Jet Provider 4.0 is not available for 64 bit, I had to write script to schedule the package. Here is my script. declare   @ssisstr varchar(8000) declare @returncode int set   @ssisstr = 'dtexec /sq Package1 /DE 123' EXEC   @returncode = xp_cmdshell @ssisstr select   @returncode I'm getting the following error when I execute the job. Could not load package "Package1" because of error 0xC0010014. Description: One or more error occurred. There should be more specific errors preceding this one that explains the details of the errors. This message is used as a return value from functions that encounter errors. I'm getting the same error if I run this from commandline. Any help would be really appreciated. Thanks in advance. PARC

Foreach Loop Container.ForEachEnumerator.Properties[FileSpec] Exception -This occurs when an attempt

I run into the following 2 exceptions when I execute a package on SQL Server 2005 64 bit machine. The package fails when SQL Server version is 9.0.3310 which is a production box. I have tested it on 3 others development boxes with SQL version 9.0.1399, 9.0.3315 & 9.0.4053 and the package executes fine. I am unable to make a determination if this issue is related to SQL versions or a bug in the package design. I would like to understand the exception better before I request an SP3 & patches installed to upgrade it to version 9.0.4053 on the production box. I have scheduled jobs to run these packages and we are trying to locate Excel files on a drive using For Each Loop Enumerator. The packages are configured to use configuration files. Please note the destination directory does have the Excel files we are looking for. Source: Well_Test_Package Description: The package path referenced an object that cannot be found: "\Package\Daily Alloc Spread Sheet Foreach Loop Container.ForEachEnumerator.Properties[Directory]". This occurs when an attempt is made to resolve a package path to an object that cannot be found.  End Warning  Warning: 2010-08-31 00:41:13.66     Code: 0x80012017     Source: Well_Test_Package      Description: The package path referenced an object that ca

C# Allowing user to select variables to use in a foreach loop

Hi guys, First project, first post. My app builds webpages and personalises the content by running a foreach loop through rows in a dataset. My original web page build was hardcoded and works fine. I want to take this one stage further and allow users to paste their own html into a text box and use button controls to insert values from the dataset arrays.  Doing this however leads to the app writing the string verbatim. Some examples at this point might help:  Original code here System.IO.StreamWriter objWriter; objWriter = new System.IO.StreamWriter(webpage);                     objWriter.WriteLine("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>"); objWriter.WriteLine("<head>"); objWriter.WriteLine("<title>" + row.ItemArray.GetValue(0).ToString() + "'s very own webpage" + "</title>";); objWriter.WriteLine("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"); objWriter.WriteLine(""); objWriter.WriteLine("</head>"); objWriter.WriteLine("<body>"); objWriter.WriteLine("<h1>Well hello there " + row.ItemArray.GetValue(5).ToStri

SSIS For Each loop with data flow hangs on DTC (SID -2) when inside transaction

The short story: I have a data flow inside a For Each container that is inside a sequence container.  A transaction is established at the sequence container, with Isolation Level Serializable.  The For Each container is limited to execute only 4 iterations.  The data flow iterates once fine, but the second iteration hangs.  The Activity Monitor shows the process is waiting on a another process with a SID of -2.  From other research in the past, I have found that this is an internal process dedicated to DTC (Distributed Transaction Coordinator). Environment: Microsoft SQL Server 2005 (SP3) on Windows 2008 R2 Standard.  I'm using SQL Server BIDS 2005 for building these packages.  I also have SQL Server 2008 installed and the databases are installed there. The details: The data flow merges data from the same table in two databases, source and target, using a Merge Join component.  It then performs a few transformations and lookups from other tables in the target DB to get IDs.  Then it inserts new records into the target DB or updates existing records in the target DB.  The two source tables are using OLE DB Source components with customized queries.  The insert target component is an OLE DB Destination and the update target component is an OLE DB Command.  In this particular scenario,

Could not set a datasource to Schema Rowset Enumerator in Foreach Loop Container



Everytime I want set a datasource (also if I create a new one with an click on the drop down) I got the following error message from the container.


TITLE: For each schema rowset

The new connection, LocalHost.ReportServer, is not an OLE DB connection so it can not be used by the schema rowset enumerator and, therefore, will not be added to the connections list.

But if I generate a OLE DB connection manually it does not appear in the dropdown list of the container. Does anybody know what I have to do in that case?

Thanks, Nils

foreach loop and SqlDataAdapter to update WHERE UserId = UserId ?


I'm trying to figure out how to update multiple rows in my Directory table, when a user changes their Street Number or Street Name in their profile.  They can add multiple users to a directory and each user they add to the directory has this primary users UserId in it. So if the primary user changes their address, I want it to update the address for all the users they listed in the directory as well. I'm adding this to the On_Click in the Code Behind, so when they click to update their profile, it fires this function as well.

This code may be way off, but here's what I have right now:

                //Update directory user addresses for this account, if profile address has changed
                if ((currentStreetNumber != creatingStreetNumber) || (currentStreetName != creatingStreetName))
                    string myDirectoryConnection = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                    SqlDataAdapter cmd2 = new SqlDataAdapter("Update Directory SET [StreetNumber] = @StreetNumber, [StreetName = @StreetName] WHERE [UserId] = @UserId", myDirectoryConnection);
                    cmd2.UpdateCommand.Parameters.Add("@UserId", currentUserId.ToString());

MySql Transaction inside a loop.


Hi.  I'm trying to delete a bunch of records in mysql.  The record info to delete (userid, name) is stored in an arraylist.  It seems to only delete the first record and gives an error ("Transaction has already been rolled back or is not pending.") afterwards.  I have the transaction commit inside the loop which I think is the issue but not sure how to go further if I bring it outside of the loop.  Here's the relevant bits of the code.  Thanks in advance.

                ArrayList alist = new ArrayList();

                //alist gets populated with data like (12345,robot)(23456,car) here.

                    MySqlConnection conn2 = new MySqlConnection(query_connection);

foreach loop - insert record in table



i createa a data flow task and read the records from a table to recordset.

now i inserted a foreach loop container which needs to go thru each record loaded in recordset from dataflow task. i made enumerator to foreach as ado enumerator and set the ado object source variable to recordset variable name from data flow task. also radiobox , rows in the first table is marked.

i also mapped the variable in foreach loop.


my question is how i can use these variable to insert variable values in antoher table?


mark it as answer if it answered your question :)

Foreach Loop Container - Excel



I´ve seen a few posts on this topic but I still can´t get my simple example to work. What I want to do is to loop thru all Excel files in a folder and put some of the data in my db. It works fine without the Foreach loop container. This is what I´ve done. First, I´m working on a Windows Server 2008r2 and against a SQL Server 2008r2. The Excel files are Excel 2007.

1) I created a new Integration Services Project.

2) I added a Data flow task to the Control flow tab.

3) I added two connection managers. One for the Excel file source and one for the destination db.

4) I added an Excel Source and a SQL Server destination and connected them. It works fine, I can preview the columns and the mapping is done correct.

When I run it, the db is populated.

5) This is where it fails. I added a Foreach Loop Container and drag the Data flow task into it. In the collection tab I choose Foreach file enumerator and browse to the folder.

5) Under the variable mapping tab I add a variable called FileName under the scope User and inddex 0.

6) Then I flipped to properties for the Excel Connection manager and enter the expression setting. I added a connection property with this expression "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @[User::FileName] + ";Extended Properties=\"E

foreach loop concern


b4 foreach loop, i have execute sql task and that calculate the distinct cust_num. Execute sql task wud return the rows something like this

cust_num      period
123456          Weekly
123456          Monthly

inside the foreach loop, i wud do the business logic i.e. i would find the period using execute sql task and next comes script task which does nothing, but i have precedence constraint editor and i would say, varReportPeriod == "Weekly" ---->do these steps and varReportPeriod =="Monthly" do these steps

now whats happening here is, the first execute sql task wud caluclate distinct cust_num (i.e. 123456 and Weekly) and it would go into the foreach loop and it sees weekly and goes to the respective logic and executes it, but it doesn't go to the 2nd record i.e. 123456 and Monthly and it would not perform the respective logic for monthly

the diagram is something like this

                      execute sql task
                      Foreach loop

foreach loop - delete file task


i have ado enumerator on foreach loop and one of the variable contains the file name. following are the task which get executed in for each loop but i'm getting error on delete file task

1. Run a script to create pdf file from ssrs (using filename and path in ado enumerator). Working great.

2. Send email with attached of above pdf file (working great)

3. Now in 3rd step, i want to delete the file, so used file system task and delete file . i'm getting following error

[File System Task] Error: An error occurred with the following error message: "The process cannot access the file 'p:\information technology\report output to send\eft\0000023744-EF_DEN1_10-29-10_EFT.pdf' because it is being used by another process.".

I'm not sure what is using the file that it is not letting me delete the file.

Any clue?

mark it as answer if it answered your question :)

ForEach Loop reads next file when child task fails


We have a ForEach loop that reads over a list of files in a directory.  We are placing the current file name into a parent variable and passing it into children packages through the Parent Package Variables configuration.  We have attached a Execute Package task for the ForEach loop Failure constraint.  The package is an exception handler that moves the current file (set in the ForEach loop variable mapping) to a failure folder.

The issue is that when the exception handler is called, the file name being passed in is the next file in the directory, not the file currently being processed.  Is this due to read ahead by the ForEach loop?  I pass this variable exactly the same way to child packages within the ForEach loop and it works fine.  It is just when I pass the variable to the constraint path from the ForEach loop that changes the value of the variable.  Is this because the exception handler sits outside of the loop container?

Thanks for your help.


Stopping a SSIS Foreach loop at a certain time.


I have a SSIS package that loops through ANSI 835 files, parses them, and posts the data to the appropriate database.  The issue I have is that sometimes this job can take some serious time and I need it over with at 6am (any remaining files can just wait until the next day).  It is unacceptable to use the SQL Agent to stop the package after 6am because the package may be in the middle of parsing a file and may only post partial data that our users would have to find and take time to clean up. 

Is there a way to include a script task in my Foreach loop such that when it tries to process the next file the Script Task checks the time and if the time is after, say, 6am it stops the loop and reports success?

I added a Script Task to our current package as a visual aid.  See screen shot in below link:



"If you have to ask about various operating system limits, you're probably doing something wrong." -Raymond Chen
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend