C# Razor The name does not exist in the current context - c#

I know I must be being stupid here, but I can't work out why this code isn't working. I'm very new to Razor, so please go easy on me.
I have the following code in my markup: (I've cut it down to be as simple as I can make it while still reproducing the issue so that I can hopefully diagnose the issue easier)
string testVar = "test";
#testVar
It returns the following:
error CS0103: The name 'testVar' does not exist in the current context
I've tried using different names for the variable, I've tried having it use "var" instead of "string" for it's declaration, I've tried assigning various different values to it, I've tried surrounding the variable in brackets like #(testVar), but the issue is still there. It's very frustrating, because in another part of my code I have
string prop = #p.Name + " (" + #p.PropertyType + ") - " + #p.GetValue(#page, null).ToString() + "\r\n";
#prop
which works fine.
I can't think what could be causing it, and it's starting to frustrate me.
Thanks,
YM

In Razor when we want to write c# statements we have to tell it that from here c# code starts:
#{ // from here c# block starts
string testVar = "test";
} // here ends
Now if you want to access this variable in html you can do like this:
<span>#testVar</span>
When you are writing:
string prop = #p.Name + " (" + #p.PropertyType + ") - " + #p.GetValue(#page, null).ToString() + "\r\n";
it is considered as plain text and will be rendered on browser like "string prop = ..."
you have to tell that it is c# code by following way:
#{
string prop = #p.Name + " (" + #p.PropertyType + ") - " + #p.GetValue(#page, null).ToString() + "\r\n";
}

Related

HDinsight Store Pig Result

I want to submit a pig job with the .NET SDK to HDInsight (but it also happens when I submit it with PowerShell, tested that already). Using following Statement:
string queryString = "REGISTER wasb:///PigTestFolder/myudfs.jar;" +
" A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage()AS Line:chararray;" +
" B =ORDER A BY Line asc;" +
" D= Limit B 20;" +
" Dump D;" +
" STORE D INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
When I remove the STORE command everything works perfectly fine. But Storing the result to the BLOB is a pain in the arse. It say in the error log: "AS was unexpected at this time". But if I remove the schema and define the column with $0 it says: B was unexpected at this time.
The following Code prints out: Dump unexpected at this time.
string queryString = " A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage();" +
" Dump A;" +
" STORE A INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
I tested the code with the Hortonworks Sandbox and it works. No problem. Another mystery is, when using only the following two lines of PigLatin code it works as well:
string queryString = " A = LOAD 'wasb:///PigTestFolder/wordlist.txt' USING PigStorage();" +
"STORE D INTO 'wasb:///PigTestFolder/results/' USING PigStorage ('\t');";
Does anyone have any idea what I am doing wrong?

Put Quote Mark in between 2 string objects

I have looked and looked and look for a way to put a " in between 2 string objects.
I know you can use "\"" to get a quote or even #"""" for the same result.
string quote = "\"";
string cheatName = "UnlockTankLine(" + nationNum.ToString() + "," + quote +
nationTankName[1] + quote + ")";
m_cheater.ActivateCheat(cheatName);
I need a result of "UnlockTankLine(int, "name")"... but when i do the above i get something like
"UnlockTankLine(int, \"name\")" and this isn't working with a cmd line for our game.
NOW if i am dumb and \"name\" is the same thing as "name" and the problem might be somewhere else. The only reason why I think i am not being dumb is if i use a different cheat cmd that doesn't take a string it works fine. Example UnlockWholeTankLine(int) works
try
to use format
string cheatName = string.Format("UnlockTankLine({0},\"{1}\")",
nationNum.ToString(), nationTankName[1])

Write html anchor text (with name / value pair) to a string

I think I can't see the forest for the trees here. I want to write a string. Using Linq to SQL I have created a result and I'm looping through it to dynamically write anchor tags.
But, the code is producing this:
<a 45="" href="ADappointment.aspx?openingid">My person booked< /a >
I want:
<a href="ADappointment.aspx?openingid=45">My person booked< /a >
Here's what I'm doing:
foreach (var anOpening in results)
string sFlag = #"";
sFlag = #"<td>" + patient.FirstName + " " + patient.LastName + " accepted </td>";
...
What am I doing wrong?
You have quotes in your href value the browser doesnt expect.. so it is rendering it completely incorrectly.
You are producing this:
href="urlhere.aspx?id="99""
Note the quotes around the ID. Remove those from your code. You want something like this:
sFlag = #"<td><a href=""ADappointment.aspx?openingid=" + anOpening.OpeningId + """>" + /* the rest here */
Ideally you would use a library to do this. There is a TagBuilder class in the MVC assembly.
Use String.Format to clearly format string.
foreach (var anOpening in results)
{
var sFlag = String.Format(#"<td>{1} {2} accepted </td>", anOpening.OpeningId, patient.FirstName, patient.LastName);
}

Replace this " " with this " " using C#

I am having an issue with this blank space " " showing up in my textbox if the table row is null. so i would like to replace this " " with this "". For example here is what i have:
StartTime.Text = row.Cells[5].Text;
so here is pseudo-code of what i am trying to achieve:
if (StartTime.Text == "" " then replace it with " ")
else show the value of StartTime.Text
I know my C# skills is so bad so please help. thanks
what about StartTime.Text = row.Cells[5].Text.Replace("&nbsp", " ")
or i haven't understood the question correctly

String concatenation doesn't seem to work in C#

I don't know what is wrong with the following string:
"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
I can't get the concatenated string. I am getting Report(29-Dec-2009. That's all and
the rest gets left out from the string.
What is the reason?
Try this:
string filename =
String.Format(
"Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
System.DateTime.Now, System.DateTime.Now.AddMonths(-1));
EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:
filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";
Seems some browsers doesn't handles whitespaces very nicely: Filenames with spaces are truncated upon download. Please check it you can to download other filenames with whitespace in other sites.
You need to assign it to something:
string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
Update: I just saw your update to the question. How are you displaying the string? I'm guessing that you are displaying it in a GUI and the label is too short to display the complete text.
Try this:
string newstring =
string.Format(
"Report ({0} to {1})",
System.DateTime.Now.ToString("dd-MMM-yyyy"),
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
);
What are you assigning the result to? It would be easier to read the code if you used string.Format
You are not assigning the concatenated result to anything, so can't use it:
string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";
Using this code...
string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";
I saw the following result.
Report(29-Dec-2009 to 29-Nov-2009)
It could be that the string is being truncated later on. Make sure that you set a breakpoint right after this code is run and check the value of the variable to which it is assigned (test in my case).
If, as in your previous question, you are using this value to create a file, it may be that it's the space before "to" that is causing the problem. Try to use:
"Report("
+ System.DateTime.Now.ToString("dd-MMM-yyyy")
+ "To"
+ System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
+ ")"
instead and see if that fixes it.
If that does fix it, you'll probably need to either figure out how to quote the entire file name so it's not treated as the three separate arguments, "Report(29-Dec-2009", "to" and "29-Nov-2009)". Or simply leave your reports names without spaces.
I'd choose the latter but then I'm fundamentally opposed to spaces in filenames - they make simple scripts so much harder to write :-)

Categories