Browsing Category: .NET

ASP.NET to officially support jQuery

Wednesday, October 1st, 2008

Check it out - a blog post from Scott indicates that .NET, and Visual Studio, will support jQuery!

Scott says

“I’m excited today to announce that Microsoft will be shipping jQuery with Visual Studio going forward. We will distribute the jQuery JavaScript library as-is, and will not be forking or changing the source from the main jQuery branch. The files will continue to use and ship under the existing jQuery MIT license.”

This is exciting news for developers, as jQuery will help fill the noticable gap in ASP.NET to be truly client rich. I know things I’d like to do, such as supporting dragging and dropping of elements in a grid (a la Netflix queue), are not possible with the out-of-the-box version of Studio / ASP.NET.

I’ve played with ASP.NET AJAX, but found the performance to suck, and the component set to be ugly and difficult to style. At work, we use a popular third party component set called Infragistics, and their components do perform well, but they are not free. I’m excited to hear about jQuery support because I think it will really open up the playing field for those who can’t afford third-party component sets.

Do you use jQuery? What do you think of it? Does it perform well? Do you think it will take off for the .NET world?

ASP.NET: What To Do If You’re A n00b

Monday, September 29th, 2008

If you’re reading this article, either you’ve already started coding ASP.NET pages or you’re thinking about learning it. There’s a lot to know in the .NET world. Few of us are truly experts, even if we hold certifications. I’m always learning something new, and I’ve been developing with .NET for 3 years now.

There is so much to know and there are so many sources of knowledge, that it can seem like a daunting task to jump in and start doing useful things. Your mind might spin, and you don’t know where to start. No worries, there is a method to the madness. Grab an IDE, a practice database, and start reading…

Getting an IDE

Get Visual Studio (costs money, sometimes provided by your company) or Visual Web Developer Express (free download here from Microsoft). I don’t know any serious ASP.NET developer who isn’t using one of those tools, so anyone offering you help will assume you have one. I’m sorry, but Dreamweaver sucks for .NET development. If you were using it for classic ASP, you’ll want to ditch it for ASP.NET.

When you get Studio or Web Developer Express, you’ll be prompted to download and install the .NET Framework if you haven’t yet done so.

Get a database to query

You can download SQL Server Express edition from Microsoft for free. Grab the AdventureWorks database as well as the older Northwind database. While Northwind was written for SQL Server 2000, it mounts just fine for 2005, and its data structure is far simpler and easier for a new person to work with.

Actually, I’m not new and I still prefer Northwind for its simplicity, as I usually use it for testing and learning .NET things, not database things. If you want to be a DBA, use AdventureWorks. If you want to learn to code .NET, use Northwind.

Learning the .NET platform

Now that you have the basic developer tools, you’re ready to dive in and learn to code.

  1. Read the basic tutorials on the ASP.NET QuickStart guide. Read it all at a high level first and let it sink in. After reading and digesting, go back through them again and actually follow with Visual Studio or Visual Web Developer Express to make them work.
  2. Check out the beginner tutorials on w3schools.com
  3. Get familiar with the MSDN documentation. You should know how to check out members, methods, properties, and events for the objects you use. For example, if you need to know how to get a record count when you’re using SqlDataReader, you pop over to the documentation and look up the class. If you look under Members, you will see a RecordsAffected member; click on that link and scroll down to Remarks, you will see details on how it works, and a caveat that it only works after all rows are read and the reader has been closed.
    I believe that learning to read and use the MSDN documentation is the single most important skill you will need in order to develop with .NET.
  4. Visit the walkthroughs on MSDN. There are thousands of walkthroughs, so you’re sure to find some that apply to what you need to do. Nearly all ASP.NET sites deal with CRUD operations with a database, so be sure to check out the Basic Data Access in Web Pages walkthrough.
    If you find yourself thinking that a particular task you’re trying to accomplish is far too complicated you’re probably doing it wrong. Check these walkthroughs to make sure you’re Keeping It Simple.
  5. Start lurking on forums.asp.net. The forums are a GREAT place to learn. I don’t know about you, but I have a hell of a time coming up with problems to solve, especially since I know how to do the basics already. I learn by doing, so I need a task to complete. As you lurk, see how many of the questions you can figure out the answers to. Try to solve the problems yourself, even if you never post a thing. Follow the answers given by others.
  6. Get RSS feeds / bookmarks of your favorite developer sites. Some of my favorites, in no particular order: Code Junkies, The Daily WTF, DevX, CodingHorror, StackOverflow, Digg.com Programming, ITToolbox,  and C# Corner.

Do you have any more pointers to share with us? Sites that helped you learn and grow as a developer? Share them in the comments! Did you find this article helpful? I write these in order to help my fellow developers, so if you found it helpful, I’d love it if you shared it and passed it along. Thanks!

C#.NET: Getting query results with SqlDataAdapter

Friday, September 19th, 2008

While the SqlDataSource control is a nifty little thing, oftentimes a developer has to have more control over the page lifecycle (asp.net) or doesn’t want to use a BindingSource (winforms). Instead, you just want to query your database and get a DataSet back. Maybe you’re building a data access layer, or you’re using someone else’s framework, or it’s just biting you in the butt that binding with SqlDataSource fires off events in a completely unintuitive order.

We’ve seen how to execute a query that just returns one thing with ExecuteScalar. Now, though, we need entire results. And for whatever reason, we want a DataSet, not a SqlDataReader.

Luckily for us, querying and getting results into a DataSet using SqlDataAdapter is extremely easy, and works the same both in web forms and winforms. The following code is a full working example for querying Northwind (I just can’t get used to AdventureWorks yet, sorry) and getting a DataSet back. You can Bind a DataGridView to it, or a GridView, or loop the tables, or whatever.

Notes:
1. You have to have a using System.Data and using System.Data.SqlClient.
2. Your connection string should normally be stored in Settings (winforms) or web.config (web forms). In this example, I was playing with winforms, so it’s a Setting. For web forms, you’d use ConfigurationManager.ConnectionStrings.

 
private void button1_Click(object sender, EventArgs e)
{
 DataSet myDataSet = new DataSet();
 using (SqlConnection sqlconnection = new SqlConnection(Properties.Settings.Default.Northwind))
 {
 SqlCommand command = new SqlCommand("select * from products", sqlconnection);
 SqlDataAdapter adapter = new SqlDataAdapter(command);
 adapter.Fill(myDataSet);
 }
}

It’s really that easy. Happy coding!

If you found this post helpful, please spread the love and share using one of the bookmark links below. Thanks!

C#.NET: Sorting A Generic Collection

Saturday, September 6th, 2008

This is a sample console application demonstrating a quick way to sort a generic collection. The relevant line is

list.Sort(delegate(Person x, Person y) { return String.Compare(x.LastName, y.LastName);});

The example sorts a List of Person objects by the LastName property.

using System;
using System.Text;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class Person
    {
        private String firstName;
        public String FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }
        private String lastName;
        public String LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        private int age;
        public int Age
        {
           get { return age; }
           set { age = value; }
        }

        public Person()
        {
        }

        public Person(String fname, String lname, int age)
        {
            this.FirstName = fname;
            this.LastName = lname;
            this.Age = age;
        }

        public override string ToString()
        {
            return String.Format("{0}, {1} Age: {2}", this.LastName, this.FirstName, Convert.ToString(this.Age));
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>();
            list.Add(new Person("John", "Doe", 32));
            list.Add(new Person("Jane", "Doe", 30));
            list.Add(new Person("Bugs", "Bunny", 50));
            list.Add(new Person("Yosemite", "Sam", 60));

            // display current list
            Console.WriteLine("BEFORE SORT");
            foreach (Person p in list)
            {
                Console.WriteLine(p);
            }

            // sort by Last Name
            list.Sort(delegate(Person x, Person y) { return String.Compare(x.LastName, y.LastName);});

            // display after sort
            Console.WriteLine(Environment.NewLine + "AFTER SORT");
            foreach (Person p in list)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine();
            Console.Read();
        }
    }
}

The logged-in user’s AD name and group [winforms]

Wednesday, August 27th, 2008

We use Active Directory where I work. We set it up with groups; so we have a Programmers group, a Manager group, and so on. Users belong to these Active Directory groups.

So when I needed to build a small windows forms application (C#.NET) that would allow users from different groups to do different things, I had to figure out how to tell if the user was in a particular group.

It’s pretty easy to get the user’s name. The username you get is the person who is currently logged in to the computer and running the application.

using  System.Security.Principal;

string user = WindowsIdentity.GetCurrent().Name;

The string “user” will have the logged-in user’s name, including the domain, i.e. “domain\aeinstein”. I didn’t need their username, though. I needed to know if they were in the Programmers or Managers groups.

To see if that user is in a particular AD group (role), you might see that there is a Groups member of the WindowsIdentity object and look there; unfortunately, those are just the SIDs, which are a jumble of numbers that make little sense to us mere humans without looking them up in AD individually. If you want to tell if AEinstein is a member of the “Programmers” group, though, you can do this instead.

WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

if  (principal.IsInRole("Programmers"))

Happy coding!