Nerdnology

Nerds + Technology = Nerdnology

Sorting a Dataset in C#, asp .net

clock February 25, 2009 13:15 by author Corby

I had some trouble today figuring out why sorting a dataset wasn't working.  Basically my SQL statement put an ORDER BY 'column' DESC but when I dumped it into a dataset, for some reason the order disappeared.  I figured out that in order to sort a DataSet, you actually have to put the data inside that DataSet into something like a DataView.  Then, you sort the DataView with myView.Sort. Here's my sample, working code.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Collections.Generic;
using Microsoft.ApplicationBlocks.Data;

 public void GetItemNumber()
        {
                string sConnectionString = "Data Source=sqlserver;Database=MY-DATABASE-NAME;uid=userid;pwd=password";
                SqlConnection objConn = new SqlConnection(sConnectionString);
                objConn = new SqlConnection(sConnectionString);
                objConn.Open();
                try
                {
                    string MySQLStatement =
                        "SELECT ItemNumber FROM ItemTable where ItemNumber " +
                        "LIKE '%" + this.txtItemumber.Text + "%' ORDER BY ItemNumber DESC";  //this DESC doesn't seem to do anything.

                   using (SqlDataAdapter da = new SqlDataAdapter(MySQLStatement, objConn))
                    {

                       //fill the DataSet

                        DataSet ds = new DataSet();
                        da.Fill(ds);
                      //Create DataView using the data in my DataSet.
                        DataView myView = new DataView();
                        myView = ds.Tables[0].DefaultView;

                     //sort in DESC order with ColumnName

                       myView.Sort = "ItemNumber DESC";


                        foreach (DataRow dr in ds.Tables[0].Rows)
                       {
                           this.txtOutputItemNumber.Text = myView[0].Row[0].ToString();
                        }
                    }
                }
                catch (System.Exception e)
                {
                    this.lblErrors.Text = e.Message.ToString();
                }
                finally
                {
                 objConn.Close();
                }
        }

That should save somebody some time some day.  If this helps you and you use it, please post a comment or link to this post.  Thanks!



GoDaddy hosting for a .NET developer is like pulling teeth!

clock January 8, 2009 21:33 by author Corby

Venting time, appologies in advance.  I am working on a website that is driving me crazy....as all of my past GoDaddy Windows hosted .NET website have been.  I blew nearly $200 last year getting away from GoDaddy hosting in order to get decent database support for my Visual Studio 2005 because I was so damn frustrated.  Here I am again, a year later, doing something completely different but needing cheap hosting, .NET 2.0, c#, Visual Studio 2005 and I'm back to GoDaddy hosting because I am poor.  Surprise surprise, I can't connect to my mySQL database with VS, I have to jump through hoops to even read a table and I have just blown 3 hours google searching how to get an Insert to work.  Did I mention I'm a .NET web developer for a living who deals in SQL every freaking day???

 I now recall why I plopped out the $200 for discountasp.net hosting last year.  GoDaddy Windows hosting when it comes to working with Visual Studio 2005 and asp.net websites SUCKS SUCKS SUCKS....  I am so pissed right now, I'm giving up for the night.  This is just flat out stupid.



Finding the number of leap years between two dates in C#

clock December 5, 2008 09:21 by author Corby

 I recently ran into a situation where I needed to figure out if there were any leap years between two dates.  Although I'm quite sure there is a more elegant way of doing it in C# for purposes of using on an ASP.NET 2.0 page, this is what I came up with and I am quite pleased that it works.  Hopefully somebody will find this useful in the future, as my searching and searching prior to writing this code didn't net me much success.

Why would you want to find out how many leap years are between two dates?  Well, let's say you want to calculate insurance premiums based on a 365 day year.  If you have a leap year, you might have 366 days if the policy started before 2-29 of that year...or if the policy is from 5-1-2000 and goes to 5-1-2004.  The year 2000 does not include the 2-29 date so you are not adjusting the 366 days down to 365...but for the year 2004, you do have to take the extra leap day into consideration when calculating.  This code takes the two dates, starting and ending, figures the total years and days between the dates, and gives a total number of leap year days you must consider between the two.

            DateTime d1 = Convert.ToDateTime(this.txtStartDate.Text);
            DateTime d2 = Convert.ToDateTime(this.txtEndDate.Text);
          
           #region Leap Year Calculations
            //Leap year checker
            int LeapValueToSubtract = 0;
            if (d1.Year == d2.Year)
            {
                //if the same year AND a leap year, we just need to subtract 1 day
                if (DateTime.IsLeapYear(d1.Year) == true && d1.DayOfYear < 60)
                {
                    LeapValueToSubtract++;
                }
            }
            else if (d1.Year != d2.Year)
            {
                if (DateTime.IsLeapYear(d1.Year) == true && d1.DayOfYear < 60)
                {
                    LeapValueToSubtract++;
                }

                int holder = d1.Year;
                holder = holder + (4 - (holder % 4));

                for (int i = 0; i < 10; i++)
                {
                    if (holder < d2.Year)
                    {
                        LeapValueToSubtract++;
                        holder = holder + 4;
                    }
                }
                if (DateTime.IsLeapYear(d2.Year) == true && d2.DayOfYear > 60)
                {
                    LeapValueToSubtract++;
                }

            }
           
this.lblLeapYearDaysToSubtract.Text = Convert.ToString(LeapValueToSubtract);

#endregion



iPhone App Store Must-Haves

iCam on iCam
Field Runners on Fieldrunners 
Bejeweled 2 on Bejeweled 2
Tetris on TETRIS®
iDracula on iDracula - Undead Awakening


What I'm loving listening to right now.

 Brother Love: Album of the Year Brother Love - Album of the Year

Nerdnology

Welcome to www.Nerdnology.com, a blog about technology, gadgets, science, Macs, PCs, and pretty much everything else geeky or nerdy.

Support


Sign in