Handling null strings and ints in Json - asp.net

JSON ignores any parameters with null values. So, when I create a string using JsonConverter.ExportToString these properties are missing. Also any integers with null values are replaced with -2147483648
This becomes an issue when I try to deserialize this string (I am writing my own deserializer and not using Json.Import)
What's the best way of handling this?

I am not sure that I understand the question. JSON is just a subset of javascript and properties with null values can be represented like so:
{"property1": 1, "property2": null}
In this case, property1 is a numeric and had value 1, while property2 has value null. I'm not sure from which library the Json.Import and JsonConverter.ExportToString calls are coming. Anyway, assigning a null value to an integer is typical "strong-typed speak". In javascript, assigning null to a numeric (no such thing as integer in js) would just make that variable stop being numeric.
So maybe you should give us more context: libraries used, language you are using the data from (apparently not javascript).

Could you use an empty string i.e. "" instead of null, and use a placeholder number e.g. -2147483648 to indicate a null integer value?

Related

CsvToBean treat string value as null

Is there a way for CsvToBean to treat a specific string or character as null when it maps into a Bean class?
For example I have a CSV that has a quoted value of "-". I know that when creating a CsvToBean you can specify
.withFieldAsNull(CSVReaderNullFieldIndicator)
which handles cases when a value is not present in CSV data. Just can't find a way to treat a character value as null.
No OpenCSV does not do data translation like that. You will have to change your the setters in your bean to translate the value as you are setting it.

How to catch a failed CAST statement in BigQuery SQL?

We are converting a STRING field to a DATETIME field using BigQuery's non-legacy SQL.
The DATETIME fields are corrupted with values like "None" and "0.0" which causes our CAST statement to fail.
We see that for other types of SQL there are TRY-CATCH functions and ISNUMERIC() tests - neither of which appear to be supported in BigQuery.
Here's an example that catches "None" but fails to catch random floats or integers:
CASE
WHEN UPDT_DT_TM LIKE 'None' THEN NULL
ELSE CAST(UPDT_DT_TM AS DATETIME)
END AS UPDT_DT_TM,
Apart from User-Defined-Functions (UDF) in BigQuery - is there any other way in create a CASE statement that can convert to DATETIME when it can but otherwise just leaves the value as NULL?
You can use the SAFE_CAST function, which returns NULL if the input is not a valid value when interpreted as the desired type. In your case, you would just use SAFE_CAST(UPDT_DT_TM AS DATETIME). It is in the Functions & Operators documentation.

Reading dates from Sqlite with Delphi / Firedac

When reading date fields from Sqlite into Firedac, I get conversion errors. The fields are called dates but with string entries (yyyy-mm-dd). I set the option for Datetime Format = string, but I've discovered that while null values are handled OK, empty values (= '') produce an error which I can't figure out how to handle.
You can enable StrsEmpty2Null option, which will automatically convert all empty strings to NULL state. But it's for all values and parameters handled by the data component. So it's not the cure.
I'm not sure what you're doing, but in general, NULL is a state and you cannot convert NULL state to a value because it's a state indicating no value. So as you cannot convert empty string to date.
So try to describe more about your value to string conversion, so we can suggest a proper way to deal with it. For SQLite, I'd suggest using DATE pseudo data type and convert values through the built-in formatting expressions.

How can I update a database string value from a TextBox?

I cant update my database through textbox.in the case of numeric value i can update but cant with a string value.
my coding is...
SqlCommand cmdup= new SqlCommand("UPDATE [port1] SET [prt1]=#prt1 WHERE [no]= 1",cn);
cmdup.Parameters.Add("#prt1", TextBox1.Text);
cmdup.ExecuteNonQuery();
if any one know the ans: reply me
I'd recommend explicitly defining the parameter type to match the type in the DB, and passing the value to Parameters.Add as the appropriate type.
e.g. at the moment, you are passing a string typed value to Parameters.Add, and not defining the type explicitly. Therefore, it will assume the data type from the type of value supplied...so the #prt1 type will be passed in to the DB as NVARCHAR I believe.
If the prt1 field is an INTEGER for example, much safer IMHO to do something like:
cmd.Parameters.Add("#prt", SqlDbType.Int,4).Value = Int32.Parse(TextBox1.Text);
or
cmd.Parameters.AddWithValue("#prt", Int32.Parse(TextBox1.Text));
I always like to fully define the type of the parameters I pass in to SQL to rule out any potential, unexpected issues.
Check the datatype for the #prt1 to see if it is a Int value because you are setting to the same column two different values.
Parse the textBox to Integer as you want to insert a numeric value i.e. Int64.Parse("Textbox1.Text"); well with string it does have problems.
Just a wild guess: Does prt maybe only accept numeric values?

Empty QueryString parameter

The Problem
What is the proper way to check for the foo parameter in the following url's querystring using asp.net? Is this even possible?
http://example.com?bar=3&foo
I have tried checking Request["foo"] as well as Request.QueryString["foo"] and I get null for both. I have also tried populating a List with the values from the QueryString collection but as I mention below, it does not include the value.
The Question
I understand that there is no value, but shouldn't Request["foo"] return an empty string rather than null? Is there a way to find out if a querystring key exists even if it has no value?
Notes
I found here that Request.QueryString.AllKeys includes null for blank querystring parameters.
[edit]
As stated below by James and Dreas a Regex to parse the raw url might be the best (and possibly only) approach.
Regex.IsMatch(Request.RawUrl, "[?&]thumb([&=]|$)")
You can use null as the key for the NameValueCollection and it will give you a comma-delimited list of parameter names that don't have values.
For http://example.com?bar=3&foo you would use Request.QueryString[null] and it would retrieve foo.
If you have more than one parameter name without a value, it will give you a value that is comma-delimited.
For http://example.com?bar=3&foo&test you would get foo,test as a value back.
Update:
You can actually use Request.QueryString.GetValues(null) to get the parameter names that don't have values.
Request.ServerVariables["QUERY_STRING"] will return the query string, complete, as a string. Then search it using a Regex or IndexOf
You get null because the foo parameter doesn't have a value with it.
...What's the problem exactly?
If you still want to check for its existence (although it lacks a value), try something like this:
bool doesFooExist = Request.Url.AbsoluteUri.IndexOf("foo=") >= 0 ? true : false;
Dreas is correct. Variable "bar" has a value but foo does not.
QueryString["Bar"] will return 3 because it has the value 3 associated with the variable Bar. However, Foo will return null because their is no value and when you are calling QueryString on a variable or key, you are querying for the value, not the key, so that is why you are returning null.
query string is probably a waste one. If you use Request.Params[""] or iterate it, then you will find your desired one. Its really handy than other things.
Let me know if you need any help in this.

Resources