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!

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


How I Built and Launched my Blog/Podcast Social News Site in one weekend

clock December 15, 2008 19:25 by author Corby

I've had an idea about creating a Digg-style social news network called www.BlogFloat.com devoted to New Media, bloggers, podcasters, video podcasters, social networks, and the like, for a while now and until recently couldn't muster up the man hours it would take to create one from scratch using the tools I am most familiar with (Visual Studio and ASP.NET). After the extensive programming I did on my Twitter-like site, www.bubeasy.com, I was sure this idea would remain in the idea realm and never materialize (like oh so many of my other concepts).

In search of an alternative route, I recently stumbled onto an article on Digg titled How to Build and Launch a Social News Site in 21 Days.

After reading the article and doing the mental calculations on how I could grab 21 days worth of time from my normally hectic schedule, I set out to research a bit more about the Content Management System featured in the article, Social Web CMS. I came to the conclusion that due to the fact I would be using a shared hosting account at GoDaddy and not having to run my own server (which would involve installing PHP, mySQL, and who knows what else), I should be able to get something up and running over a weekend.

Step one was to download and install the files on a free hosting GoDaddy account I had associated with another domain name. I wanted to test out the SWCMS before I plopped out any cash for the adventure and since I already had a Windows hosting credit, I thought why not? Unfortunately, after hours of heartache and visits to the SWCMS forum, with lots of support from a variety of folks over there, I opted to go ahead and buy the domain name I wanted, www.BlogFloat.com, along with some Linux hosting.

From there, I uploaded the files (I had some trouble using Filezilla on a Mac, as it seemed to only be willing to upload one sub-directory....if there were sub-directories within sub-directories, I had to manually upload them), did some slight tweaking, and then set out to Admin the new site. With great pleasure, everything seemed to work quite well.

I set out to try to tweak the graphics of the site by finding some Pligg-based templates on the web. After trying this one and that one, finding that things like the nav menu became broken or that certain pages wouldn't load at all, I re-uploaded the original files and basically started from scratch. Actually, I went through this procedure several times over the weekend as any time I seemed to tweak anything with a non standard template, things went to hell. Not that there is anything wrong with SWCMS, nor the templates, but I have been in the .NET world for a very long time and my PHP skills are foggy.

Administering the site proved simple, too. There is a module store that allows you to easily add existing modules to the new site, pretty much every setting for the site you can imagine is located in the Language menu, and the only notepad editing I had to do for the entire site was to re-write the FAQ page to suit my site's needs.

So over a weekend, purchasing the domain name and hosting on a Friday, tweaking, testing, and reloading from scratch a few times on Saturday, Sunday rolled around and I was pretty happy with the admin settings and layout (the default template “yget” is just pretty, I like it). I spent the day attempting to upload blog post links, editing a bit more in the Admin section, and that was it.

I showed off my new site to a few friends at work and was met with “wow” and “how long?” responses. The next step for the success of the site is to build the user network. My intention is to hit up every podcaster forum I can find, contact podcasters I know personally and ask for their support, and somehow I need to get the word out to the blogosphere, as such users would really, really find a site like this handy.

Imagine as a blogger that you are able to write a post, then post it at www.BlogFloat.com with a nice description and rise in popularity on the page based on the content of your writing. If people like it, you get hits. If not, then you try again with your next blog post.

I'm exicted about the future of www.BlogFloat.com. If anyone is interested in more details on how I made it work, feel free to contact me. Or, heck, just write a blog post, post it over at BlogFloat.com and I'll see it!

-Corby-

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Captcha, can simple random numbers in C# be better than images?

clock December 12, 2008 13:32 by author Corby
I'm curious if a little random number generation would do the trick or if the same robots that can read the Captcha's could figure out this, too.  I guess I don't know enough about how the bad guys are able to defeat image Captchas on websites...but if you've got a .NET site, creating some simpl code tied to your comment or blog post submission button that generates random numbers and checks to see if the end-user can do the simple math would be an easy way of stopping bots from comment spamming.

This has always been my thought...and something I could whip up in 5 minutes with Visual Studio and C#.

int Rand1         //Random #1 (1-20)
int Rand2         //Random #2 (1-20)
int RandSign     //Random math sign (1-2).  If 1, sign is plus, if 2, sign is minus.

Display
Random #1    Random math sign    Random #2
Type Answer here: ______
Submit Button

when the button's clicked:
if (Convert.ToInt32(this.txtRand1.Text) + Convert.ToInt32(this.txtRand2.Text) != Rand1 + Rand2) ||
(Convert.ToInt32(this.txtRand1.Text) - Convert.ToInt32(this.txtRand2.Text) != Rand1 - Rand2)
{
      //Failed to match, probably robot, do not let them in
}
else
{
     //They did good math, probably human, let'em in.
}

Are these Captcha defeating robots able to screen scrape or something and that's why something like this doesn't seem like it's implemented anywhere on the web?  Any thoughts on why this wouldn't work are welcome.

-Corby-

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


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

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


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