Error get value null in Excel MVC ASP.NET - asp.net-mvc

I am using the following:
using (ExcelPackage ExcelPackage p = new ())
To open the excel worksheet and getting the values ​​of the cells as follows
string ap = ws.Cells[Lin, 12].Value.ToString().Trim();
But when the cell is empty presenting me this error, as I do so that when the cell is empty it ternha the null value. Estrou needing grab values ​​from several cells having value or not.

You can test for a null value and substitute an empty string using code like this:
string strValue = Worksheets.Cells[2,5].value==null ? string.Empty : Worksheets.Cells[2,5].value.ToString();
example

string ap = (ws.Cells[Lin, 12].Value??"").ToString().Trim();

Related

Processing json string in mql4

I have received the following string:
{"records":[{"id":"rec4haaOncoQniu8U","fields":{"orders1":5},"createdTime":"2020-02-08T09:08:22.000Z"}]}
I am not understanding how I can process and separate the values of the json in mql4 using the "JAson.mqh " library, located here: https://www.mql5.com/en/code/13663
I need the values of "orders" located under "fields" , value = 5.
the only "KEYS" that changes are the keys within the "fields" values.
i would like to be able to get the values with something like this:
string value1 = Result[0].["fields"].["orders1"]; //5
string value2 = Result[0].["fields"].["orders2"];
Please let me know what I can do.
You can get the value using the following format. Note that it has to be casted to a type. (I have casted it to int as it is the type it is in the JSON, but you can cast it to string as well)
int value1 = json["records"][0]["fields"]["orders1"].ToInt(); // if you want to make it a string use ToStr() instead of ToInt()
Here is a full example of what I did
string jsonString = "{\"records\": [{\"id\": \"rec4haaOncoQniu8U\",\"fields\": {\"orders1\": 5 }\"createdTime\": \"2020-02-08T09:08:22.000Z\"}]}";
if(json.Deserialize(jsonString))
Alert(json["records"][0]["fields"]["orders1"].ToInt());
Hope it helped.

How to get DataGridView cell value?

I need to compare my array string with DataGridView cell value
string z = DataGridView->Rows[e->RowIndex]->Cells[1]->Value.ToString();
I'm getting a problem:
IntelliSense: expression must have class type
I figured out the answer if anyone else needs solution here it is
String^ z = System::Convert::ToString(Grid_Darbuotojai->Rows[e->RowIndex]->Cells[1]->Value);

Calling sqlite3_column_type returns SQLITE_INTEGER for a STRING column that contains only numbers causing an integer overflow

I'm working on a legacy project and have found an issue. The code is reading from an sqlite database. It performs a query and iterates over the statement and then for each column it gets the column name and the column value.
To get the value it calls sqlite3_column_type. Then it checks the types and uses sqlite3_column_int, sqlite3_column_double, sqlite3_column_text, etc. to read the value.
The issue is that there is a row in the DB that has a STRING column but the value contains only numbers (the value was set as 12 characters long, something like #"123456789012"). Calling sqlite3_column_type for this row returns SQLITE_INTEGER as it's only looking at the value of the column and not the declared column type. This means that the code will then read the value with sqlite3_column_int and I'll end up with an integer overflow and a number nothing like the original value contained in the DB.
Here is a quick example:
int columnType = sqlite3_column_type(statement, index);
if (columnType == SQLITE_INTEGER) {
//use sqlite3_column_int(statement, index)
}
else if (columnType == SQLITE_FLOAT) {
//use sqlite3_column_double(statement, index)
}
else if (columnType == SQLITE_TEXT) {
//use sqlite3_column_text(statement, index)
}
///etc...
How can I make sure the the value I get out from a STRING column containing only numbers is treated correctly? Can I somehow check the declared column type (other than querying and parsing out the original create table sql)? Is there a better way to do this?
Using column_decltype instead of column_type means that I get that declared column type (STRING) instead of the type of value. Then I can compare the result against STRING, TEXT, etc.to figure out how to read the value.

How can I convert a string to a char array in ActionScript 3?

How do you convert a string into a char array in ActionScript 3.0?
I tried the below code but I get an error:
var temp:ByteArray = new ByteArray();
temp = input.toCharArray();
From the error, I understand that the toCharArray() function cannot be applied to a string (i.e in my case - input). Please help me out. I am a beginner.
I am not sure if this helps your purpose but you can use String#split():
If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array.
var array:Array = "split".split("");
Now you can get individual elements using index
array[0] == 's' ; array[1] == 'p' ....
Depending on what you need to do with it, the individual characters can also be accessed with string.charAt(index), without splitting them into an array.

LINQ Checking for Nulls

How do I check Nulls in Linq?
I have a third-party code that returns a DataTable with a column type DateTime that is nullable.
My code:
var profile = (from d in ds.Tables[0].AsEnumerable()
select new User
{
FirstName = d["FirstName"].ToString(),
Birthdate = Convert.ToDateTime(d["BirthDate"].ToString())
}).FirstOrDefault();
returns an error that the Object cannot be cast from DBNull to other types on the Birthdate= statement.
I tried using the following code but returns a cast exception (Cannot cast from string to DateTime?)
Birthdate = (DateTime?)d["BirthDate"].ToString();
Is there a way to short-hand checking the null values?
I have not tested this, but you can try the following:
Birthdate = d.Field<DateTime?>("BirthDate")
From MSDN:
The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is [DBNull.]Value, the returned nullable type will have a value of null.
You can create a helper function to check the value for DBNull, as follows:
private static DateTime? ConvertNullableToDateTime(object val) {
return (val != null && val != DBNull.Value) ? Convert.ToDateTime(val.ToString()) : null;
}
Now you can use this method in your LINQ query:
var profile = (from d in ds.Tables[0].AsEnumerable()
select new User
{
FirstName = d["FirstName"].ToString(),
Birthdate = ConvertNullableToDateTime(d["BirthDate"])
}).FirstOrDefault();
I have got error while I used the above code. I think we can use the below code.
Birthdate = dr["BirthDate"] == DBNull.Value ? (DateTime?)null : (DateTime)dr["BirthDate"];
If we use Convert.ToString(dr["BirthDate"]), then it convert to empty string, if it has DBNull.
Try that also.

Resources