I'm trying to set a value in a foreach but i get the error that it dosent exist in the current contect.
Here is my code;
#{
string sUsr = System.Web.HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToLower();
var db = Database.Open("SQLServerConnectionString");
var selectUser = "SELECT * FROM USERS WHERE username = " + #sUsr;
}
#foreach (var userid in db.Query(selectUser))
{
var sReg = userid.userdisplayname;
var sAns = userid.usersign;
var smail = userid.usermail;
}
...some html code etc.
#sReg <---- The name sReg dosent exist in the current context
If i try with the folowing code it works and Hello World is rendered in my webpage
#{ var myMessage = "Hello World"; }
...some html code etc.
#myMessage
Change your code, so that sReg is defined outside the scope of the foreach loop. You can do this like:
var sReg;
#foreach (var userid in db.Query(selectUser))
{
sReg = userid.userdisplayname;
var sAns = userid.usersign;
var smail = userid.usermail;
}
You can now use the (last) value of sReg outside the loop's body.
Variables defined in code only have a limited area in which they are available, which is commonly called the "scope" of a variable.
Read this article to learn more about scope in C#.
Please be also aware of the fact that you may get multiple results from your query (unless you are selecting with top 1 or such). In that case, your sReg variable would only contain the value of the last iteration, which may not be desirable for you. In this case, a List could come to the rescue: http://www.dotnetperls.com/list
sReg in this context only applied inside your foreach loop.
The reason you're getting the doesn't exist in current context error is because it's not in scope.
If you wanted to proceed and do something with your var sReg you would have to perform whatever operation you would like to do within the loop, or declare it outside if need be.
Hope this helps
Related
I am trying to give my ComponentID a new value when iterating through my list this is how I currently set it up
#{Component subComp = db.Components.Find(Model.ComponentSubComps[c].ID);}
#for (int sp = 0; sp < subComp.CHP.Count; sp++)
{
Part sPart = db.Parts.Find(Model.SubCompParts[sp].ID);
#Html.HiddenFor(x => x.SubCompParts[sp].PartID)
#Model.SubCompParts[sp].ComponentID == #subComp.ID;
}
As you can see I try assigning the value by doing this
#Model.SubCompParts[sp].ComponentID == #subComp.ID;
//also tried this #Model.SubCompParts[sp].ComponentID = #subComp.ID;
But this isn't working. How can I properly assign the value?
Two equal signs are equality, not assignment. Also, you are already inside a code block but are switching contexts when you call the #Html.HiddenFor method. Change this:
#Model.SubCompParts[sp].ComponentID == #subComp.ID;
To this:
#{ Model.SubCompParts[sp].ComponentID = subComp.ID; }
There are several other things worth noting in your code but I will refrain from commenting on those.
So I'm working on learning LINQ and was assigned to work with two .txt files and to join them.
So far I'm doing well, but I've reached a bit of an impasse with the display. I'm supposed to have the name display once and then the following cases that are closed have only the case information.
The issue I'm having is that the name keeps repeating after the dataset is listed in the ListView. I think there is something wrong with the LINQ statement or the way I'm going through the foreach loop. Here is the code for the main form below:
//Fills the lists by calling the methods from the DB classes
techs = TechnicianDB.GetTechnicians();
incidents = IncidentDB.GetIncidents();
//Creates a variable to use in the LINQ statements
var ClosedCases = from Incident in incidents
join Technician in techs
on Incident.TechID equals Technician.TechID
where Incident.DateClosed!= null
orderby Technician.Name, Incident.DateOpened descending
select new { Technician.Name, Incident.ProductCode, Incident.DateOpened, Incident.DateClosed, Incident.Title };
//variables to hold the technician name, and the integer to increment the listview
string techName = "";
int i = 0;
//foreach loop to pull the fields out of the lists and to display them in the required areas in the listview box
foreach (var Incident in ClosedCases)
{
foreach (var Technician in ClosedCases)
{
if (Technician.Name != techName)
{
lvClosedCases.Items.Add(Technician.Name);
techName = Technician.Name;
}
else
{
lvClosedCases.Items.Add("");
}
}
lvClosedCases.Items[i].SubItems.Add(Incident.ProductCode);
lvClosedCases.Items[i].SubItems.Add(Incident.DateOpened.ToString());
lvClosedCases.Items[i].SubItems.Add(Incident.DateClosed.ToString());
lvClosedCases.Items[i].SubItems.Add(Incident.Title);
i++;
}
And here is the result I get: Result
As can be seen by the bar on the right hand side, the list continues on for several more columns.
What am I missing here?
Thank you.
EDIT: Per request, here is what the results are supposed to look like:
The example I was given
Why you are iterating closed cases twice?
foreach (var Incident in ClosedCases)
{
foreach (var Technician in ClosedCases)
{
}
}
Good evening,
I am trying to get the following done. I have seen a similar post but it was related with Unity.
Anyway, I am on web forms in asp.net and I have a radiobuttonList with ID="id001"
so on my code behind, I would normally be able to get the selected value by just doing:
string value = id001.SelectedValue
However, in this situation, I don't know the exact ID name, so I have a function that retrieves it. So now I have the variable with the name of the ID. So I want to be able to now, convert the value of that variable in something like this:
string foundid = "id001"
string foundidvalue = id001.SelectedValue
I hope this makes sense.
Thanks in advance for the help.
I am assuming this one is related to your previous question. So, when you found the control, instead of using function to get the fullname, you can do like this:
foreach (Control c in Page.Form.Controls.OfType<RadioButtonList>())
{
if (c.ID.Contains("id"))
{
// string FullID = c.ID.ToString();
var radioButtonList = c as RadioButtonList;
var selectedValue = radioButtonList.SelectedValue;
}
}
You want to use FindControl.
string foundid = "id001";
var foundCtrl = (RadiobuttonList)FindControl(foundid);
var result = foundCtrl.SelectedValue;
This is a part of the code that i was trying to use to get the respective elements, but it keeps giving me the following error:
System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]or
others identical
This is also shown in a datagridview, in her rows.
IList<IWebElement> ruas = Gdriver.FindElements(By.ClassName("search-title"));
String[] AllText = new String[ruas.Count];
int i = 0;
foreach (IWebElement element in ruas)
{
AllText[i++] = element.Text;
table.Rows.Add(ruas);
}
First thing is: as far as I understand the elements you are talking about are not contained in table. Its a list: <ul class="list-unstyled list-inline">... (considering the comment you left with site link)
If you want to find those elements you can use the code below:
var elements = driver.FindElements(By.CssSelector("ul.list-inline > li > a"));
// Here you can iterate though links and do whatever you want with them
foreach (var element in elements)
{
Console.WriteLine(element.Text);
}
// Here is the collection of links texts
var linkNames = elements.Select(e => e.Text).ToList();
Considering the error you get, I may assume that you are using DataGridView for storing collected data, which is terribly incorrect. DataGridView is used for viewing data in MVC application. There is no standard Selenium class for storing table data. There are multiple approaches for this, but I can't suggest you any because I don't know your what you are trying to achieve.
Here is how i answered my own question:
IList<string> all = new List<string>();
foreach (var element in Gdriver.FindElements(By.ClassName("search-title")))
{
all.Add(element.Text);
table.Rows.Add(element.Text);
}
I am pulling in a Dapper FastExpando object and want to be able to reference the column names dynamically at run time rather than at design/compile time. So I want to be able to do the following:
var testdata = conn.Query("select * from Ride Where RiderNum = 21457");
I want to be able to do the following:
foreach( var row in testdata) {
var Value = row["PropertyA"];
}
I understand that I can do:
var Value = row.PropertyA;
but I can't do that since the name of the property i'm going to need won't be known until runtime.
The answer from this SO Question doesn't work. I still get the same Target Invocation exception. So...
Is there any way to do what I want to do with a Dapper FastExpando?
Sure, it is actually way easier than that:
var sql = "select 1 A, 'two' B";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row["A"].IsEqualTo(1);
row["B"].IsEqualTo("two");
Regarding the portion of the title "or index?" - I needed to access results by index since the column names being returned changed sometimes, so you can use a variation of Sam Saffron's answer like this:
var sql = "select 1, 'two'";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row.Values.ElementAt(0).IsEqualTo(1);
row.Values.ElementAt(1).IsEqualTo("two");
There a simple way to access fields direct below sample
string strConexao = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;
conexaoBD = new SqlConnection(strConexao);
conexaoBD.Open();
var result = conexaoBD.Query("Select Field1,Field2 from Table").First();
//access field value result.Field1
//access field value result.Field2
if (result.Field1 == "abc"){ dosomething}