When I decided to start programming one of the first places I visited was Project Euler, not because it would teach me how to program but because it had the perfect challenges for using programming to solve problems when starting out learning to code. The problems are based on mathematics, starting off very easy (think fizzbuzz type thing) where it is mostly the programming part that is the challenge but very quickly the problems become harder and increasingly you have to recall your mathematics knowledge in combination with programming skill to get to the answer.
Once you manage to enter the correct answer to the problem you get access to the forum for that particular problem to see what everyone else's solution is. This is the most interesting part! Posted are solutions using all manner of languages such as Haskell, Lua, Pascal etc (looking at the statistics page there are 74 different languages among the users!) and the first solutions were published 9 years ago. You can really see the effort made in the beginning (at least for the first 30-40 problems that I have tackled) to optimize the code to run faster which seem to be decreasing these days with faster PCs.
Also apparent is the solutions by mathematicians where not a line of code seem to have been written before thorough boundary analysis has taken place and the problem has been thought out using standard mathematical rules that once pointed out seem obvious, but as coders we often seem to leave the the computers to take care of, giving them often more than twice the amount of work necessary.
If there ever was a website to inspire you to try a different language or see how clever people use logic and mathematics before coding a solution, this is it! Have a go but beware it is quite addictive!
Wednesday, 25 December 2013
Wednesday, 11 December 2013
How to add buttons dynamically to ASP.Net
If you don't know how many of something you need on a page, ASP.Net allows you to add controls dynamically. These dynamically loaded controls are unfortunately forgotten each time the page is posted back and needs to be reloaded at every postback as well as the first time the page is requested. To be able to access their values through the viewstate you are best adding them as early as possible (Page_Init) and populate them and customize them in the Page_Load event. To read an excellent article about this please visit the 4guysfromrolla website which goes into this in great detail.
However, what if it isn't just a value you want to access but instead want to access an event from a dynamically added control such as a button? Events are only raised after the Page_Load has taken place and then your controls have already been regenerated.
The way to solve this as I have found (if there is a better way let me know!) is to specifically reload the section that should be updated by the button click after first having cleared it of the controls that the postback generated in the Page_Init stage if there are controls already there that you wish to update. Failure to clear the placeholder means that they would be added twice (one with the old values as the button event hasn't been raised yet and once with the new values) which isn't allowed unless your IDs are different. Please see example below which tries to demonstrate this.
However, what if it isn't just a value you want to access but instead want to access an event from a dynamically added control such as a button? Events are only raised after the Page_Load has taken place and then your controls have already been regenerated.
The way to solve this as I have found (if there is a better way let me know!) is to specifically reload the section that should be updated by the button click after first having cleared it of the controls that the postback generated in the Page_Init stage if there are controls already there that you wish to update. Failure to clear the placeholder means that they would be added twice (one with the old values as the button event hasn't been raised yet and once with the new values) which isn't allowed unless your IDs are different. Please see example below which tries to demonstrate this.
Monday, 9 December 2013
Being bad at maths does not automatically make you creative!
![]() |
| Created by mathematicians and programmers in an easy format for arty people to appreciate. |
When people say this, it is like the only signs of creativity is what is immediately in front of them, a pretty picture, a lovely piece of music etc. What unfortunately they have failed to acknowledge is the amazing creativity that goes into many technical and scientific projects that would require them first to learn the technical concepts and backgrounds to be able to appreciate. It is a lot easier to label it as non-creative and keep looking at a painting instead.
In no other technical field is creativity so visible and tangible as in computing. Designs are forever evolving and ways of solving problems can be truly beautiful. The restrictions are fewer than in most fields and that leaves the programmer to truly fulfill his creative potential. Maybe not with a huge audience but still, that doesn't matter as the best bit about programming is the feeling you have created something! And you can always feel a little sorry for the self proclaimed "arty" people who might never be able to appreciate a beautiful piece of code...
Saturday, 7 December 2013
So what have I learnt....
| Cats and computing in perfect harmony! |
Trying to explain and present something makes you think extra hard about what you do and gives you a much better understanding for how things work. I often find at work, that once you have made something work you are inevitably focusing on the next stage, not feeling that there is enough time to go back and dive deeper into a specific feature. However when you are trying to explain it to someone else you have to at least try and figure out how and why you did certain things and inevitably you find you understand things better afterwards. And who knows someone might even suggest an even better way of doing it.
When I now encounter problems at work that either I can't figure out straight away or even after searching the web is not particularly obvious, I think whether it might be worth writing another post and straight away I feel more motivated to try and understand what is happening rather than just randomly trying things until it magically works.
So even if not many people will read my posts (and hey ho if I'm the only person reading them to remind me next time I encounter the same issue) then the worst thing that can happen is that more server space gets filled up with more data. And honestly lets face it, it would be filled up with pictures of cats anyway!
Friday, 6 December 2013
Customize headers for the ASP.Net Gridview control dynamically
I almost exclusively bind the data for my gridview in the code behind as often the same gridview is used for displaying different tables. It also gives the user more control to make minor changes in the database tables without having to redesign the UI every time. So a lot of things happen in the gridview's onRowDatabound event which is the event that gets triggered when the data is being bound to the elements of the gridview. For instance this is where you can change the background colour of cells depending on their value and add formatting to the text in the cells etc but as I'm going to show you below you can also add and format the header rows dynamically depending on the data.
In this particular gridview I am adding three extra rows as headers as well as formatting the data rows depending on if they are in edit mode or not
The method that created as add the new header rows this is shown below:
If you are just displaying data you can finish here. However one thing to bear in mind is that if you are calling the commands on individual rows such as edit/update etc you are going to have a slight problem as there is a mismatch of the index sent to the code behind (which has three extra rows!) compared to the actual index of the row you want to update as the page doesn't know about the extra rows you have added dynamically when you are of reloading the page. So in the RowEditing event you simply subtract the extra rows from the e.NewEditIndex. This seems to work a little different depending if you have autogenerated the edit/update buttons or if you have added them in a template field. For this particular case I have added them in a template field.
In this particular gridview I am adding three extra rows as headers as well as formatting the data rows depending on if they are in edit mode or not
The method that created as add the new header rows this is shown below:
If you are just displaying data you can finish here. However one thing to bear in mind is that if you are calling the commands on individual rows such as edit/update etc you are going to have a slight problem as there is a mismatch of the index sent to the code behind (which has three extra rows!) compared to the actual index of the row you want to update as the page doesn't know about the extra rows you have added dynamically when you are of reloading the page. So in the RowEditing event you simply subtract the extra rows from the e.NewEditIndex. This seems to work a little different depending if you have autogenerated the edit/update buttons or if you have added them in a template field. For this particular case I have added them in a template field.
Friday, 29 November 2013
How to update sql database when you don't want to hard code the column names in the application
One of my first applications I was asked to do was an intranet based reporting tool and my customer was a software department. I wasn't given many constraints apart from that it would have to be a ASP.Net application and nothing project specific should be hard coded into the web application to allow multiple uses for the same page. I quickly realized that the gridview control in ASP.Net would make the perfect control for showing and allowing for editing of the data regarding the projects. Pretty much all the coding was done in the C# "code behind" of the application with the .aspx page containing only the named control linked up with the event methods in the code behind where all the data binding and formatting was taking place.
It was relatively easy to produce a nice looking table retrieved from the sql database in the gridview but how would I go about saving any changes without specifying any columns names in the code? It turned out it wasn't so hard after all as the information was already known to the application as the column names were present in the gridview as the gridview column names and the data to be saved was in the data row below.
In the GridView1_RowUpdating event I stored the column names and the data in two lists as shown below:
Code here
In the sql update method I first created the sql string with the column name information and made sure that every update was made using parameters to avoid the risk of sql injection. The data was then taken from the second list and added in the Parameters.AddWithValue function and the sql query was executed.
Code here
For this to work you need to have all your data in one database table but you don't necessarily have to show (or update) the whole table. Also if you have different names for your columns in the gridview compared to your column names in your database table you could potentially just create a look up table in the database and call this for each name in the list of column names before calling the sql updating method.
It was relatively easy to produce a nice looking table retrieved from the sql database in the gridview but how would I go about saving any changes without specifying any columns names in the code? It turned out it wasn't so hard after all as the information was already known to the application as the column names were present in the gridview as the gridview column names and the data to be saved was in the data row below.
In the GridView1_RowUpdating event I stored the column names and the data in two lists as shown below:
Code here
In the sql update method I first created the sql string with the column name information and made sure that every update was made using parameters to avoid the risk of sql injection. The data was then taken from the second list and added in the Parameters.AddWithValue function and the sql query was executed.
Code here
For this to work you need to have all your data in one database table but you don't necessarily have to show (or update) the whole table. Also if you have different names for your columns in the gridview compared to your column names in your database table you could potentially just create a look up table in the database and call this for each name in the list of column names before calling the sql updating method.
Friday, 22 November 2013
Right, finally I took the plunge!
The first post in a blog has a lot in common with the first entry in a diary. Something has happened and it suddenly it feels like it needs writing down. There is a pre-"this has now happened" and a post-"this has now happened" in time. So what has now happened that justifies using up yet more server space and clogging up the ether (well the slow cables that serve us our broadband here in our tiny hamlet anyway). Well I have finally taken the step from being a user of coding projects/help forums and other people's blogs to actually contributing something myself to the vast pool of knowledge that is present on the World Wide Web!
Was it something extraordinary? A massive piece of software showcasing some genius programming concepts? No of course not! It was small, self contained and pretty safe bit of code to go in as an add-on to a GitHub project (and it might not even get included!) But the step has been taken and I have now entered the post-"this has now happened" phase and I feel that now is the time to share a little of what I know to offer the same help as everyone out there provided me when I started coding.
It is quite funny how this also has coincided with the fact I don't get my own questions answered by searching the web in the same way as when I started two years ago. Possibly because then I gave up if there wasn't an answer to it on the web or just compromised to get something working that was similar but not exactly what I needed. Now I feel more confident in doing my own thing and trying to create exactly what need (unless searching for it makes me realize that what I really needed was not at all what I originally wanted!).
So this is it! Start of a new era for me (but for human-kind a very non-significant change to an already amazing era).
Was it something extraordinary? A massive piece of software showcasing some genius programming concepts? No of course not! It was small, self contained and pretty safe bit of code to go in as an add-on to a GitHub project (and it might not even get included!) But the step has been taken and I have now entered the post-"this has now happened" phase and I feel that now is the time to share a little of what I know to offer the same help as everyone out there provided me when I started coding.
It is quite funny how this also has coincided with the fact I don't get my own questions answered by searching the web in the same way as when I started two years ago. Possibly because then I gave up if there wasn't an answer to it on the web or just compromised to get something working that was similar but not exactly what I needed. Now I feel more confident in doing my own thing and trying to create exactly what need (unless searching for it makes me realize that what I really needed was not at all what I originally wanted!).
So this is it! Start of a new era for me (but for human-kind a very non-significant change to an already amazing era).
Subscribe to:
Posts (Atom)
