.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

To Advertise or Not To Advertise?

…that is indeed a troubling question for me and my ecommerce site. Many (if not most) sites on the intartrons leverage Google Adsense to drum up additional funds, and some make a decent profit from it. Up to this point I’ve resisted placing ads on the Columbus Supply website out of my desire to maintain a professional image for the company. We do not *need* the extra funds since we generate plenty from sales. However, we’ve started (not even started, have been) receiving some significant traffic for some time now and I can’t help but think how great it would be to supplement our company income with a bit of ad revenue.

I did a bit of research and discovered sites like amazon.com, woot.com, and buy.com do, in fact, have advertising spots in various locations throughout their site. These are pretty big sites that can be considered somewhat similar to ours. I also noticed that sites like newegg.com, grainger.com, and edarley.com do not have any advertising spots. I have admittedly tried to model a lot of our business off NewEgg’s, since I like the way they do things.

What are your thoughts on adding theme-matching, unobtrusive, responsibly-placed advertisements on the site? Would it ruin the “classiness” or “professionalism” of the site? Would it detract from the items and cause a possible dip in sales? Or do you think that it will provide a non-threatening way to cash in off the casual browser who does not necessarily intend to purchase from us?

October 16, 2008 Posted by | Random | , | 4 Comments

BlogEngine.NET: How To Customize Your Blog With New Class Objects

I’ve been working pretty extensively with BlogEngine.NET over the past few months and have learned a lot about its inner workings. My project (www.madcowultimate.com) has essentially used the BlogEngine.NET platform (connected to a MySQL database) as a base to create a full-fledged website. Of course, all of the customization I’ve done to the core would mean that I am stuck with this version (1.4.5.0) of the platform, but so far I am okay with that. It serves my purposes for the most part, and everything else I can make work to fit my needs.

One of the major parts of customizing the core to shape the BE.NET platform into your custom solution is creating your own business classes. For instance, in my site, I needed a way for my teammates to check the status of practice, since some of them come in from out of town and would probably prefer to know if practice is on or off for that day before driving in. Thus, I created the Practice class in the BlogEngine.Core namespace to populate a widget displaying the status.

How did I do this? Well, I wish it was as easy as 1, 2, 3, but this project is a little more complex than that. I have definitely learned a lot by wading through the code, but I have also made some mistakes and wasted some time trying things out that did not work. I will break it down for you into manageable chunks, but you will still need to know certain things, like SQL (or MySQL), data table architecture, how classes and objects fundamentally work, and a little bit about inheritance and abstraction.

The following will assume that you have successfully created your blog on your webserver and that you are hooked up to a database (I will use MySQL in this example). I would highly recommend you back up your entire project before embarking on any changes that may affect your blog (especially changes to the core).

First things first, open BE.NET in Visual Studios and expand BlogEngine.Core. You should see a list of familiar-looking classes like Post.cs, Page.cs, and Category.cs. To create your own class, you will essentially need to model your class after these. Right-click on BlogEngine.Core and go to Add > Class… and type the name for your new class. For the purposes of this article, we’ll call it MyObject.cs.

At the top where you import classes, you will need to add at least “using BlogEngine.Core.Providers”, in addition to any other classes you will need (you can, of course, add any you need later). This will make it easier to hook your object up to the data provider. Next, put your object in the “BlogEngine.Core” namespace and have your object inherit the BusinessBase, like so:

namespace BlogEngine.Core
{
public class MyObject: BusinessBase
{
}
}

Inheriting the BusinessBase will do a lot of your work for you when it comes to managing your object, like knowing which properties have been changed and saving your data. Next you’ll want to create your constructor. You won’t use this very frequently (more on that later), but you still need to have it:

public MyObject(int id)
{
base.Id = id;
}

After this, create whatever properties you need using the following template:

private bool _myproperty;
public bool MyProperty
{
get { return _myproperty; }
set
{
if (value != _myproperty) MarkChanged(“MyProperty”);
_myproperty= value;
}
}

This alerts the BusinessBase when something has been changed so that it may take the appropriate action when you need to save your data. You can (and should) also create a read-only ID property by just returning the base ID:

public int MyObjectID
{
get { return Id; }
}

Of course you may create your own methods if you would like, but they are not required. The last step in your basic class construction is to override the base methods for data retrieval/storage/deletion. These are abstract methods delineated by the BusinessBase you inherited, so they are required by your class. When you are done, they should look like this:

protected override void ValidationRules()
{
//nothing
}

protected override MyObject DataSelect(int id)
{
return BlogService.SelectMyObject(id);
}

protected override void DataUpdate()
{
BlogService.UpdateMyObject(this);
}

protected override void DataInsert()
{
if (IsNew)
BlogService.InsertMyObject(this);
}

protected override void DataDelete()
{
BlogService.DeleteMyObject(this);
}

You may have noticed at this point that your Intellisense isn’t coming up with the methods “SelectMyObject” or “UpdateMyObject” when you type in BlogService. This is because we need to add them to your BlogService class. This class is created to separate data access and business so any kind of data provider can be used without any change to how the class functions, therefore making it extremely flexible.

Before you modify your BlogService.cs class, you will need to add the abstract methods to your BlogProvider.cs class, which is in the “Providers” folder. This is where you create the outline for the methods you just outlined in your class. You probably can do this before the last step in your class construction (so that Intellisense will show up, thereby preventing any typos) but it’s not necessary so long as you’re keeping track of what you’re doing. Go ahead and add the following somewhere in that class file:

public abstract MyObject SelectMyObject(int id);
public abstract void InsertMyObject(MyObject obj);
public abstract void UpdateMyObject(MyObject obj);
public abstract void DeleteMyObject(MyObject obj);

This will create the framework for data access to your object that will be inherited by the respective providers.

Now go ahead and open your BlogService.cs class located in the “Providers” folder. Don’t worry too much about what is going on here, just know that a lot of work has been done on your behalf so that it is this simple (two lines of code!) to hook up your object to your data provider of choice per action required by your object. If you really want to look, expand the region in this file called “Provider Model” and look at the details. When you’re done adding your static methods for your object, it should look something like this:

public static MyObject SelectMyObject(int id)
{
LoadProviders();
return _provider.SelectMyObject(id);
}

public static void InsertMyObject(MyObject obj)
{
LoadProviders();
_provider.InsertMyObject(obj);
}

public static void UpdateMyObject(MyObject obj)
{
LoadProviders();
_provider.UpdateMyObject(obj);
}
public static void DeleteMyObject(MyObject obj)
{
LoadProviders();
_provider.DeleteMyObject(obj);
}

Finally, once all your structures are completed, you’ll need to add the actual functionality to your data access. This is done in the DbBlogProvider.cs file, also located in the “Providers” folder. As you will see, because this class inherits the BlogProvider class, it is mandatory that it include (and provide the behavior for) the methods you just described in the BlogProvider.cs. This becomes most obvious when you start typing your methods and Intellisense finishes the rest of your method signature.

This is the point at which you should start creating your data tables that are going to be accessed and modified by your blog, as you will need to know what tables to access and what fields to call in your SQL string!

After you have created your tables, you should go back to the DbBlogProvider file and create a region for your object’s data access methods and start filling them in with the appropriate SQL. Since I don’t know what your objects requirements are, I can’t really help you any further than this, other than to tell you to take a look at how other objects’ data is being accessed for the same behavior (for example, look at how a post is selected, inserted, updated, and deleted) and model yours off of theirs. This part probably takes the longest, since you need to know what to get and how to get it, but if you follow the other objects’ examples, you’ll see that you can benefit from a lot of copy and paste action.

After you complete all of your data access functions, you’re pretty much done! Now, how do you get an instance of the object you just built? Not in the typical manner, which would be to use the “new” keyword, but rather using the BusinessBase’s static .Load() function, or MyObject.Load(id);. Of course, once you’re done modifying the core, don’t forget to build it (right-click on BlogEngine.Core > Build)! If you get any errors, be sure to fix them, as your project will not work until the Core can compile.

I’m somewhat certain this covers the basic complexity involved in the creation of a new, custom object. I hope this saves someone some time! I wish I had known this before going into this project, as I know it would have saved me a lot of time and figuring out. Good luck, and let me know if I forgot anything, as I will gladly add it.

kick it on DotNetKicks.com

September 30, 2008 Posted by | ASP.NET, BlogEngine.NET, C#, MySQL, Tips & Tricks, Visual Studio.NET | , , , , | 2 Comments

Google Chrome: First Bug

I like to be an early adopter of new technologies, so when Google Chrome came out, naturally I downloaded it.  There are many features I liked, including the quickness of loading and the ability to turn my Gmail into an application.  However, there are still some problems that need to be ironed out, and until they do, I will not be using this browser for anything at all, save development (yeah, thanks for another browser to check. Ugh).

First of all, Google Chrome isn’t actually a new rendering browser; it’s essentially Apple Safari re-branded. So if your pages work in Safari, typically they’ll work in Chrome. But I don’t use Safari, and now I know that if ever given the chance I wouldn’t. The way it handles things is so dumb compared to IE7. For instance, if I want to add a link in Gmail using IE7, I highlight the text I want to link, click the link button, enter my link, and hit enter, and the highlighted text is now a link.  In Chrome, I follow the same steps, except when I’m done, there’s an additional space after the word. At this point, you’re thinking, you nitpicky a-hole, but the problem with this is, let’s say I want to put a comma next to the linked text. If I try to do that (which I do whenever I process an order), Chrome assumes the comma is part of the link, and then any text following the comma that I type becomes the link. Now if I only linked to things in emails every once in a while, I could live with it, but this is something I do 20-30 times a day. It gets annoying.

There are a plethora of other weird (and admittedly nitpicky) differences. For instance, when holding Ctl and pressing an arrow key to highlight a word, it selects the word and the preceding space. Or when copying and pasting something with formatting, some of the formatting is stripped, some is kept, with no rhyme or reason as to what. Also try pasting anything copied from Excel. Gross.

While yes, these annoyances are small, remember that if I’m going to start using a new browser over one that I’m already comfortable with, it has to provide me with features the incumbent does not without pushing me out of my comfort zone. Even still, due to sheer geekdom and willingness to be on the “cutting edge”, I endured these minor hardships.

Until I started receiving reply emails from people wondering who the hell I was.

I have several email addresses that forward to my main (hub) Gmail account, and I have them set up as “Accounts” within Gmail so that I may send as that email address. I use this feature very frequently, as I like to keep certain emails separate during certain parts of the order lifecycle (ordering, processing, shipping, support, etc). However, I also run several other websites on which I have email accounts that I run into my Gmail account. In IE7, I choose which address I want to send out as and it works, no problem. In Chrome, I pick the one I want to send as, and it picks whichever one it wants, often resulting in a very incorrect FROM address.  Unfortunately, this is a dealbreaker.

So until Google Chrome undergoes some signficant changes (the last one being the most important), I will not be using this browser outside of the development arena (again, thanks for giving me another browser to check, Google).

Has anyone else experienced any other bugs in Chrome? Maybe in certain apps/sites?

September 10, 2008 Posted by | Bugs, Google Chrome, IE7 | Leave a comment

BlogEngine.NET: The First Stumbling Block

Let me start off by saying that BlogEngine.NET is a fantastic piece of software with a ton of awesome features and is relatively easy to use.

That said, I’ve spent nearly all of my free time in the past month building a custom site using the BE.NET framework as a base, and while it’s been frustrating at times (I basically had to teach myself C#), I learned a lot about programming just by getting into the core and messing around and seeing how things are linked up and organized. It’s been working great on my local machine (first hooked up to my local MySQL database and then hooked up to the hosted MySQL database) and I was really excited to upload it and get it going.

I fired up good ol’ FileZilla, took a deep breath, and hit “Upload” and watched with mounting anticipation as the 700-some files in the queue slowly (actually, really quickly, but it seemed slow) dwindled. Finally, it was time! I opened my browser, typed in my URL, and held my breath as IE7’s thinking circle went round and round.

That’s when disaster struck.

The YPOD popped up with an unfamiliar error message: “Unable to find the requested .Net Framework Data Provider.  It may not be installed.” Exqueeze me? What does that even mean?? My first instinct was that there was something wrong with my Web.Config file, so I checked there. Some idiot (read: me) forgot to change over the local DB string to the hosted DB string. Fixed. Uploaded. Ta-da!….. ? What? Still not working? Oy… this is going to be a long night…

Well, I did some digging and found out that my instincts were correct and that there is probably an issue with my Web.Config. I found a CodePlex thread by someone who got the same error and tried their solution, but to no avail. I also created my own thread, but as of this writing, no one has responded to my cry for help. I found this tutorial by Al Nyveldt and my spirits were raised again because he is one of the authors of the damn software, but again they were dashed when his tips were also unable to help me out. I even downloaded his sample Web.Config and applied it to my own.

I downloaded the latest MySQL/ASP.NET connector (5.2.3.0 as of now), placed it in my /bin folder and modified the versions in the Web.Config to match (they are 5.1.6.0 in the Web.Config). Still nothing.

With frustration and utter lack of any idea where to turn, I emailed both Mads Kristensen and Nyveldt beseeching them for advice on what to do. I don’t expect an answer (as I’m sure they get 290352268 emails a day from guys like me who are too dumb to figure out their software), but hopefully they will help me out.

Unfortunately, there is no happy ending to this story as of yet… no magical fix that I can present to you that will get you out of the same rut. But maybe I’ve pointed you in the right direction where someone’s suggestions may help you.  If you have any ideas, please please please post them or email me. If it works, I will definitely post your solution with a link to whatever site you want. I would not be surprised if it’s a one-line fix in my Web.Config that makes me smack my forehead. With a hammer.

Thanks in advance for any help you can provide!

EDIT: I’ve received a bit of help from the wonderful nberardi and we (he) came to the conclusion that my host is running a pre-5.1 MySql/ASP.NET connector (they are running an outdated MySql.Data assembly) [5.0.5.0, to be exact]). The 5.1 release included support for Roles and Memberships, so anything older than that will not work with this engine. Should they update it (or I go elsewhere) I will keep you posted.

8/30: SOLVED! But you’ll never guess what the problem was. You ready for this? My hosting company had a stray character in their Machine.config file that was right by the DbProviderFactories node… no, I’m not kidding. Everything is working fine now… I will post more on this tool later.

August 27, 2008 Posted by | BlogEngine.NET, Bugs, C#, Errors, MySQL | 5 Comments

AutoHotKey: The Greatest Program of All Time

Ok, so I know that this is a .NET blog, but people reading this are most likely programmers anyways, so I figured this would be at least somewhat relevent. If you missed the title of the post, AutoHotKey is the greatest program of all time.

If you are unfamiliar with what it does, basically you can create little shortcuts for yourself using their very simple and well-documented series of symbols and functions that will perform repetitive tasks for you.  For instance, I send a lot of emails every day to customers. Rather than type out “[Columbus Supply] Order: X[whatever] Thank you for buying from Columbus Supply!” into the subject line every time, I have created what’s called a “HotString” that types all that for me automatically! All I do is type “psubj” (a HotString I came up with) and it will type all of that out and then move the mouse to the left right in front of the X. This saves me maybe 10-15 mins a day, which may not seem like a lot, but it does a few things: it ensures there are no misspellings/typos, it reminds you to put the correct order number in, and it ensures consistency in subject lines. I have almost 20 of these scripts ranging from simple to complex (one script I have loads my entire purchase response letter into an email!). With all of these scripts used in a day, I’ve shaved a good 2-3 hours a day off of how long it takes me to process orders (and my typing is pretty good for the most part). Think about what you could do with an extra 2-3 hours a day.

My favorite new script that I came up with is a huge time-saver. I use Excel a lot to keep track of orders and I have several tabs in a templated order form. If you are an Excel user, you may well know that moving between tabs is not as easy as ctl+Tab, as that will simply move you between open Excel files. You actually need to hit ctl+PgUp/Dn to move between tabs. This is a bit awkward on the keyboard (especially on a laptop w/no easy NumPad), so I built a script that will move between tabs when I type ctl+right/left arrow key. I say “built” but it was really really simple. Here’s the whole script:

#IfWinActive ahk_class XLMAIN
^left::^PgUp
^right::^PgDn

Yep, three lines of code to create immensely easier tab paging that only works in Excel when Excel is the active window. The possibilities are literally endless.  There is some minor setup involved, but if you are somewhat computer-inclined (ie, you understand what’s going on in the contents of this blog), it should be fairly simple and straightforward.

What scripts have you come up with that have made your life incredibly easier?

August 26, 2008 Posted by | Tips & Tricks | | 2 Comments

State of the Coder: Code Complete 2, Promo Codes, BlogEngine.NET

I just recently purchased Code Complete 2 by Steven McConnell, and am quite excited to read it, especially after all the coding I’ve done recently.  I’ve heard some great things about the book, and being a self-taught programmer, I’m pretty excited to learn the right way to do and think about some things.  I can’t really review it yet (as I’ve read maybe 4 paragraphs in the preface as of this writing), but I will share some of the tips that I glean from this work as I get through it (more regularly than I have been doing).

I’ve been working on a few projects here, some for business and others for pleasure, and they’ve kept me busy. One of which is the building of a Promo Code engine into my site at www.columbussupply.com. Let me tell you, it’s no easy task, especially since PayPal does not accept negative amounts (pretty hard to apply a discount!), but I’m sure I’ll figure it out in time.  The problem has three parts: 1) how to display the discount to the user (easy), 2) how to track the promo/prevent it from being abused (harder), and 3) how to transfer the information to PayPal/Google Checkout so that the transaction appears seamless to the user (hardest).

This third part has gotten me stuck a little. I don’t want to change the display to the user, but I need to find a way to transmit the discount to PayPal. One idea was apply the discounted amount to the last item in the cart (that has a price higher than the discount) with a note on it that mentions the promo code. That may work, but it may be confusing and is definitely kludgy. Anyone have any ideas how to combat this?

As for my pleasure work, I’ve been hacking away furiously at the newly released 1.4.5.0 version of BlogEngine.NET, which has been pretty fun, for my Ultimate Frisbee team’s website, www.madcowultimate.com (as of this writing, there’s nothing there, but there will be very soon). BE.NET is a great open-source blogging framework that is very well-designed and somewhat easily customizable. The official version is in C#, but there’s also a VB.NET version floating around. I considered the VB.NET version for a bit, but I decided to stick with the C# version for a few reasons, the main being I realize I need to learn C#, and that some of the techniques they use in this app are so advanced that I wouldn’t know what they were in VB.NET or C# or even English. So far I’ve had pretty good success and I’ve learned a whole bunch about the language as well as good coding practices. I would suggest that even if you don’t want to use it, you should download it just to look at how it was built (how often do you get to peek at the work of professional .NET coders?). 

I’ve discovered (reported, and corrected!) a few bugs, added some new features, built my own theme, and created my own widgets (somewhat) successfully. I’ve also opted to use the MySQL option included with the app, which has been awesome for me because that’s the DB I’m most comfortable with.  Overall, I’d rate this app very highly because there are a ton of features that you can use (even though I probably won’t use half of them) and it is so easy to modify.  Occasionally you may have to dip into the Blogengine.Core if you REALLY want to customize things (such as create your own data access methods), but so long as you don’t mess around too much, you should be fine.

Anyone else having a good ol’ time with BE.NET? I’d like to see your work!

August 15, 2008 Posted by | ASP.NET, Tips & Tricks, Google, PayPal, MySQL, C#, Books | | Leave a comment

Visual Studio.NET: How To Comment/Uncomment Large Blocks of Code

Just a quick tip that I find extremely useful: To comment a large block of code (at least for VB.NET, I don’t know if it works in C#, but I assume it does), highlight the area you want to comment out and hold Ctrl and press K and then C. To uncomment, highlight the commented area and hit Ctrl + K + U. The mass uncommenting merely removes the forward-most apostrophe, so if you have actual comments in your commented code that were included in your initial highlighted region, they will remain comments upon uncommenting.

I use this technique a lot for debugging large areas of code.

April 14, 2008 Posted by | ASP.NET, Tips & Tricks, Visual Studio.NET | 9 Comments

ASP.NET: How To Use BulkEditGridview To Save Hours In Database Editing

The GridView is a fantastic, versatile, and powerful tool. Unfortunately, it is also unrealistic in several aspects. Say you have a page in your website’s administrative back-end that is to update all of your prices for your products. With a typical GridView, if you wanted to update, say, 10 of these prices, you will require at minimum 20 postbacks, which depending on the speed of your server can be a lengthy process. Now imagine that you have over 1000 lines to edit. 2000 postbacks, minimum… sound like a daunting task that may take you hours in just waiting for your server to respond?

What if I told you that you could edit an unlimited number of records in a GridView with one click, one postback, and one call to your database, and that only the records you modified would be updated? What if I told you it was way, way easier than you think? You may think I am crazy at first if you have any experience with GridViews, but let me introduce you to the panacea of GridView woes, a little tool created by Matt Dotson called BulkEditGridView (more recently moved to this location on CodePlex). I have implemented this tool into nearly every editing application I have that requires a GridView and it has shaved not minutes or hours, but days of off time waiting for the server to post back.

I have been using this tool for quite some time now, but have waited until I had a full understanding of its power and nuances before I posted about it.  The links I provided you will give you more background on the why and how its production came about (and where to download it), but the documentation on its use is a bit on the scattered side. That’s where I come in!

To give you a bit of perspective, the BulkEditGridView is little more than a custom user control that inherits the GridView. However, the functionality it provides is considered more “real world applicable”, because on binding, it places every row in the GridView in Edit mode automatically. You may then either tab through columns or click through records you wish to edit, and then once you’re done with ALL your editing, you click your designated save button and everything is updated! It also knows which rows were edited and stores them in a Generics List Of(GridViewRow) called DirtyRows, which can be accessed programmatically.

Enough already, you say. How do I use it? Here’s a step-by-step of how I have implemented it. Please note that this is how I have implemented it. There may be better ways, but this is how it has worked for me. In this documentation, I am going to assume you have downloaded and installed the .dll for this control.

Step 1: Add BulkEditGridView to your .aspx page

Remember, it is essentially a GridView, so add it like you would to any other page. You may specify bound columns, template columns, or anything of the like the same way you would a regular GridView. The only extra property you must specify is the SaveButtonId property. This is just a button you have placed on your page that when pressed will make the BulkEditGridView work its magic.

Step 2: Add Save Button to page

As mentioned in the previous step, the BulkEditGridView requires that you specify which button is to be pressed for it to run. Any codebehind for this button is not required, but I typically add some catch-all code for when no edits have been made:

Protected Sub btnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
	If BulkEditGridView1.DirtyRows.Count = 0 Then
		litMsg.Text = "No bulk updates made."
	End If
End Sub

Step 3: Define your RowUpdating Event Handler

This is where all the magic happens. When your Save Button is pressed, if you have any DirtyRows the RowUpdating event will fire for each row in that collection. You should create your RowUpdating event the same way you would for a regular GridView, but include the following modifications:

  1. Declare a Static counter variable (Static count As Integer), and immediately increment it at the beginning of your event handler. This will come in handy later.

  2. Declare a Static sql string variable (Static sql As String) or System.Text.StringBuilder if you are looking to optimize. This is where you will store all of your UPDATE strings for your database so that once you have iterated through all your rows, you may send them to your database to be run all at once. In other words, rather than update your database every time the RowUpdating event is called, return the sql string from your update function that you would typically run and add it on to a growing string of calls to make.

  3. At the end of the RowUpdating event, include some conditional code like:

    If count = BulkEditGridView1.DirtyRows.Count Then
    	Dim rowIndexes As New StringBuilder
    	For Each row As GridViewRow In BulkEditGridView1.DirtyRows	'find out which rows were updated
    		rowIndexes.Append(row.RowIndex + 1 & ", " ) 'real count, not 0 based
    	Next
    	rowIndexes.Remove(rowIndexes.Length - 2, 2)	'get rid of final comma
    	msg = "Rows " & rowIndexes.ToString & " have been updated."
    	AddOrUpdateData(sql.ToString) 'your function to call your database
    	BindGridView() 'your function to bind your GridView
    	litMsg.Text = msg
    End If
    

    The preceding code does four things: it checks to see if it has completed running the RowUpdating for each modified row (remember that static count variable?), it iterates back through the DirtyRows so that you may relay back to the user which rows were updated, it updates your database using one call (the AddOrUpdateData function, which is however you call your database to make updates), and it re-binds your GridView. NOTE: when building your SQL string, remember to end your separate UPDATE commands with a semicolon!

Some notes to consider:

  • If you are using template fields, do NOT include controls you wish to modify (textboxes, etc) inside of a Panel control. For some reason it will not detect them. What I did to get around this (if I wanted to display a panel on RowDataBound based on a variable, for instance) is I created two Literals: one where I would want to place the <div style="display:none;"> and one for the closing div tag.

  • At least for me, Intellisense doesn’t work for the BulkEditGridView in the .aspx sourcecode edit mode, so either know your stuff or drop a regular GridView into your page first, build it the way you want, and then copy and paste the guts into a BulkEditGridView once you’re done.

  • Once you update all your items and rebind your BulkEditGridView, it will show up again in edit mode. To toggle this “Bulk Edit” mode, I have sometimes created one BulkEditGridView and one normal GridView on the same page, and then a button to bind and show one and hide the other. For me it has created a seamless user experience and only costs you one postback.

And that’s it! I know this is a long post, but when you get used to using this amazing control, you will see that it boils down to only a few different things you need to change to get it working for you.

kick it on DotNetKicks.com

April 6, 2008 Posted by | ASP.NET, Generics, GridView, MySQL, Tips & Tricks | | 20 Comments

ASP.NET: Images in DropDownLists?

I am having a major dilemma; apparently there is no easy way to add images to list items in a DropDownList. This to me seems ludicrous. I would like my user to be able to pick an icon from a dropdown and afterwards be able to grab that value to update my database. I’ve seen it done elsewhere, I just don’t remember where. I’ve seen solutions like EasyListBox, but I’m not about to shell out $100 for this little bit of functionality, and I’ll be damned if I’m gonna stare at their advertisements in their demo version. I’ve also seen people suggest a DHTML (CSS/Javascript) solution from DynamicDrive.com, but I was unable to find any sort of applicable solution.

THERE MUST BE A WAY. Please help!

Status: Unsolved
Solution: None yet.

March 15, 2008 Posted by | ASP.NET, CSS, Javascript, VB.NET | | 3 Comments

ASP.NET: How to Integrate Both Google Checkout and PayPal In 3 Steps

Trying to get Google Checkout and PayPal to work with a custom .NET site or solution has been a problem for as long as they’ve been around. If you don’t know what you’re doing, it can be a daunting task, and sometimes even if you do know what you’re doing! Well, I am going to take the guesswork out of this chore once and for all, as I have recently discovered the ultimate way to integrate these payment gateways into your website.

If you’re saying, “Wait a second, haven’t you already written an article on this?” you are correct. However, the solution I present to you here is infinitely simpler and requires fewer moving parts. The only reason I didn’t take the previous article down is because it does present interesting information about nesting MasterPages.

The Solution

In their documentation, PayPal and Google Checkout both mention that you must put their code into your site exactly as they have it, using the HTML forms they way they have structured them. This is not true. There are only a few bits of information that are required, and so long as they are present in an acceptable fashion, your pass-off to these gateways will work smoothly and flawlessly.

  1. Get rid of your form tags. That’s right. Lose the form tags. You will only need your <form runat="server"> tag, no more nested HTML forms. W3C compliancy, here we come! However, don’t lose the information contained in your old <form> tag attributes. You’ll still need some of that.

  2. Create image buttons for Google Checkout and PayPal. While they don’t have to be image buttons (for functionality’s sake), both Google and PayPal will get upset if you have standard buttons instead of their images as checkout buttons. It is in this button where the magic happens – the button has a property called PostBackURL. This is where you will put the action string from your form. For instance, PayPal’s PostBackURL value is “https://www.paypal.com/us/cgi-bin/webscr&#8221;. Google’s will be “https://checkout.google.com/cws/v2/Merchant/%5Byour merchant number]/checkoutForm”. You can put both Google’s and PayPal’s buttons next to each other. Here is some sample code:
    <asp:imagebutton id="btnSubmitPaypal" runat="server" imageurl="/images/checkout-paypal.gif" alternatetext="Purchase Using Paypal" postbackurl="https://www.paypal.com/us/cgi-bin/webscr" />

  3. Create your hidden inputs to transfer information to PayPal and Google Checkout (from the same page, no extra postbacks!). However you dynamically create your hidden input tags, keep doing it. Just keep this in mind: Google Checkout and PayPal have no tags in common except one, the “item_name_x” (where “x” is a number) tag. If this is the same for both Google Checkout and PayPal, leave it alone and only have one! If you must have two (as I do), make sure you put Google’s tags before PayPal’s. This is important because Google reads the first tags that match what it’s looking for and ignores everything else. PayPal will take the last tag that matches its requirements.

    I mention this because Google Checkout has an “item_description_x” attribute that will take additional information about the item where PayPal does not, meaning that PayPal users have to cram all the info about their product into the “item_name_x” tag. Thus, if you change the order, Google will display double information (PayPal’s “item_name_x” plus their own “item_description_x” tag) and PayPal will display only what you want to transfer to Google Checkout (the last “item_name_x” tag).

  4. BONUS: Hook up Google Analytics. For those advanced users who want to hook up their Google Analytics, you still can using this method. The asp:button element has an attribute called OnClientClick. Put the following as the value for this attribute in your Google Checkout image button: setUrchinInputCode();

For those looking for some sample code on how to dynamically generate your input tags, simply create a literal somewhere on the page and populate it with the following function:

Public Function CreateCheckoutTag(ByVal att As String, ByVal value As String) As String
		Dim input As String = "<input type=""hidden"" name=""" & att & """ value=""" & CleanText(value) & """ />"
		Return input
	End Function

Where the CleanText(value) function cleans any possible special characters from the value. Not necessary, but recommended.And that’s it! Three or four easy steps and you’re ready to use both Google Checkout and PayPal Website Payments on your website! I hope this saves someone 60 or 70 hours of their life, as it would have mine had I known about it three years ago 🙂

kick it on DotNetKicks.com

February 27, 2008 Posted by | ASP.NET, Google, MasterPages, PayPal, Tips & Tricks, UpdatePanel | 52 Comments