Tuesday, September 29, 2009

The Chronicles of George

If you have some time to fall off your chair in tears laughing, this is a must read.



Friday, July 3, 2009

Generate A Schema from a SQL Server Table

Xml Serialization can help if you have to save some very complex data from the web. This is the process:
  1. Post an xml document representing the business object(s) of what you want to save. This should conform to a valid schema (which I'll show you how to generate in just a second).
  2. Deserialize (unmarshall) the xml document on the server.
  3. Save the data to the database
  4. Return something (depends on your specific requirements).
So, to start with, you'll need a valid schema for the tables(s) to which you want to save data. Here's the code (in the form of a console application) to generate those schemas (unless you have some sick affinity with writing these by hand):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace GenSchema
{
class Program
{
static void Main(string[] args)
{
string constr = @"Data Source=YOURSERVER\;Initial Catalog=YOURDB;Integrated Security=True";
string table = "yourTable";
SqlConnection conn = new SqlConnection(constr);
using (conn)
{
SqlDataAdapter sql = new SqlDataAdapter("SELECT * FROM " + table, conn);

sql.TableMappings.Add("Table", table);
DataSet ds = new DataSet("NewDataset");
sql.FillSchema(ds, SchemaType.Mapped);
ds.WriteXmlSchema(table + ".xsd");
}
}
}
}

Of course, you would fill in the information for your connection string and the table with which you want to work.

Next, to deserialize the data, you must pass in an xml document that conforms to the schema you just generated. That may require some testing, but once you get it right you're golden. If you haven't already, use the xsd.exe utility to generate the C# class from your schema. You'll notice that there are various xml attributes assigned to each property. These will be used in the deserialization process. If you already have the classes (if you're using LINQ to SQL, for example) then simply rename this new class to something else. It's only purpose is to hold the deserialized information temporarily anyway. Here's the code to deserialize your data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
namespace GenSchema
{
class Program
{
static void DeSerialize(string xDoc)
{
XmlSerializer ser = new XmlSerializer(typeof(MyObject));
XmlTextReader reader = new XmlTextReader(xDoc);
MyObject obj = (MyObject)ser.Deserialize(reader);
reader.Close();
// Save using whatever method you choose.
}
}

Certainly, your situation will require some modifications to the above code, but this should at least get you started in the right direction. Hope it helps.

Wednesday, May 20, 2009

Right From the Get Go

Ever hear someone use the expression "Right from the Get Go"? What does that mean? Get Go? What am I supposed to be getting? Where have I gone? Well, after a little research, I have discovered the roots of this seemingly silly expression.

Turns out that just before the Renaissance, it was customary to start off large projects with gifts exchanged between all project members. These gifts would generally be of the exotic type - silk, exotic plants, even exotic animals. One of the most coveted gifts, usually only given on the eve of the most important projects, was the gecko. Yes, the little lizard of Geico fame. Soon it became so customary to receive one of these cute reptiles that the giving of a gecko soon became associated with the start of any substantial project - temples, major works of art, opera, etc. Over the centuries, the expression "Right From the Gecko" was transformed through translation and dialect (and perhaps lack of geckos) to "Right From the Get Go".

There you have it.

Thursday, April 30, 2009

Resolving Cannot generate SSPI context.

While there are some pretty complete explanations regarding this error (see links below), if you're looking for a post that forgoes the lengthy explanations and just gets to the good bits ...

You are out of luck on this one :)

That being said, I know that I would have benefited from someone mentioning some of the more basic places to look. In my team's case, the SQL Server account that our web app was using had the password expire on us. When we set up the account we inadvertently neglected to check off the bit about having the password never expire. In a production environment that's probably a good thing. But this is our test server.

Here are those helpful links I mentioned earlier:
How to troubleshoot the "Cannot generate SSPI context" error message
Cannot generate SSPI context” error message, when connect to local SQL Server outside domain