C# - Using A Variable From A Foreach Loop?
Dec 6, 2010
i'd like to be able to check to see if an item with the same id has already been placed in the database, if so to then update the quantity for that item, however due to the fact I have this in a foreach loop it will update the quantity for each item.
When I placed the Command outside of the loop I am unable to use 'ItemID' as it's not in context, is there anyway I can get around this?
foreach (UserItem ItemID in (List<UserItem>)Session["UserSession"])
{
ConclusionPage.InsertCommand = "IF EXISTS (SELECT ItemID FROM tblUserItems WHERE UserID='@CurrentUser' AND ItemID='@ItemID') UPDATE tblUserItems SET Quantity = Quantity+1 WHERE (UserID = '@CurrentUser') AND (ItemID = '@ItemID')";
ConclusionPage.Insert();
}
View 4 Replies
Similar Messages:
Mar 9, 2010
I'm using a foreach to loop through an IList of objects in a Partial View in ASP.NET MVC.
Here's the entire code (Message is one of my classes from the Model).
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<Message>>" %>
<%
if (ViewData.Model.Count > 0)
{
foreach (MvcTest.Models.Message m in ViewData.Model)
{
Response.Write(m.RenderHtml());
}
}
ie explicitly declaring a variable for the IList rather than just using the (strongly-typed) Model, and it works fine.
View 1 Replies
Dec 21, 2010
in my asp.net mark up I have a foreach loop that iterates through a simple list. In this foreach loop I am adding a new user control and attempting to pass in the value from the loop. However, this value just wont budge and get inside that damn control!
<%foreach (userInfo i in this.items)
{ %>
<uc1:ItemControl ID="ItemControl" runat="server" UserID='<%#Eval("userID") %>'/>
<%} %>
userID is a public property in the control, when it goes to set, the value is just literally :
<%#Eval("userID") %>. I've tried #Bind and =Value but nothing seems to work.
View 2 Replies
Feb 21, 2011
I am trying to do some basic. Display customer testimonials in a table using foreach loop and have a checkbox on each row. Once checked, update the table.
I have a stored procedure of complex type in my entity framework. Here is my View.
[Code]....
Here is my model
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
[Code]....
View 5 Replies
Jan 5, 2010
I had the following code to send different mails to differnt users in my asp.net web application
foreach (DataRow dataRow in dataTable.Rows)
{
sendMails();
[code]...
View 8 Replies
Feb 14, 2011
List<string> IDs = new List<string>();
XDocument doc = XDocument.Parse(xmlFile);
var query = from c in doc.Root.Elements("a").Elements("b")
select new { ID = c.Element("val").Value};
How can I convert query to List without loop foreach ?
View 2 Replies
Jul 21, 2010
foreach (GridViewRow row in gridView.Rows)
{ // Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("SuburbSelector");[code]....
I tried the following and got error
Linq:
var Str = SuburbGridView.Rows.Cast<GridViewRow>().Where(r=>(CheckBox)r.FindControl("SuburbSelector")==checked);
Error:
Delegate 'System.Func < System.Web.UI.WebControls.GridViewRow,int,bool>' does not take 1 arguments
View 3 Replies
Sep 6, 2010
this is my code
[code]....
basically, the InstellingGegevens table gest filled in by some procedure from another server.
the thing i then need to do is check if there are new records in this table, and fill in the new ones in Instellingens.
this code runs for like 4 minutes on 15k records. how do I optimize it? or is the only way a Stored Procedure?
this code runs in a timer, running every 6h. IF a stored procedure is best, how to I use that in a timer?
[code]....
View 3 Replies
Mar 10, 2010
I have a ImageButton in a for loop
[Code]....
How do I set the CommandArgument? The above code does not work and how to do this.
View 1 Replies
Apr 23, 2010
I am saving for each user a session with information about his current page viewing.
Each time the user is moving to a new page, I am updating the session with the new page he is right now.
Now, my question is if I can make any loop over all of my users session and answer a question like: How many users are now on page "2.aspx" for example?
View 2 Replies
Mar 11, 2011
How can I continue to the next row in the foreach loop below.
[code]....
View 9 Replies
Apr 27, 2016
I want to get Array Elements by ForEach loop with Counter ..
whislist = dsddsds.Tables[0].Rows[0]["wishtlist_clg"].ToString().Split(',');
int i = 0;
foreach (string id in whislist) {
if (i != 0) {
tarsk.Value="1";
} else {
tarsk.Value="0";
}
whislist =525,1315,1331;
TARSK is an hidden field....
I want if whitelist is not in blank then 1 not equal to 0 is True other false but yet false condition is fire....
View 1 Replies
Sep 2, 2010
I have added the below tag in my web.config file;
<RewriterConfig configSource="configuration
ewriter.config"/>
It gets the rewrite vlues from another config file. I need to update this rewriter.config file dynamically.
I need to gets values inside it from sql databse. here is the look of the file
<?xml version="1.0"?>
View 3 Replies
May 5, 2010
as a php developer ive been told to go the MVC route which ive been trying since yesterday. Im using visual web delveloper 2010 express and im using the MVC template project.Ive basically got a news table in MSSQL Express, in there are these fields ID,(Title,Body,PostedOn,PostedBy) all i wanna do is loop through them to put them on the view.Im after a nudge in the right direction, I want to be able to do a SQL lookup in the controller put that data in a DataTable then run a foreach loop on the table to display this on the view. The problem im getting is I know youre not suppsed to put any HTML in the controller and I cant pass the datatable to the view to do the loop in the view.. so where/how do I do the loop.
View 2 Replies
Jan 14, 2010
Im stuck with declaring a string which is in a loop within another loop.
Dim CompanyDetails As String = ""
Dim CompanyRow As DataRow
For Each CompanyRow In newdt.Rows
CompanyDetails += CompanyRow(1) & " " & CompanyRow(0) & "<br/>"...
How can I get this to see the GetInfo as declared..... since its in a loop within a loop?
View 9 Replies
Jan 12, 2011
have 5 datatables and will add up more, what i need is to copy the content(rows) of the each and every datatable in a separate datatable. All the datatable columns are same. Is that possible without foreach statements. We can achieve the results in foreach
but the datatables will contain more rows, so iterating each and every datatable will affect the performance.he code i implemented is exact thing i need, but without foreach statement. To copy content of each and every datatable and put the content in a final datatable
DataTable dat = new DataTable();
DataRow Dr = dat.NewRow();
dat.Columns.Add("c1", typeof(string));
Dr[0] = "01";
[code]...
View 3 Replies
Jun 14, 2010
My code for splitting the strings is working however, i still need to use the splitted string my page. How can i achieve this? Here's my current code
private void SplitStrings()
{
List<string> listvalues = new List<string>();
listvalues = (List<string>)Session["mylist"];[code].....
how can i replace the response.write with any label or literal? when i tried to use a literal on the code it displays one single string not all of the strings that's been splitted.
View 3 Replies
Sep 24, 2010
I have a static class with a static get property, and in this property, I do this:
// property body
{
// HttpContext.Current is NOT null
Parallel.ForEach(files, file =>
{
// HttpContext.Current is null
var promo = new Promotion();
});
// HttpContext.Current is NOT null
}
This static class doesn't undergone type initialization until a view uses this property.
The problem is that Promotion's static constructor, which is initialized the first time a new Promotion() is created within the Parallel.ForEach(), uses HttpContext.Current. When promo is instantiated within the scope of this Parallel.ForEach(), HttpContext.Current is null, and new Promotion() therefore causes an exception.
HttpContext.Current is not null within the static get property because it's not called until the view uses it (and there is therefore a HttpContext.Current).
If Promotion used HttpContext.Current in its instances instead of its static members, I could probably just pass HttpContext.Current into the new Promotion() constructor:
var context = HttpContext.Current;
Parallel.ForEach(files, file =>
{
var promo = new Promotion(context);
});
But since static members of Promotion need HttpContext.Current, I can't. I could probably redesign the Promotion class to change the static members that need it to be instance members, but they are static for a reason--there would be a large performance penalty if all the members that were static had to be defined instead on each instance each time a new Promotion was instantiated.
What are the possible workarounds for this? I didn't realize HttpContext.Current would be null within the scope of Parallel.ForEach().
View 2 Replies
Mar 29, 2011
What am I missing here. I just want to pull the first one returned from row.detail in an MVC2 view, how would i do this without a foreach loop? This code currently works, but i just want the first one listed. I know this may be simple, but i am drawing a blank.
if (Model.App != null)
{
foreach (var row in Model.App.Instructions)
{
<input type="hidden" value="<%= row.Detail %>" id="ixd" />
}
}
View 4 Replies
Jun 11, 2010
I'm close to the solution I see where I am going wrong just unsure how to approach it. I want to be able to take a tables worth of data (via DataClasses) and put it in a CSV to download.
A LINQ to SQL query is performed (basically a 'Select All' from tbl_Newsletter), takes those results and the idea is to feed those results, one by one through the foreach which, inside it, is the CSV's definition e.g. List.Add(etc) as you'll see. After which the CSVExporter method is called - which works, as I can Download it, it just wont display any results.
C#: [Code]....
To which I get: Compiler Error Message: CS1729: 'Newsletter' does not contain a constructor that takes '2' arguments
with Newsletter obj = Newsletter.GetAll(); in place of Newsletter obj = new Newsletter();
I get: Compiler Error Message: CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<Newsletter>' to 'Newsletter'
View 5 Replies
Mar 10, 2010
how to do do some action recurting each row? My data looks like
[Column1] [Column2]
item 1 10
item 2 5
I would like to do something like..
foreach row in Column1 <-- how would I do this part??
While (row.Column2 > 0)
BEGIN
.. do something
row.Column2 = row.Column2-1
END
View 3 Replies
Mar 23, 2011
I did a small experiment to test whether lamdba expression can retrieve faster results than foreach statement. but, Lambda failed
[Code]....
why lambda is slower than foreach. This may be a drawback of lambda expression
View 3 Replies
Apr 1, 2010
I'm using Entity to SQL to retrieve some data from the database and i'm using it to fill an DataList.The query bellow is correct and working properly. My problem is when i try to convert it to an List.I've tried with foreach(EncomendasProdutos ec in produtos)but with no sucess ("Cannot convert type 'AnonymousType#1' to 'cenatolim.EncomendasProdutos'").
Is it because i'm joining tables? I need to save the values of some fields like IVA and Final from all products of the result, but i don't know how.[Code]....
View 1 Replies
Jun 2, 2010
I have:
[Code]....
How can I show it in view ? It:
[Code]....doesn't work.
View 3 Replies
Jul 20, 2010
Is it possible to select multiple entiies in ForEach Extension Method?
(i.e)
partial code is given
DataTableA.AsEnumerable().ToList().
ForEach(x=>
{
x.SetField<string>("Name","Jon Skeet"),
x.SetField<string>("msg","welcome")
});
when i apply multiple selection in ForEach
x=>
{
x.SetField<string>("Name","Jon Skeet"),
x.SetField<string>("msg","welcome")
}
I am unable to complete the statement.
View 1 Replies