Base solution for your next web application

Activities of "KarakTheWise"

I found a solid solution and thought I'd post it here:

In my .ts file I added a formData.append()

<br>

uploadExcel(data: { files: File }): void {
    const formData: FormData = new FormData();    const file = data.files[0];
    formData.append('file', file, file.name);
    formData.append('guestEventId', this.guestEventId.toString());

<br> Then on the server, I added the following roughed in code <br>

[HttpPost]
public async Task<JsonResult> ImportAttendeesFromExcel()
{
long currentGuestEventId = 0;
            try
            {

                var eventAttendeeFile = Request.Form.Files.First();
                var hasGuestEventId = Request.Form.ContainsKey("guestEventId");
                _logger.Warn("Has GuestEventId: " + hasGuestEventId);
                var formKeys = Request.Form.Keys;
                foreach (var formKey in Request.Form)
                {
                        var guestEventId = formKey.Value;
                        _logger.Warn("GuestEventId: " + guestEventId);


                    currentGuestEventId = long.Parse(guestEventId);
                    _logger.Warn("GuestEventId Int64: " + currentGuestEventId);


                }

                if (eventAttendeeFile == null)
                {
                    throw new UserFriendlyException("You must submit a file");
                }

<br> I have some clean up to do but it is working well. Just thought I'd share.

Greetings,

API v 9.2.0 Angular

I'm having an odd bug where I can successfully import an Excel spreadsheet one time. If I try and do it again, a second time w/o recompiling the server code, I get a NullRef exception:

WARN  2022-07-13 17:00:04,517 [61   ] cts.Importing.ContactListExcelDataReader - DoB Exception: System.NullReferenceException: Object reference not set to an instance of an object. at NPOI.Util.LocaleUtil.GetLocaleCalendar(TimeZone timeZone) at NPOI.Util.LocaleUtil.GetLocaleCalendar() at NPOI.SS.UserModel.DateUtil.GetJavaCalendar(Double date, Boolean use1904windowing, TimeZone timeZone, Boolean roundSeconds) at NPOI.SS.UserModel.DateUtil.GetJavaDate(Double date, Boolean use1904windowing) at NPOI.XSSF.UserModel.XSSFCell.get_DateCellValue() at Resolution.Contacts.Importing.ContactListExcelDataReader.ProcessExcelRow(ISheet worksheet, Int32 row) in D:\App Development\Resolution\ASPNET-Zero\Resolution\aspnet-core\src\Resolution.Application\Contacts\Importing\ContactListExcelDataReader.cs:line 53

It doesn't make sense as to why the DateOfBirth data is reading as nullref on the second import.

try
            {
                contact.FirstName = GetRequiredValueFromRowOrNull(worksheet, row, 0, nameof(contact.FirstName), exceptionMessage);
                \_logger.Warn("FirstName: " + contact.FirstName + " ex " + exceptionMessage);
                contact.LastName = GetRequiredValueFromRowOrNull(worksheet, row, 1, nameof(contact.LastName), exceptionMessage);
                \_logger.Warn("LastName: " + contact.LastName + " ex " + exceptionMessage);
                //contact.DateOfBirth = DateTime.Parse(GetRequiredValueFromRowOrNull(worksheet, row, 2, nameof(contact.DateOfBirth), exceptionMessage)).ToShortDateString();
                try
                {
                    //contact.DateOfBirth = DateTime.Parse(GetRequiredValueFromRowOrNull(worksheet, row, 2, nameof(contact.DateOfBirth), exceptionMessage));
                    c**ontact.DateOfBirth = worksheet.GetRow(row).Cells[2].DateCellValue;**
                    \_logger.Warn("DateOfBirth: " + contact.DateOfBirth + " ex " + exceptionMessage);
                }
                catch (Exception e)
                {
                    \_logger.Warn("DoB Exception: " + e);
                }

            }
            catch (System.Exception exception)
            {
                contact.Exception = exception.Message;
            }

The GetRequiredValueFromRowOrNull() method is the same as the method of the same name in UserListExcelDataReader.cs. I'm quite baffled and not sure what to look for. Could something be getting cached? But then I would think the entire set would be null or something, not just one column. I know the other columns are not null via logger entries:

WARN  2022-07-13 17:00:04,517 [61   ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN  2022-07-13 17:00:04,517 [61   ] cts.Importing.ContactListExcelDataReader - FirstName: Zoe ex WARN  2022-07-13 17:00:04,517 [61   ] cts.Importing.ContactListExcelDataReader - LastName: James ex WARN  2022-07-13 17:00:04,517 [61   ] cts.Importing.ContactListExcelDataReader - DoB Exception: System.NullReferenceException: Object reference not set to an instance of an object.

This is screen grab of the Excel file:

Any advice would be so great. I've been on this for a couple of days now and not sure where else to look.

Thanks!

I would like to check for null but I can't pass the date value to that method because it's a seen as a number. So I get an error. And I guess the bigger issue is I'm not sure why it's fine the first time I run the import but not the times after? It's really odd. I've tried debugging but when it get's to that part of the code it just throws the exception error and the DateOfBirth spreadsheet column is being read as null. It doesn't make any sense :). If I recompile the server code, it imports correctly the first time.


After looking at my code some more throughout the day, what doesn't make sense is the fact that the first impor is working fine. Here's some log outputs: WARN 2022-07-14 17:14:09,205 [18 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:14:09,210 [18 ] cts.Importing.ContactListExcelDataReader - FirstName: Jane ex WARN 2022-07-14 17:14:09,210 [18 ] cts.Importing.ContactListExcelDataReader - LastName: Doe ex WARN 2022-07-14 17:14:09,211 [18 ] cts.Importing.ContactListExcelDataReader - DateOfBirth: 3/22/1997 12:00:00 AM ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - FirstName: Jim ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - LastName: Smith ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - DateOfBirth: 10/8/1992 12:00:00 AM ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - FirstName: Carry ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - LastName: Brown ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - DateOfBirth: 9/23/1972 12:00:00 AM ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - FirstName: Zoe ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - LastName: James ex WARN 2022-07-14 17:14:09,212 [18 ] cts.Importing.ContactListExcelDataReader - DateOfBirth: 6/13/2005 12:00:00 AM ex

But when I submit the very same sheet again, I get this for all four row. I only listed one. And I'm checking for duplicates yet, so that's not throwing any errors: WARN 2022-07-14 17:17:34,933 [7 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:17:34,933 [7 ] cts.Importing.ContactListExcelDataReader - FirstName: Jane ex WARN 2022-07-14 17:17:34,933 [7 ] cts.Importing.ContactListExcelDataReader - LastName: Doe ex WARN 2022-07-14 17:17:34,944 [7 ] cts.Importing.ContactListExcelDataReader - DoB Exception: System.NullReferenceException: Object reference not set to an instance of an object. at NPOI.Util.LocaleUtil.GetLocaleCalendar(TimeZone timeZone) at NPOI.Util.LocaleUtil.GetLocaleCalendar() at NPOI.SS.UserModel.DateUtil.GetJavaCalendar(Double date, Boolean use1904windowing, TimeZone timeZone, Boolean roundSeconds) at NPOI.SS.UserModel.DateUtil.GetJavaDate(Double date, Boolean use1904windowing) at NPOI.XSSF.UserModel.XSSFCell.get_DateCellValue() at Resolution.Contacts.Importing.ContactListExcelDataReader.ProcessExcelRow(ISheet worksheet, Int32 row) in D:\App Development\Resolution\ASPNET-Zero\Resolution\aspnet-core\src\Resolution.Application\Contacts\Importing\ContactListExcelDataReader.cs:line 54 WARN 2022-07-14 17:17:34,944 [7 ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN 2022-07-14 17:17:34,944 [7 ] cts.Importing.ContactListExcelDataReader - FirstName: Jim ex WARN 2022-07-14 17:17:34,944 [7 ] cts.Importing.ContactListExcelDataReader - LastName: Smith ex WARN 2022-07-14 17:17:34,944 [7 ] cts.Importing.ContactListExcelDataReader - DoB Exception: System.NullReferenceException: Object reference not set to an instance of an object. at NPOI.Util.LocaleUtil.GetLocaleCalendar(TimeZone timeZone) at NPOI.Util.LocaleUtil.GetLocaleCalendar() at NPOI.SS.UserModel.DateUtil.GetJavaCalendar(Double date, Boolean use1904windowing, TimeZone timeZone, Boolean roundSeconds) at NPOI.SS.UserModel.DateUtil.GetJavaDate(Double date, Boolean use1904windowing) at NPOI.XSSF.UserModel.XSSFCell.get_DateCellValue()

I've been using the user import code as my template for this process. I've compared the code line by line but that doesn't mean I'm not missing something. But, it would be one thing if it consistenlty was miss data in x column, but that's not the case only on subsequent submissions after the first submission succesfully imports.

Alright, after doing some more digging I found something interesting that you may wish to know about:

This is from NPOI's github: Not sure if it's the same verison but, it's still having NullRef issues. https://github.com/nissl-lab/npoi/issues/358

I found this Stack post that is dealing with the exact same type of issue I am: https://stackoverflow.com/questions/54465099/npoi-icell-datecellvalue-return-nullreferenceexception

"Npoi ICell.DateCellValue return NullReferenceException"

In this post, someone posted a solution that I think will work:

<br>

// my code
var doBString = GetStringValue(worksheet.GetRow(row).Cells[2]);

<br> code from Stack minus the static method:

<br>

private string GetStringValue(ICell cell)
        {
            switch (cell.CellType)
            {
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(cell))
                    {
                        try
                        {
                            return cell.DateCellValue.ToString();
                        }
                        catch (NullReferenceException)
                        {
                            return DateTime.FromOADate(cell.NumericCellValue).ToString();
                        }
                    }
                    return cell.NumericCellValue.ToString();

                case CellType.String:
                    return cell.StringCellValue;

                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();

                default:
                    return string.Empty;
            }
        }

<br> Log results show the NullRef AND the correct date returned by the returned value of var doBString above: WARN  2022-07-14 17:35:30,817 [7    ] cts.Importing.ContactListExcelDataReader - Process Excel Row WARN  2022-07-14 17:35:30,817 [7    ] cts.Importing.ContactListExcelDataReader - FirstName: Jane ex WARN  2022-07-14 17:35:30,817 [7    ] cts.Importing.ContactListExcelDataReader - LastName: Doe ex WARN  2022-07-14 17:35:30,817 [7    ] cts.Importing.ContactListExcelDataReader - GetStringValue result: 3/22/1997 12:00:00 AM WARN  2022-07-14 17:35:30,828 [7    ] cts.Importing.ContactListExcelDataReader - DoB Exception: System.NullReferenceException: Object reference not set to an instance of an object. at NPOI.Util.LocaleUtil.GetLocaleCalendar(TimeZone timeZone) at NPOI.Util.LocaleUtil.GetLocaleCalendar() at NPOI.SS.UserModel.DateUtil.GetJavaCalendar(Double date, Boolean use1904windowing, TimeZone timeZone, Boolean roundSeconds) at NPOI.SS.UserModel.DateUtil.GetJavaDate(Double date, Boolean use1904windowing) at NPOI.XSSF.UserModel.XSSFCell.get_DateCellValue()

So my take on this thus far is it's most likely problem with NPOI. Thought I'd put that out there. Any thoughts?

Just to finish off for anyone needing to use this. I will be adding null checks etc. before call the DateTime.Parse() but, this what the 'happy path' looks like at this point :)

<br>

var doBString = GetStringValue(worksheet.GetRow(row).Cells[2]);
contact.DateOfBirth = DateTime.Parse(doBString);

Works great and inserts into SQL just fine. I hope this helps.

You're welcome! Sorry for the walls of text. I hope it's useful information.

API: 9.2.0 .NET Core UI: Angular

Greetings,

As the title states, whenever I open a new window, E.g.; create a contract with our app, I get a 404. But only in production on IIS, not in development. Also, I get a 404 when I attempt to switch tenants. If I enter the Url again, karaktthemad:4500, all is well. So I'm not sure what is happening or where to looking to fix this.

Thanks again!

Thanks for the response. I have that in my web.config but, one thing that unclear to me in the publishing to IIS document was WHERE to put that particular web.config. Right now I have it here:

It seemed to be the only location I could place it that either didn't throw and error or where a web.config didn't already exiist. E.g.; the Resolution folder in the API code. The ResAngular root folder also has a web.config which consists of this code: <br>

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpErrors>
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/index.html" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

Where should this be running? In ResAngular > assets, I do have another web.config that is the same as  your Git version. If I add the Git code to the web.config in the root folder of ResAngular, I get 500 error. And it not something that even makes it to the logs.  Please advise :)

Thanks again!

Ok, I'm not clear as to which config? The one in the wwwroot as shown in the screenshot above? If so I have that in there and it doesn't seem to do anything solve the problem.  This is my web.config in wwwroot:

<?xml version="1.0" encoding="utf-8"?> <configuration>   <system.webServer>     <staticContent>       <remove fileExtension=".json" />       <mimeMap fileExtension=".json" mimeType="application/json" />   <mimeMap fileExtension="woff" mimeType="application/font-woff" />       <mimeMap fileExtension="woff2" mimeType="application/font-woff" />     </staticContent>     <!-- IIS URL Rewrite for Angular routes -->     <rewrite>       <rules>         <rule name="Angular Routes" stopProcessing="true">           <match url=".*" />           <conditions logicalGrouping="MatchAll">             <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />             <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />             <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />           </conditions>           <action type="Rewrite" url="/" />         </rule>       </rules>     </rewrite>   </system.webServer> </configuration>

<br> What am I missing?  Thanks again!

Yes, it's two separate apps: API and Angular. I just tried to add that portion of the code to the web.config in the root the Angular app. It throws a 500.

<?xml version="1.0" encoding="UTF-8"?> <configuration>     <system.webServer>         <httpErrors>             <remove statusCode="404" subStatusCode="-1" />             <error statusCode="404" prefixLanguageFilePath="" path="/index.html" responseMode="ExecuteURL" />         </httpErrors>     </system.webServer> </configuration>

This is all I have in the web.config of the Angular (ResAngular) app. In the ResAngular > assets folder, I have web.config that matches the one you posted. So I don't know what is supposed to be where.

Showing 41 to 50 of 56 entries