Posts by garys:
NULLS Are Funny Animals
Gary Synan | June 8th, 2011in Other, Web Development
Nulls are funny animals that require special handling. They are not even like empty strings that have a zero length and nothing for a value. Nulls have no value, undefined.
When you access data from a DB table and return it to your application code you try to keep the DB nulls from returning to the code. If you don’t it is a sure error waiting to blow up your page. A Common method for preventing that is to replace the DB null with an empty string in the SELECT statement.
I have used the following technique often.
Let’s assume we have DB column named ‘flag’ that we are concerned with.
The select statement may look like this:
SELECT ID,
FirstName,
LastName,
.
.
.
isnull(Flag, ‘’) as Flag
FROM …
In the example above, if the flag was null, an empty string would be returned and I would check in my code something like this: If flag <> “” then … to see if there was any data in the field.
However I found out the hard way that the DB column has to be defined so that it can pass an empty string. Varchar allows this to happen since it can have a length of 0.
I ran into a situation where my application code check for an empty string was false but when I checked the DB field it was null. So why wasn’t it passing and empty string? Obviously there was a bug or some type of corruption in the DB because I use this technique all the time.
After quite some effort in debugging, I discovered that the column of flag was defined as a char(1).
No problem, because I indicated in the select statement to pass me an empty string if the field was null. I may have indicated to pass me an empty string but that is not what happens when the column has a defined length. I got passed back a 1 character field of a space because the column was defined to be 1 character and it got space padding.
Lessons learned. If I indicate in the select statement to pass a null value back as an empty string, I make sure the column does not have a defined length but rather a varchar designation so it can have a 0 length to reflect the empty string.
Nulls are funny animals.
Tags: website development
Posted in Other, Web Development | No Comments »
Java Script with Dot Net
Gary Synan | January 11th, 2011in Other, Web Development
There are many times when you want to execute some javascript within your Dot Net page to give it that smooth client side processing without reloading the page. One example would be when you have a contact or registration page that needs to be edited before emailing or saving the page data. You could accomplish this with some Dot Net validation controls, or by registering your javascript within your code behind.
If you already have some javascript that edits input fields and displays a popup box or does whatever it is you need it to do and you want to leverage the existing work then you can also execute it from your Dot Net control. You still want to edit the fields in your code behind in case the user has javascript turned off. Yes there are still people that do this. You can catch the click event with a custom validator in the code behind for this purpose and use an OnClientClick to execute the javascript edits. Hopefully the javascript edit are executed the vast majority of the time and the code behind edits will have no impact.
Look at the example below. Lets say you have some javascript called ‘validate’ that validates the fields on the form. This is the work that you want to leverage in your new project. Placing this within your <head> tag would appear as so:
function validate(theForm) {
javascript edits here
on err return false
else return true
}
Now let’s look at your submit button. In this example it is an image button.
<asp:ImageButton imageUrl=”/images/buttons/submit.gif” AlternateText=”Submit” OnClientClick=”return validate(registrationForm)” width=”92″ height=”24″ tabindex=”12″ runat=”server” > </asp:ImageButton>
Notice the OnClientClickattribute. This will execute your javascript validation. Your javascript validation should return a true or false depending on whether validation passed. The return is captured in the OnClientClick attribute and if it is false, control will not be passed to the code behind. This will keep the Click event from firing and editing your data from within your code behind.
Now let’s suppose javascript is turned off. We need to edit the page from the code behind. So lets set up a custom validator below the image button as so:
<asp:customvalidator id=”registerPage” onServerValidate=”CheckPage” ErrorMessage=”" Runat=”server” ValidationGroup=”Page”></asp:customvalidator>
We will capture the Click event in our code behind with the following code: Execute the custom validator, that will execute your code behind validation.
Sub btnSubmit_Click(ByVal sender AsObject, ByVal e AsSystem.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
registerPage.Validate()
If (Page.IsValid) Then
ProcessForm()
End if
End Sub
Below is the validation routine referenced by the custom validator.
these should be the same edits as your javascript
End Sub
By using the OnClientClick attribute to execute the javascript edits, we can then capture the Click event when javascript is turned off and execute the code behind edits. It should not matter if the javascript passes validation and then the code behind validation is executed because these edits should also pass validation.
That is one way to execute client side validation and leverage some pre-existing javascript code.
Happy coding!
Tags: beacon technologies, javascript, Web Development
Posted in Other, Web Development | No Comments »
Security & Performance Issues: What can I do?
Gary Synan | July 29th, 2010in Other
Many of us today work with PCs on a daily basis and are constantly being made aware of security and performance issues with them. Sometimes we are made aware by being a victim and wonder what could I have done to prevent that or how can I keep the issue from occurring again or even mitigate the collateral damage next time.
I found an interesting article for some of the security/performance issues that we all face and it discusses some actions to take to help with the problems. There were some things that I already knew but the explanations were insightful just the same. Some of the maintenance actions I rarely do because I guess I never stopped to think about the consequences of not doing them. After reading the article I have a better appreciation of those rarely performed maintenance items. Maybe they won’t be so ‘rarely performed’ anymore.
Check it out for yourself: Security and Performance Issues from Iolo Threat Center
Posted in Other | No Comments »
Think Outside the Dot Net Repeater
Gary Synan | May 24th, 2010in Web Development
In Dot Net, as with most web development, there seems to be several ways to accomplish the same task. Some may be better based upon maintainability, clarity, etc. In some cases it’s just preference.
I had such a case recently when I was tasked with making a field in a repeater red if a quantity value was negative. I know, the obvious way would be to use the ‘findControl’ method within the ‘OnItemDataBound’ event, checking the quantity for a less than zero condition and then setting the color property on the field in question to red.
I wondered if I could walk on the wild side and accomplish the same thing by supplying the color to the repeater with the data from the DB and not invoking the ‘OnItemDataBound’ event at all. Take a look at the snippet of code below to see how the repeater was defined for this task. Note the column ‘OrderNo’ – this is the column to make red if the quantity is negative. The quantity is not to be displayed in the repeater.
.
.
.
<ItemTemplate>
<tr>
<td><span style=”color:<%#Container.dataItem(“color”) %>”><%#Container.DataItem(“OrderNo”)%></span></td>
<td><%#Container.DataItem(“SalesPersonName”)%></td>
<td><%#Container.DataItem(“CustomerNo”)%></td>
<td><%#Container.DataItem(“CustomerName”)%></td>
.
.
.
All I have to do now is supply the color that I want the field to be as a column named ‘color’. Now let’s take a look at the select statement to do that:
SELECT
o.OrderNo,
ISNULL(o.SalesPersonName, ””) SalesPersonName,
ISNULL(o.CustomerNo,””) CustomerNo,
o.CustomerName,
(select case
when min(oi.Quantity) < 0 then ”red”
else ”black”
end
from OrderItems oi where oi.OrderNo = o.orderno) as color
FROM Orders o
And Voila!! The repeater field obtains its color from the data selected from the DB. Now I know this is not as radical as stopping the oil leak at the Gulf Coast (or event the one in my leaf blower for that matter), but a different twist none the less.
Now, pardon me while I go work on my leaf blower -
Tags: Web Development
Posted in Web Development | 1 Comment »
Who Am I?
Gary Synan | June 17th, 2009in Web Development
Websites come in many sizes, with the content as varied as the people who developed them. Some are strictly content while some are eCommerce and heavy transactional. It doesn’t matter the technology used to create the site, whether it be DotNet, ASP, PHP or even Java Based I have been used in all of them.
I am probably more prevalent in transactional web sites but it is hard to imagine I have not been used in some content only sites. I can make your programming much easier when used properly, but I can also frustrate you and make debugging a little tricky to find that I am the problem. I am like the genie that appears when you rub the bottle, I am there by a mere mention of my name.
I hold data for you. You had better not need the data very long after you summon me because I don’t live very long and therefore I am better used when a response from the website user is not needed.
One last clue: You cannot bake or eat me.
Who am I?
Tags: asp, DontNet, Java, php
Posted in Web Development | No Comments »
