Programming: C#


Start New Discussion
Posted At: 11/14/2017 2:55:29 PM
Posted By: Ben Kimball
Viewed: 878 times

If you need to search all tables in a SQL Server database to find any tables that have a certain field name, this script comes in really handy.

Posted At: 7/16/2017 4:18:29 PM
Posted By: Ben Kimball
Viewed: 799 times

The Repository Pattern is very popular these days. In some environments you will be shredded as a know-nothing if you don't use it for every possible project that involves data storage. And not to knock it, when it was first described, it solved a lot of problems with keeping the code involved with database communication organized and maintainable.

Posted At: 1/28/2017 3:15:38 PM
Posted By: Ben Kimball
Viewed: 844 times

Article 1: Singleton Design Pattern in C#

Posted At: 12/6/2016 1:45:53 PM
Posted By: Ben Kimball
Viewed: 892 times

This is pretty neat, I have always played around with building a StreamWriter to write to a text file, but now you can do it with one line of code:

Posted At: 5/12/2016 9:15:43 PM
Posted By: Ben Kimball
Viewed: 1242 times

When using the code first option for Entity Framework, in order to control the length of varchar and nvarchar fields on the actual generated database, you can use one of the two approaches:

Posted At: 3/9/2016 3:36:17 PM
Posted By: Comfortably Anonymous
Viewed: 1844 times think a SOAP message is a nice way to tell someone he needs to take a shower.

Posted At: 9/27/2010 8:50:10 PM
Posted By: Comfortably Anonymous
Viewed: 853 times

Found a excellent set of examples of the basic techniques of LINQ to SQL. Very helpful on those days when you just can't remember those tricky incantations.

Posted At: 6/25/2009 12:17:21 PM
Posted By: Comfortably Anonymous
Viewed: 829 times

While you'd think something like the following code would work, it doesn't and fails with a frustrating "No constructor defined" error:

Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = new MiddleWare.ProjectName.DAL.DataModel.SomeDataRow();

The way to obtain an instantiated row object that belongs to a TableAdapter is as follows:

// First, instantiate a DataTable of the type the new row will belong to:
Middleware.ProjectName.DAL.DataModel.SomeDataTable dtSomeData = new Middleware.ProjectName.DAL.DataModel.SomeDataTable();

// Next, use the DataTable instance to create a new row that is the same "shape" as the DataTable:
Middleware.ProjectName.DAL.DataModel.SomeDataRow rowNew = dtSomeData.NewSomeDataRow();

It's easy as that, intuitive in retrospect, but a mind-twist to get the concept down the first time.

Posted At: 12/2/2008 1:20:54 PM
Posted By: Comfortably Anonymous
Viewed: 877 times

Quite a few times, I've needed to create a new row in a table obtained from a web service.

Posted At: 8/25/2008 9:10:51 PM
Posted By: Comfortably Anonymous
Viewed: 884 times

Not really sure why'd you'd do such a thing these days, but C# does support GOTO, so this guy used it to do a line-by-line conversion of an ancient Star Trek written in BASIC in 1972.

Posted At: 3/30/2007 12:00:22 PM
Posted By: Comfortably Anonymous
Viewed: 1036 times
No Summary
Posted At: 12/21/2006 2:44:44 PM
Posted By: Comfortably Anonymous
Viewed: 1114 times
No Summary
Posted At: 12/18/2006 12:06:56 PM
Posted By: Comfortably Anonymous
Viewed: 1055 times
No Summary
Posted At: 5/1/2005 7:53:53 PM
Posted By: Comfortably Anonymous
Viewed: 1172 times
No Summary
Posted At: 4/15/2005 8:51:52 AM
Posted By: Comfortably Anonymous
Viewed: 1225 times
No Summary
Posted At: 1/5/2005 10:54:30 AM
Posted By: Comfortably Anonymous
Viewed: 1094 times
No Summary
Posted At: 10/24/2004 7:19:50 PM
Posted By: Comfortably Anonymous
Viewed: 3177 times

Finally found out how to do it!  Have been searching (for months) for a way to handle events from buttons dynamically created by code inside a table in ASP.NET. Couldn't find anyone that knew how to do it, couldn't find any web pages with what I was looking for. Got a lot of answers back from posts on other C# message boards, but none of them were correct. I even had several people say that the ability to do so was not present in ASP.NET 1.1 and I'd have to wait and hope it showed up in ASP.NET 2.0.  I had actually given up and believed them for a few months. But decided to jump back into it today and found the correct way of doing so after piecing together clues from all over the web. :)

So now, I'm going to try and explain it the best I can, in my own words so that anyone out there chasing down the same concept doesn't get stuck spinning their wheels like I did.

First of all, it's actually embarrassingly simple in retrospect, but getting there was the real trick. I feel kind of stupid admitting it took me so long to figure it out, but I know I'm not alone - I talked with a lot of people on the net and everyone seemed lost as I was so...

The big trick is to use delegates. That's what slowed me down so much, as most explanations of delegates rely on referencing function pointers in C++, which I've never done a whole lot of C++ and never anything using function pointers, so the explanations did nothing for me.

Even worse, the explanations about delegates always involve declaring a delegate and the example code to do so. Normally declaring a delegate is a GOOD thing. However, in the case what I was trying to do (Deal with the click event from the System.Web.UI.WebControls.Button object) there were two things wrong!!

1. The delegate for handling the click event for a Button, LinkButton, or ImageButton (And only these) is ALREADY DEFINED BY THE SYSTEM. That means that you don't have to create the definition, nor can you look at the code for the definition (Other than in the MSDN documentation), and if you try to create your own, you get all kinds of problems. The pre-defined delegate is called CommandEventHandler. More on that after point 2.

2. I said I was trying to work with the Click event. (Oops) With a Button, LinkButton, or ImageButton you don't want to mess with the Click event (although it is there just like it is for all the other GUI objects, so finding out there's something better to use is even better) - You want to use a different event that only exists for  the (you guessed it) Button, LinkButton, and ImageButton objects. This event is called Command. It fires the same as the Click event, but allows you to pass additional data via the Button.CommandArgument property. (The normal Click event does not support sending this additional data, thus why you should use the Command event instead of the Click event, unless you just need to see if a button was clicked and do not need any additional data about the click.)

OK, with those two misconceptions out of the way, let's actually get into how to do this:

All you really need to do is create a method that requires two objects sent to it when invoked. The first is the 'sender' object, which identifies which object is calling it (The button itself) and another object called 'CommandEventArgs'. So create a method such as:

ButtonHandler(object sender, CommandEventArgs e)
  // Do something

Now, the next thing to do is register the method with the Button.Command event via the pre-defined delegate CommandEventHandler. Say we have a button called newButton. We attach the ButtonHandler method to the Command event of newButton with the following code:

newButton.Command += new CommandEventHandler(ButtonHandler);

Then, with any additional data you need to send to the ButtonHandler method, you add it to the Button.CommandArgument property and it will be sent as part of the CommandEventArgs object that is passed via the delegate. CommandArgument only accepts a string value, so any numerical data to be sent needs to be converted to a string first. In the code example at the end of this article, I am sending both the X and Y position of the table separated by a colon. Since it's a single string, formatting of that string is up to you.

Hopefully this account of my misadventures helps you out!

To try the following example, simply create a new web form, then drag an empty Table from underneath Web Forms in the toolbox to the new form (Leave it named the default name of Table1). Next, drag a label to the same webform, and set its name (ID under properties) to lblInfo.

Here's the example code, enjoy:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

Posted At: 10/23/2004 1:00:39 PM
Posted By: Comfortably Anonymous
Viewed: 1076 times
No Summary
Posted At: 5/13/2004 3:10:24 PM
Posted By: Comfortably Anonymous
Viewed: 1371 times
No Summary
Posted At: 9/4/2003 2:29:53 PM
Posted By: Comfortably Anonymous
Viewed: 1221 times
No Summary