Sunday, July 19, 2020

download Attribute in 'a' Tag Does Not Work when Viewing a Web Page Locally

Overview

If you're like me, when you're building web pages, you work locally before publishing your work to the web. While this is helpful in ensuring you don't publish bad code and break something, it also has its disadvantages in that not everything functions the same locally as is does in the great outdoors, so to speak.

What Happened?

I was working on my portfolio page for class and I needed to include a downloadable link for my resume. I later found out that it was fine to have my resume open in the browser, as long as there was a link to download it, which with a pdf, there is. However, I had already invested time trying several different things to get my resume to automatically download instead of opening in the browser first.

I found this article on w3schools and it seems pretty simple to just add the download attribute to my a tag, which I did. I even tried to add a file name to the download attribute as well. No matter how I manipulated the a tag, my resume just continued to open in the browser instead of download.

Next Steps

I reached out to the instructor and found that it was OK for my resume to open in the browser, as long as there was an option to download it. I added the target attribute to my a tag and set it to equal _blank so that at the very least, my resume would open in a new tab and the viewer would then have the option to download locally, if they wished.

Even though this would suffice, I was dumbfounded as to why it didn't work. I had followed all of the steps and spent time trying several different things to no avail. Even though I fulfilled the requirements for the assignment, my curiosity got the best of me and I wanted to get it to function correctly.

Voilà!

In building my portfolio and testing, I was doing everything locally. I try to at least have the majority of the framework in place before I push things to GitHub. Once I finally pushed my first version to GitHub and published it, I went through all of the elements and links on the page to make sure everything was working correctly. When I got to my resume link and clicked it, rather than open in a new browser tab, it downloaded directly to my download folder. 🤦‍♀️

Lesson learned: the download attribute on the a tab only works when the website it published. At least for me, it did not work when I viewed the page locally.

Second lesson learned: Since I'm not really working in a live environment until I officially submit my homework, push early and more often. Don't ever spend time trying to fix something locally before testing it published first.

Behind the Curtain

This is what I had initially tested that kept opening the pdf in the browser. I'm sure if I replaced what I currently have with this, it would work in the published version:
<a href="./assets/download/christina-resume.pdf" download>Resume</a>

My final code looks like this. I think I can probably remove the target attribute in a future update:
<a href="./assets/download/resume.pdf" target="_blank" download="Christina-resume.pdf">Download Resume</a>

Something to note: based on a suggestion from my instructor, I changed the link text from Resume to Download Resume so that the user would expect the resume to download instead of open in the browser.



Wednesday, July 15, 2020

Calling a Stored Procedure with a Parameter in C# using ExecuteNonQuery()



Background

I had a very frustrating issue the other day. I was trying to call a Stored Procedure (SP) from my C# code using the same command I use to call the SP in MySQL. I kept getting a very irritating error:

Exception error: “MySql.Data.MySqlClient.MySqlException: Parameter ‘@_itemId’ must be defined”


My SP call required me to pass a parameter (@_itemId) that it would handle. This was not a parameter I needed to set in my code - I just simply needed to add it as part of my SP call.


My Code

I was trying to keep my code simple - build the SQL statement with incoming JSON using a foreach loop, append the id and connection string, and make the call to the SP. I think in total, I have about 10 lines of actual code that are actually doing something, as opposed to lines of comments or log lines. So why didn't this work??

MySqlCommand cmdSQL = new MySqlCommand($@"{spCallStatement}", conn);

cmdSQL.ExecuteNonQuery();


Troubleshooting

I spent a good bit of time troubleshooting. I tried to create a variable and add the parameter to the call. I tried several other methods besides ExecuteNonQuery(). I logged my spCallStatement text, copied and pasted it into MySQL, and it ran fine but it would NOT run from my code. I added a lot of log lines to determine that it was making the connections but it was definitely failing when making the SP call. Frustrating!!


myConnectionString

After honing in on my Google-Fu skills, I finally put the pieces together and realized it wasn't the method I was using. It wasn't my code, per se, it was my connection string. I needed to add a setting to my connection string. I didn't share my connection string above, for obvious reasons, but it essentially gathers the necessary components to connect to the db. All I had to do was append "Allow User Variables=True" to the end of my connection string.

myConnectionString += $"{myConnectionString}Allow User Variables=True";


Success!!

That was it. The simple setting was all I had to add and now my code is running as expected. It's a beautiful thing when your code finally works as you expect it, isn't it?  ;) 


Why a Stored Proc?

Let the SP do the work to see if the record exists in your table to determine whether you should perform an update/insert/etc of the record. Make one call from your program/code to your database instead of multiple.


download Attribute in 'a' Tag Does Not Work when Viewing a Web Page Locally

Overview If you're like me, when you're building web pages, you work locally before publishing your work to the web. While this is h...