Cannot implicitly convert type "string" to "bool" when using if condition? - asp.net

When I write if condition in one line for the textbox i.e.
if (txtNotes.Text.Equals(" ") ? string.Empty: gvrow.Cells[6].Text)
I am getting the error stating:
Cannot implicitly convert type "string" to "bool"
Just want to check where I am going wrong.

You're confusing the standard if statement syntax with the ternary operator ?:. It's either:
txtNotes.Text = txtNotes.Text.Equals(" ") ? string.Empty : gvrow.Cells[6].Text;
or
if (txtNotes.Text.Equals(" "))
{
txtNotes.Text = string.Empty;
}
else
{
txtNotes.Text = gvrow[6].Cells.Text;
}
Edit: from your comment you've stated your setting the value of txtNotes.Text, so I recommend using the ternary operator to achieve this.

Related

XQuery Invalid entity reference error caused by "&" entity reference

I'm trying to run this line xdmp:unquote(concat('<info>', string( $paragraph) , '</info>')) but I've got the following error: xdmp:unquote("<info>LEARNING & MEMORY</info>") -- Invalid entity reference " " at line 1. It seems like this entity reference & is causing the problem. I tried to remove it using replace function but it still present. What should I do?
I am assuming that you have something like this-
let $paragraph := <p>LEARNING & MEMORY</p>
return
xdmp:unquote(fn:concat('<info>', fn:string($paragraph),'</info>'))
And that the result you want is XML that looks like-
<info>LEARNING & MEMORY</info>
The ampersand is definitely the issue and the workaround is to use the "repair-full" option. This example works:
let $paragraph := <p>LEARNING & MEMORY</p>
let $contents := xdmp:unquote($paragraph, "", "repair-full")
return
<info>{$contents}</info>

Groovy Map Issue with Variable Properties and String INterpolation

I have been navigating map structures fine for a long time now. Yet, for some reason, the root of this problem escapes me. I've tried bracket notation as well, no luck.
Why doesn't the final output (null) return "[serverinfo:[listenPort:19001]]"
If I replace the two instances of ' "$instanceName" ' with simply ' services ', it works.
String instanceName = "Services"
Map serverNode = [
instances:[
"$instanceName":[
serverinfo:[
listenPort:19001
]
]
]
]
println "$instanceName"
println serverNode.instances
println serverNode.instances."$instanceName"
//output
Services
[Services:[serverinfo:[listenPort:19001]]]
null
The type of "$instanceName" is GStringImpl, not String. It's a common mistake (and hard to find!)
def serverNode = [
instances:[
("$instanceName" as String):[
serverinfo:[
listenPort:19001
]
]
]
]
as stated by #tim_yates in comment, if your interpolated string is as simple as in this example (ie ,"${property}"), then you can use the (property) syntax : Groovy put the value of the property as a key, not the word "property"

Using ternary operator to output a string containing whitespace in Razor

I'm trying to use a ternary operator in Razor, similar to this question, but what I want to output contains whitespace. This code
#(selectedGoal == null ? "" : "value=" + selectedGoal.Name)
should produce
value="Goal 3"
as the value of selectedGoal.Name is "Goal 3". Instead, I get
value="Goal" 3
which is no good. I've tried a bunch of different combinations of escaped quotes, # symbols and no # symbols, and I just can't get this to work, i.e.
#(selectedGoal == null ? "" : "value=" + "selectedGoal.Name")
#(selectedGoal == null ? "" : "value=#selectedGoal.Name")
and then I just get something like
value="selectedGoal.Name"
Anyone know how this should be done?
Your value attribute is missing its own quotes, so they are being automatically added before the space. Try moving value outside of the expression.
value="#(selectedGoal == null ? "" : selectedGoal.Name)"
What about
#(selectedGoal == null ? "" : "value=\"" + selectedGoal.Name + \")
Or you can try rendering them directly as an HTML block, using my method on
Html literal in Razor ternary expression

Error 1123: Filter operator not supported on type object

I have a a structure like this:
e.item.fatturato_ac_s1
e.item.fatturato_ac_s2
e.item.fatturato_ac_s3
e.item.fatturato_ac_s4
[...]
and so on...
in order to compute dinamically the string I wrote:
e.item.(myStr.toString()) where myStr (type string) = "fatturato_ac_s" + Index (so I can have fatturato_ac_s1, fatturato_ac_s2, ...)
I can correctly retrieve the value of e.item.(myStr.toString()) (a numeric value), but if I try to put it in a variable I get the error in the title:
myVariable = e.item.(myStr.toString())
myVariable is a Number.
I also tried:
myVariable = Number(e.item.(myStr.toString()))
but doesn't work... the same if I try String to String....
How can I solve it!?!!?
thank you!
This is correct syntax:
myVariable = Number(e.item[myStr])

ASP conditional error

i am trying to use an ASP conditional here:
if (Request.Cookies("username")) and
(Request.Cookies("password")) <> ""
Then
And i keep getting this error:
Type mismatch: '[string: ""]'
Any ideas what I am getting that?
try
if (Request.Cookies("username") <> "") and (Request.Cookies("password") <> "") Then
Actually, I would do the following..
if (!string.IsNullOrEmpty(Request.Cookies("username")) &&
!string.IsNullOrEmpty(Request.Cookies("password")))
{
// Do your stuff, here :)
}
Get into the habit of using string.IsNullOrEmpty for testing variables and string.Empty for setting values, if u don't want a string to be null.

Resources