Interesting programming ideas, solutions, and logic that I have used to solve problems or have come across throughout my career.
About Me
- William Andrus
- Northglenn, Colorado, United States
- I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).
Monday, August 27, 2007
How to programatically bind a value to a parameter in a ObjectDataSource
From here I just add the value:
protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
e.InputParameters["IP"] = Request.UserHostAddress;
}
Friday, August 24, 2007
File Download in ASP.Net
So on my download page I have:
protected void Page_Load(object sender, EventArgs e)
{
String filepath =
Request.Params.Get("Author") + "-" + Request.Params.Get("Title") + ".mp3";
if
(filepath.Length != 0)
{
Stream readStream =
MusicManager.GetSong(Int32.Parse(Request.Params.Get("file")));
int Length =
256;
Response.Clear();
Response.ContentType =
"application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=\"" + filepath + "\"");
Response.Flush();
Byte[]
buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0,
Length);
while (bytesRead > 0)
{
Response.OutputStream.Write(buffer,
0, bytesRead);
bytesRead = readStream.Read(buffer, 0,
Length);
}
Response.End();
readStream.Close();
}
}
Of course I have a page that sends in the query string with the necessary info
This seems all simple when it starts working, but it took me 2 hours to get the file to download correctly.
Wednesday, August 22, 2007
Tuesday, August 21, 2007
Interview question: What structure would you use to represent shapes in a database?
I then started thinking about this question, again. The idea of the question was to see what tables and columns would you set up to make this feasible. I wanted an easy solution with minimal work, so I avoided the polymorphism concept and went to a simpler definition of a shape (polygon).
I figure I would make 1 table called SHAPE with 2 columns and make the assumption that the shapes are defined as "simple polygons":
ID int, COORDINATES varchar
The ID is obviously the primary key for each shape.
The Coordinates is a order list of Cartesian coordinates going counter-clockwise.
Now, given a shape, the most common questions asked would be what is the perimeter and area of said shape. I could easily make 2 store procedures to find the perimeter and area.
To figure out the perimeter, I just need to add up the distance from each point, simple enough.To figure out the area of the polygon, I knew there had to be a formula that would do this for me, and thanks to wikipedia I found one:
"The formula was described by Meister in 1769 and by Gauss in 1795. It can be verified by dividing the polygon into triangles".
I was looking around, to see if others seen this question. I did find something similar at http://lists.mysql.com/mysql/207823
If I needed to query the table for certain shapes, then it would be good to add an additional column called: Points int
So, if I needed to query the table for triangles only then I can do a search on points = 3.
Monday, August 20, 2007
Efficient use of Iterators and Delegates
http://msdn2.microsoft.com/en-us/vcsharp/bb264519.aspx
Take the initial uneffecient example:
// First attempt:
public List
{
IEnumerable
List
foreach ( PhoneBookEntry ph in newYorkNumbers)
{
string name = string.Format(“{0} {1}”, ph.FirstName,
ph.LastName);
if ( RecognizePerson( name ) )
peopleIKnow.Add(name);
}
return peopleIKnow;
}
// Fifth attempt:public
IEnumerable
PeopleIKnowInNewYork()
{
IEnumerablenewYorkNumbers =
PhoneBook.FindListFor(“New York”);
IEnumerablenames = Transform (newYorkNumbers,
delegate(PhoneEntry entry)
{
return string.Format(“{0} {1}”, entry.FirstName, entry.LastName);
});
return Filter(names,
delegate(string name)
{
return RecognizePerson(name);
}
);
}
Friday, August 17, 2007
LINQ and AI?
var locals = (from c in customers
where c.ZipCode == 91822
select new { FullName = c.FirstName + “ “ +
c.LastName, HomeAddress = c.Address})
.Count();
What I find really interesting about the possibility of using LINQ, is the idea that the program can look at itself in an evaluation. If you add some artificial intelligence, this idea can be a great help in developing programs that can prove themselves correct in logic. I even think, that this concept can be used, with lots of time and planning, to develop a program that can write other programs given a set of parameters and acceptable outcomes.
The concept of using a database like language in a structure language is a very nice concept. If LINQ could be used to select all number of the type Int32 to represent integer numbers, would be something fun to play with. On this subject I might be a little too much in the dream world at the momement, but I do look forward to the possibilities.
Some sources on LINQ:
http://msdn.microsoft.com/msdnmag/issues/07/06/CSharp30/
http://blogs.msdn.com/charlie/archive/2007/07/26/visual-studio-2008-beta-2-released.aspx
Some info on Lambda Calculus:
http://en.wikipedia.org/wiki/Typed_lambda_calculus
Monday, August 06, 2007
Quick Reference: Raise Events in C#
Declare:
private delegate void OnMouseOverDelegate(object
sender, System.EventArgs e);
private event OnMouseOverDelegate OnMouseOver;
After InitializeComponent(); :
this.OnMouseOver += new OnMouseOverDelegate(my_MouseHover);
Function:
private void
my_MouseHover(object sender, System.EventArgs e)
{
}