Updating Dataset with Foreign key

Posted on: Wed Aug 27 18:21:20 -0700 2008. Updated on: Wed Aug 27 18:21:20 -0700 2008.
Category: DotNet

So, let's say we have a Dataset for orders and within this dataset we have two tableadapters, one being the master_orders table and the other the child order_details table. The two table adapters are connected by a foreign key relation.

First thing, when you configure the table adapter query, hit the "advanced" button at the bottom and check all three options. Note that the query must be one that allows for Updating. If not these checkboxes will be greyed out.

Now we can have some sample code. The DSOrders contains both tables that need to be inserted/updated:
public DSOrders SaveOrder(DSOrders order)
{
// declare the master and child table adapters
    DSOrdersTableAdapters.ordersTableAdapter ordersTa = new DSOrdersTableAdapters.ordersTableAdapter();
				
    DSOrdersTableAdapters.orders_detailTableAdapter detailsTa = new DSOrdersTableAdapters.orders_detailTableAdapter();

// run update for master
    ordersTa.Update(order.orders);
// do a foreach loop and set the parent row
    foreach (DSOrders.orders_detailRow row in order.orders_detail.Rows)
    {
        row.SetParentRow(order.orders[0]);
    }
// run update for child details table
    detailsTa.Update(order.orders_detail);

    return order;
}

By the way, this would be a good example of when to use Transactions. Look for my post on using transactions

Split a string with multiple characters or newline

Posted on: Tue Aug 26 12:09:45 -0700 2008. Updated on: Tue Aug 26 12:09:45 -0700 2008.
Category: DotNet

It took me a while to figure this out but it is quite useful, specially when you want to split on a windows newline, ie. \r\n

strint str = "first line \r\n another line \r\n blank";

string[] array = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);

Now you can iterate through array and have the lines nicely split. By the way, this is faster than using regex for relatively short strings, under 400 chars.

Viewstate and Session

Posted on: Wed Aug 13 11:35:31 -0700 2008. Updated on: Wed Aug 13 11:36:38 -0700 2008.
Category: DotNet

When needing to store data for use in a different postback we can use either a session or viewstate key.
The Session object is kept on the server and can be used in different pages. So if you need an object that will always be available no matter where in your web application the user is, you should use the Session.
The Viewstate is kept on the client, and is actually encoded into the html page that is spit out to the client. It is only available in the page that it was defined. You can postback to the same page and have access to the defined viewstate but if the user goes to a different page and comes back, the viewstate will be gone.

Using a Session:
// setting it
int number = 25;
Session["key1"] = number;
// retrieving
int num2 = (int)Session["key1"];
// note how you must type cast it to get it out correctly

// you can also put in any serializable object, ie a datatable.
DataSetClients.ClientsTable table = WebService.GetClients();
Session["key2"] = table;
// to retrieve simply cast it
table = (DataSetClients.ClientsTable)Session["key2"];

// check if Session is defined and exists
if(Session["key1"] == null)
{
    // not defined
}

You can interchange the above for ViewState["key3"] and get the same result.

Request.Redirect vs. Server.Transfer

Posted on: Wed Aug 06 14:49:37 -0700 2008. Updated on: Wed Aug 06 14:49:37 -0700 2008.
Category: DotNet

I often forget which one of these to use in what scenario:

Server.Transfer("~/admin/someurl.aspx");

Request.Redirect("/control/anotherurl.aspx");

In essence, they do the same thing, simply take you to the page you specify as a parameter. But they do this in different ways.

Request.Redirect actually sends a 304 and loads the url as if you typed it in and hit enter. You should use this when you need to pass querystrings in the redirection url. The url at the top will actually change, so any previous querystrings will not stick. Also, you cannot use the PreviousPage c sharp command to have access to variables in the page doing the redirection.

Server.Transfer on the other hand does not hit the client. The url in you browser window will not change although you will be in a different page. This means previous querystring will stick. This also means you shouldn't use it to pass querystrings in the redirection url. The cool thing about it is that you can use PreviousPage on the page you transferred to so as to get access to variables in the transferred from page.
In load balanced scenarios, Server.Transfer will actually keep the object and the client connected to the same sever. If you use Request.Redirect the client could end up requesting from another server. Depending on what it is you are trying to do, keep this in mind.

Enum is for Enumeration

Posted on: Fri Aug 01 16:08:24 -0700 2008. Updated on: Fri Aug 01 16:15:56 -0700 2008.
Category: DotNet

Enumerators can come in handy anyplace where you need a list of items that could be passed in as parameters. This way you avoid using a string or an integer that could get messed up and cause a run time error. Declared enums provide intellisense in VS so they are quick to code too.

An example would be user types or groups, ie Admin, public, editor, etc.
In many cases it is better to have types databound to sql database but for simpler things an enum will do.

public enum Groups
{
   Unknown = 0,
   admin = 1,
   editor = 2,
   everyone = 3
}

int admin = (int)Groups.admin;
// the above would return integer with value 1
Note that I define the integer value of each item and start with Unknown = 0. There is a bug that causes an enum not to travel well through web services. So, if you are declaring an enum in a web service and want to access it on the client, be sure to start with 0.

Generics and Arrays

Posted on: Tue Jul 22 15:43:19 -0700 2008. Updated on: Tue Jul 22 15:46:55 -0700 2008.
Category: DotNet

Quick how to on using generics and arrays:
To declare a new generic list and then do operations with it:

List intsGeneric = new List();
// Add item to list
intsGeneric.Add(234);
// convert to array
int[] intsArray = intsGeneric.ToArray();

Note you can create a generic of any type of object, including your own objects.

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.

Categories