C#

  • Hertz and Aerlingus team up for new project HertzFlyDrive.com, designed and developed by Dragnet Systems

    HertzFlyDrive.com - A new car rental site from Hertz and Aerlingus, designed and developed by Dragnet Systems

    After many months of work I'm proud to announce my latest project HertzFlyDrive.com. This website was designed from the ground up provide Hertz with a more effective way to manage their car rental booking information and help speed up the booking process for end users.

  • how to sort a DataTable by a specific column using C#

    The majority of the time when displaying content to the end user you will want to order the data by certain criteria. More often than not you will want to sort by a specific column. If your data is in a DataTable how do you do that?

    You need to convert your DataTable into a DataView, apply your sort and save the results back to your DataTable. Let's say we have a DataTable that holds details of all our customers and we want to order the information by their names. To keep things simple I will hard code in only 2 customer values here. You would order the information by doing the following:

    using System.Data;

    //create our datatable and setup with 2 columns

    DataTable dt = new DataTable();

    dt.Columns.Add("CustomerFirstname", typeof(string));

    dt.Columns.Add("CustomerSurname", typeof(string));

    DataRow dr;

    //store some values into datatable

    //just using 2 names as example

    dr = dt.NewRow();

    dr["CustomerFirstname"] = "John";

    dr["CustomerSurname"] = "Murphy";

    dt.Rows.Add(dr);

    dr = dt.NewRow();

    dr["CustomerFirstname"] = "John";

    dr["CustomerSurname"] = "Doe";

    dt.Rows.Add(dr);

    //check to make sure our datatable has at

    //least one value. For this example it's not

    //really need but if we were taking the

    //values from a database then this would be

    //very important!

    if (dt.Rows.Count > 0)

    {

    //convert DataTable to DataView

    DataView dv = dt.DefaultView;

    //apply the sort on CustomerSurname column

    dv.Sort = "CustomerSurname";

    //save our newly ordered results back into our datatable

    dt = dv.ToTable();

    }

    //display your datatable to the user by databinding

    //to repeater/gridview

  • How to get the values of HTML checkboxes within a Repeater using C#

    If you want to read in a bunch of HTML checkboxes that are in a repeater using C# then you need to add a runat="server" element to you checkbox input tag. For example:

    In your code behind you can now read in this element by using the following code:

    using System.Web.UI.HtmlControls;

    foreach (RepeaterItem currentControl in MyRepaterList.Items)

    {

    HtmlInputCheckBox MyCheckBox = new HtmlInputCheckBox();

    MyCheckBox = (HtmlInputCheckBox)currentControl.FindControl("CheckboxName");

    if (MyCheckBox.Checked)

    {

    //logic if checkbox is ticked here

    }

    }

    The above code will loop through all the HTML checkbox elements within your repeater. You can easily modify this to let you look for HTML textboxes (HtmlInputText), HTML radio buttons (HtmlInputRadioButton), etc.

  • Solution to storing lots of form information between pages - Use a Dictionary Object and store it into a Session

    I am working on a site at the moment that requires me to store a lot of user entered values from form elements from one page to the next before a user can complete an order. This is unusual as normally the user just enters all the required values on one screen but for this particular project the design required the project to be setup to work like this.

    ViewStates or hidden fields are a nice way to store form values on post back but it's very hard to carry those values across to other pages. The Session object was my only choice.

    I currently use the asp.net session state service for all my projects. This is a little slower (around 10%-15%) than using the default session setup of in-proc but it has the advantage that the sessions themselves are a lot more stable and tend to last the right amount of time I set them to before they expire.

    The main issue I had with the design was that it is a high volume site and that the number one requirement was that this new site was quick to use. I had around 15 values that needed to be carried through the site after page one and around 5 more items to be gathered from pages 2 and 3 before sending the values off to a third party through XML. A database table would have worked except that it would have been slower than using a session and I had some values that were in dynamic Lists which would made it trickier to save into a table.

    I did some research and found that List objects are great but slow down if you store a lot of information in them so I decided to use a Dictionary object to hold the user values. I would then store this object into a Session to hold my values from page to page.

    It's quiet easy to setup a Dictionary object to hold these user values. First thing you need to do is setup the object. Once you've done that you can add new items to it very easily. Once you have stored all your items into your Dictionary object, simply store it in a session.

    To get your values out of your session all you have to do is:

    Although this isn't ideal, because of the current site setup I wasn't left with much of a choice but to go down this route. The downside to storing my values in a Dictionary object in a session using the State service is that every time I pull the item out of the session or store it to the session there is an overhead as .net serializes my data. It's a small price to pay though. On the plus side I have a setup that allows me to store a lot of variables but just use one session value to hold them.

    This particular project is on a brand new Win Server 2008 with buckets of memory and so far, thankfully, the site has worked and scaled up quite well for us.

  • Loop through all radio buttons in your form using C#

    There have been times when making forms with radio buttons that I have used ASP:RadioButtonLists but recently I was working on a project that required me to have a lot of radio buttons on screen within a nicely styled grid. I could have taken the easy (but messy) way out and simply done an 'if statement' to check through each radio button but that would have been impractical because of the number of radio buttons on the page. I also didn't want to hard code any radio button names because that would have been too messy to manage going forward.

  • How to access a variable from the web.config file using C#

    The web.config file can be a handy place to put site wide settings like the website name or contact admin email address. The steps below will let you use your web.config file to store and retrieve those details. This example will show you how to set the website name in the web.config, create a class to access that name and display the name in page title.

    First up we need to store the values in the web.config file itself so open up your web.config file in your project and locate the <appSettings /> tag. You want your appSettings element to look something like this:

    <appSettings>
        <add key="SiteName" value="My New Site" />
     </appSettings>

    So now you have the name for your site set in the web.config. But how do we actually get this information into our pages? Well the good news is that it's quite easy! I'm going to make a new class called 'sitesettings.cs' in my App_Code folder and inside in that put the following code:

    using System;

    using System.Collections.Generic;

    using System.Web;

    //this will allow you to pickup the web.config values

    using System.Web.Configuration;

    ///

    /// Gets the website settings stored in the web.config file

    ///

    public class WebsiteSettings

    {

    public WebsiteSettings()

    {

    }

    //get the web site title

    public static string SiteTitle

    {

    get

    {

    return WebConfigurationManager.AppSettings["SiteName"];

    }

    }

    }

    Once you have the code above saved as a new class you can access the values within your .cs pages with ease. For example, if you want to print out the website name as a page title you simple need to include the following line in your code behind page (this example is for an aspx page that uses a MasterPage):

    protected void Page_Load(object sender, EventArgs e)

    {

    //line below will read the site name from webconfig

    //and display "My New Site - home page" in your browser window title

    Master.Page.Title = WebsiteSettings.SiteTitle + " - home page";

    }

    Obviously this is only a quick example of what you can do but it can be a handy way to store some of your site settings like the site name, url links, admin email address, etc.

  • Integrate Realex Redirect Payment option using C#

    We do a lot of payment integrations these days for online stores, charity sites, etc. One provider we do a lot of payment integrations with is Realex Payments. They offer a number of payment solutions for both clients and developers to choose from. The main types of setup are called Realex Redirect or Realex Remote. Unless you absolutely have a thirst for pain I would recommend the Redirect option.

    The advantages of choosing this setup are that Realex will handle all the credit card processing on their side so you don't need to worry about verifying the credit cards entered and they handle the notifications for 3Dsecure/Verified By VISA. The only downside is that, to date, the Redirect option does not allow you to do re-occurring payments. If you need that then you will be better off using the Remote option but be prepared to create all the 3Dsecure code yourself! 3D secure is not turned on by default either so make sure you ask for this option if required (I would highly recommend it to avoid costly charge backs!).

    The point of this article is to talk you through how to setup the Realex Redirect option within a C# application. From doing a few quick searches online there doesn't seem to be many examples out there and Realex themselves only provide a classic ASP example.

    C#
  • Solgar.ie goes live with Dragnet Systems New Online Store Software

    Solgar.ie launches

    Dragnet Systems Limited is proud to launch its new flagship online store software. Our software has a number of benefits to businesses:

    • No nonsense, simple to use interfaces for your customers and your site admin team.
    • Excellent reporting tools showing you what products were sold in any timeframe you choose, view all sales for the last 30 days, view all sales for the last year, etc.
    • Reminder emails automatically sent to customers. Useful technique to get your customers to return to your site.
    • Promotions tools giving you the flexibility to offer your customers discounts.
    • Auto image resizing for product images uploaded. you don't need to worry about resizing your images, our software will manage it all.
    • SEO friendly page links. Site fully optimized for Google, MSN, Yahoo, etc.
    • Email Marketing. Your customers can easily sign up, and opt out of email marketing on the site. You have total control over the email's sent to your customers. Includes filters to effectively targeting the right people.
    • Site optimized for speed. Sites load insanely fast and fully tested with over 100,000 products and orders in the system - it's fully robust!
    • And much much more!

    If you think you might be interested in taking our store software for a spin simply contact Dragnet Systems today and see how this software could help your online business succeed.

    ----------------------------------------------

    This ends my shameless plug ;) Sorry about that, I'm just very excited about this new product!

  • How to fix an animated GIF not working in an ASP:Panel when using Internet Explorer

    If you want to use an animated gif within an ASP:Panel when using the ModalPopupExtender you might have noticed that there is an issue displaying this correctly in Internet Explorer. Everything will work as normal in IE except that if you look closely you should notice that the animated gif only runs through once. It doesn't continuously refresh the animation in Internet Explorer. Usually Modal Popups are used to display a friendly message to your users that the site is busy carrying out their request and the animation on a 'loading' graphic is a great way to give the impression that the site is site actually doing something.

    I will include 2 examples that show how to get an animated gif working in an asp:panel - one in a page using MasterPages and another in a regular aspx page. If you didn't know how to get modalboxes working in asp.net you should pick up some tips from this article too (hopefully!). By the end of this example you will have a page with a ModalBox that is called when a submit button is clicked on. I will assume that you have ajax.net setup on your page using the ModalPopupExtender control and that you have the asp:ScriptManager at the top of your page.

    C#
  • Use JQuery to add a simple watermark to your asp.net textbox

    The script below will let you use jQuery to place a simple watermark in your .net textbox. A good example would be a search box on your site where you want the word "search" in your textbox. When the user clicks into the textbox the word "search" will disappear.

    To get this up and running you just need to make sure you have jQuery (obviously) and then you add the following javascript within your <head> tag:

      The function should be easy to follow. Basically we are just checking the value of the textbox that has the class searchField on our page. If that textbox contains the word Search we are going to remove the word when the user is inside that textbox. The last part of the function is a check to ensure we don't wipe out the search term if the textbox contains a value that was entered by the user.

    So now that we have our javascript we just need to make our .net textbox. You will have seen in the above code that I am looking for a class called searchField so I just need to ensure my textbox control has this css class on it.

      <asp:TextBox ID="searchField" runat="server" CssClass="searchField" Text="Search" />

    And that's all there is to it!

Get In Touch

Follow me online at TwitterFacebook or Flickr.

Latest Tweets