How to use Logical operator OR " || " in ASP .NET - asp.net

I've followed this question How to use IF statement in asp.net using C#, but I really can make it out with my code.
if ((txtNome.Value == null) || (txtNome.Value == (""))
{
}
Here is the error
The "||" operator cannot be applied a bool and string operands
I've tried all the possible solutions in the question above, but still not working. Some ideas?
Thank you

Solution 1
if ((txtNome.Value == null) || (txtNome.Value == ""))
{
}
Solution 2
The same as above but without the extra round brackets.
These are unnecessary for single logical statements.
if (txtNome.Value == null || txtNome.Value == "")
{
}
Solution 3
Built in function for the above in C#
if (string.IsNullOrEmpty(txtNome.Value))
{
}

Related

ASP.NET Converting & to &amp. I need plain text

I've looked around for a while for an answer but no one has one and everyone keeps saying it's how it's supposed to work.
I need to render an & (ampersand) as plain javascript code. Not as a string.
if (#(Model.Month == null ? "now.getMonth() == tooltipItems[0].index" : "now.getMonth() == tooltipItems[0].index && now.getDay() == tooltipItems[0].index") && now.getFullYear() == $('#DistinctYears').val()) {
//
} else {
//
}
I need this section:
"now.getMonth() == tooltipItems[0].index && now.getDay() == tooltipItems[0].index"
To render as plain javascript code but when it renders, the ampersands automatically get converted to &&
Surround the entire ternary expression with Html.Raw():
#Html.Raw(Model.Month == null ? "now.getMonth() == tooltipItems[0].index" : "now.getMonth() == " + (Model.Month - 1) + " && now.getDate() == (tooltipItems[0].index + 1)")

Is there an alternative to writeFields?

This article stated that "[writeFields] is now deprecated".
Additionally, I cannot find any documentation for writeFields, it is not even listed as part of Request in the documentation anymore.
Problem
The problem I am facing with Cloud Firestore Security Rules is that verifying that only particular fields are modified requires massive amounts of conditions.
For example, if I want to verify that the only modified value of a document is cakes, I have to write the following rule:
allow update: if request.resource.data.size() == 20
&& request.resource.data.likes == resource.data.likes
&& request.resource.data.name == resource.data.name
&& request.resource.data.date == resource.data.date
&& request.resource.data.body == resource.data.body
&& request.resource.data.title == resource.data.title
&& request.resource.data.tags == resource.data.tags
&& request.resource.data.comments == resource.data.comments
&& request.resource.data.answers == resource.data.answers
&& request.resource.data.awards == resource.data.awards
&& request.resource.data.image == resource.data.image
&& request.resource.data.link == resource.data.link
&& request.resource.data.format == resource.data.format
&& request.resource.data.type == resource.data.type
&& request.resource.data.user == resource.data.user
&& request.resource.data.views == resource.data.views
&& request.resource.data.reports == resource.data.reports
&& request.resource.data.roles == resource.data.roles
&& request.resource.data.category == resource.data.category
&& request.resource.data.votes == resource.data.votes
&& request.resource.data.cakes is int;
Using writeFields, the exact same rule would have looked like this:
allow update: if request.writeFields.hasOnly(['cakes']) && request.resource.data.cakes is int;
What can I do to decrease the code size of my rules / what is the alternative to writeFields?
Limits
There are two limits mentioned in the documentation that make this problem even worse:
Maximum number of expressions evaluated per request: 1,000
Maximum size of a ruleset: 64 KB
I expect to reach both of these at some point with this limitation.
Yes! There is now a replacement called "Map Diffs". Check this syntax out:
allow update: if request.resource.data.diff(resource.data).affectedKeys().hasOnly(['cakes'])
&& request.resource.data.cakes is int;
Unfortunately, what you're doing right now is currently your best option.
The Firebase rules team is working on a language improvement to make it easier to compare/diff map type objects, which will drastically cut down on the number of expressions it takes to do this sort of thing, but there is no timeline for that right now. Please stay tuned.
I end up writing these helper function:
// returns true if all the fields changed are contained in the array parameter
function onlyAllowedFields(fieldsArray) {
return fieldsArray.toSet().hasAll(request.resource.data.diff(resource.data).affectedKeys());
}
// returns true if none of the fields are changed
function noneOfDisallowedFields(fieldsArray) {
return request.resource.data.diff(resource.data).affectedKeys().hasAny(fieldsArray.toSet()) == false
}
I found more helpful this way instead of using hasOnly() that would require that all fields that could be changed must have been changed.

Does ternary operator can only do one kind of condition?

ternary make code concise and readable, I'm curious about how to change the following if condition to ternary operator:
var1 = if(true){'a'};
I try the following
var1 = true? 'a': ;
since it require nothing to do with false condition, I leave blank after :, but apparently it gives me a error.
Is there a way to do this?
--------update---------
The intention of using the above example is that I want to simplify the problem, however it made everyone more confuse, so I post my original code:
if($_SERVER['REQUEST_METHOD'] == 'GET'){ $sub_count = 0; }
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : ;
how to change the if condition to ternary ?
$sub_count = null;
$sub_count = $_SERVER['REQUEST_METHOD'] == 'GET'? 0 : null;
// To check:
if(!isset($sub_count))
{
// Do something because $_SERVER['REQUEST_METHOD'] != 'GET'
} else {
if($sub_count===0)
{
// REQUEST METHOD IS GET
}
}

Trouble checking if url param exists

I'm having a bit of trouble trying to find if my url parameters exist or not.
I have tried the following:
// doesn't work
(Request.QueryString["showTop"] != "" && Request.QueryString["showTop"] != null)
// doesn't work
(Request.Params["showTop"] != "" && Request.Params["showTop"] != null)
I am trying to find the correct value. The full statement looks like:
showTop = (Request.QueryString["showTop"] != "" &&
Request.QueryString["showTop"] != null) ?
Request.QueryString["showTop"] : (10).ToString();
Which works fine, if showTop exists with a value.
This is being done within the view.
Try the following:
showTop = string.IsNullOrEmpty(Request["showTop"]) ? "10" : Request["showTop"];
Assuming you want "showTop"to default to "10".
First check whether QueryString has keys or not by calling this method.
bool qKeys = Request.QueryString.HasKeys();

Detect 0 rows in a Telerik ASP.NET MVC Grid

What is considered the best practice for determining whether there are any rows bound?
Currently, I'm using the client-side OnDataBound event, and code similar to the following:
gridDataBound: function (event)
{
var rows = $('tbody tr:has(td)', this);
if (rows.length == 0 || (rows.length == 1 && rows[0].innerText == "No records to display'))
$('#GridSection').hide("slow");
}
There has got to be a better way!
I can suggest a shorter version:
if ($(this).find(".t-no-data").length) {
$("#GridSection").hide("slow");
}
Ah, a few minutes poking around and I think I have a solution that really feels better-
if ($("tbody tr:has(td).t-no-data", this).length != 0) {
$("#GridSection").hide("slow");
}
$('#grid-name').data('tGrid').data is an array of all of the records.
So, you can get the number of records using:
$('#grid-name').data('tGrid').data.length;

Resources