posts - 42, comments - 11, trackbacks - 4

My Links

Archives

Post Categories

Web Dev

Saturday, November 15, 2008

Pulling Web Content with C#

A simple way to perform a 'wget' on a url with c#. This method will return the 'html source' of a webpage.

static string GetContent(string url)
{
WebClient client = new WebClient();

client.Headers.Add("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
data.Close();
reader.Close();

return s;
}

posted @ Saturday, November 15, 2008 1:05 PM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Saturday, November 01, 2008

SVN + BSOD = Fresh Checkout

Note that if you get a BSOD during a commit/update its quite possible a fresh checkout is in order. This was definitely the case for me… Commit often :)

(and replace RAM)

posted @ Saturday, November 01, 2008 8:42 PM | Feedback (0) |

kick it on DotNetKicks.com

Friday, October 17, 2008

Recover Database Schema with DBML (SqlMetal)

I upgraded SQL on my laptop and during the process managed to 'lose' my database…. Total n00b move. I couldn't recover the database file (it was over a gig and was quickly written over). Determined not to recreate the work since the last backup, I kept searching for a solution. Then it hit me; pull the database from the data context (dbml) I created with SQLMETAL. Simply call CreateDatabase(), and 'bam' database recovered. Whew!!!

posted @ Friday, October 17, 2008 3:52 PM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Monday, October 13, 2008

LINQ to SQL Debug Visualizer

If you use LINQ, check this out, it's definitely worth the time. You can execute LINQ statements at the break point.

http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

"One of the nice development features that LINQ to SQL supports is the ability to use a "debug visualizer" to hover over a LINQ expression while in the VS 2008 debugger and inspect the raw SQL that the ORM will ultimately execute at runtime when evaluating the LINQ query expression."

posted @ Monday, October 13, 2008 7:16 AM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Thursday, October 09, 2008

X Server for Windows, Xming

Running programs remotely through ssh is great with Linux or Mac OS X with the –X flag. Not quite as easy with Windows. I lean on PUTTY for my ssh, which of course does not come with an X Server. Xming will piggy back with Putty to run an X Server on windows and allow you to remote the application with the –X flag. The application has worked great for running GUIs off the HPC through ssh.

 

"Xming is the leading free unlimited X Window server for Microsoft Windows (XP/2003/Vista). It is fully featured, small and fast, simple to install and being standalone native Microsoft Windows, easily transported portable as a Pocket PC X server."

 

http://sourceforge.net/projects/xming

posted @ Thursday, October 09, 2008 3:44 PM | Feedback (0) |

kick it on DotNetKicks.com

Tuesday, October 07, 2008

Differential Equations in Matlab

Matlab may be used to solve differential equations symbolically as well as numerically. Well, actually Maple is being used behind the scenes. This can be seen whenever an error occurs with the solver 'Error using ==> maple at 129'. Regardless, say you wanted to solve for a spring mass damper system under a forced oscillation:

 

m*d(d(x))+c*d(x)+k*(x)=f*cos(w*t)

 

The solution is lots of fun to do by hand, but faster and easier in Matlab using dsolve. Dsolve calls Maple to symbolically solve the system. The solver can solve for a single equation with boundary conditions or for systems of equations. For this case its one second order non harmonic with two initial conditions (I will set the initial position to 0.05 to keep the solution 'interesting').

 

dsolve('m*D2x+c*Dx+k*x=f*cos(w*t)','x(0)=0.05','Dx(0)=0')

 

Running this returns:

1/40*exp(-1/2*(c-(c^2-4*k*m)^(1/2))/m*t)*(2*w^2*m^2*k-2*k^2*m+40*m*f*k+k*c^2-(c^2-4*k*m)^(1/2)*c*k+20*(c^2-4*k*m)^(1/2)*c*f-20*f*c^2)*(-c*(c^2-4*k*m)^(1/2)-c^2+4*k*m)/(-2*w^2*m^2*c^2+8*w^2*m^3*k-c^4-8*k^2*m^2+6*c^2*k*m+c^3*(c^2-4*k*m)^(1/2)-4*m*(c^2-4*k*m)^(1/2)*c*k)/k+1/40*exp(-1/2*(c+(c^2-4*k*m)^(1/2))/m*t)*(2*w^2*m^2*k-2*k^2*m+40*m*f*k+k*c^2+(c^2-4*k*m)^(1/2)*c*k-20*(c^2-4*k*m)^(1/2)*c*f-20*f*c^2)*(4*k*m-c^2+c*(c^2-4*k*m)^(1/2))/(2*m^2*w^2+c^2-2*k*m+c*(c^2-4*k*m)^(1/2))/k/(-c^2+4*k*m)-f*(-cos(w*t)*k+cos(w*t)*w^2*m-sin(w*t)*w*c)/(c^2*w^2+k^2-2*k*m*w^2+w^4*m^2)

 

Fun, but say I have values for m, c, k, f and w…

 

subs(subs(subs(subs(subs(dsolve('m*D2x+c*Dx+k*x=f*cos(w*t)','x(0)=0.05','Dx(0)=0'),'k',2000),'c',20'),'m',5),'w',pi),'f',10)

 

Running this returns:

1/80000*exp(-1/10*(20-(-39600)^(1/2))*t)*(100000*pi^2-35280000-36000*(-39600)^(1/2))*(39600-20*(-39600)^(1/2))/(1980000*pi^2-776160000-792000*(-39600)^(1/2))+1/3168000000*exp(-1/10*(20+(-39600)^(1/2))*t)*(100000*pi^2-35280000+36000*(-39600)^(1/2))*(20*(-39600)^(1/2)+39600)/(50*pi^2-19600+20*(-39600)^(1/2))-10*(-2000*cos(pi*t)+5*cos(pi*t)*pi^2-20*sin(pi*t)*pi)/(-19600*pi^2+4000000+25*pi^4)

 

This is better, but it could be simpler…

 

vpa(subs(subs(subs(subs(subs(dsolve('m*D2x+c*Dx+k*x=f*cos(w*t)','x(0)=0.05','Dx(0)=0'),'k',2000),'c',20'),'m',5),'w',pi),'f',10))

 

Running this after setting the digits to five (by running 'digits 5') returns:

(.22439e-1-.22422e-2*i)*exp((-2.0000+19.900*i)*t)+(.22439e-1+.22420e-2*i)*exp((-2.0000-19.900*i)*t)+.51214e-2*cos(3.1416*t)+.16496e-3*sin(3.1416*t)

 

This is much more manageable. I wonder what it looks like:

 

plot([0:.01:1], subs(vpa(subs(subs(subs(subs(subs(dsolve('m*D2x+c*Dx+k*x=f*cos(w*t)','x(0)=0.05','Dx(0)=0'),'k',2000),'c',20'),'m',5),'w',pi),'f',10)),'t',[0:.01:1]))

 

Running this plots:

Note that there are better ways to plot this. For example, one could take the simplified expression and make it into an expression and use fplot. This will plot faster than substituting for each data point and evaluating the string.

posted @ Tuesday, October 07, 2008 8:51 AM | Feedback (0) | Filed Under [ Technical Computing ]

kick it on DotNetKicks.com

Domain Email for Cheap

For $6.99 a year you can have your own mail domain. Something like yourname@yourdomain.com. The route I chose was to go with http://www.1and1.com to order the domain name. I then use Google Apps' Standard Edition to host the mail (http://www.google.com/apps/intl/en/business/editions.html). The process is fairly straightforward. After registering for the Google app just apply the mail records from Google into the mail records for the domain on 1and1. That's all there is too it, and in a few days your mail will up and running.

posted @ Tuesday, October 07, 2008 8:24 AM | Feedback (0) |

kick it on DotNetKicks.com

Visual Studio 2010 and .NET Framework 4.0 Overview

An overview of Visual Studio 2010 and .NET 4.0 was posted on MSDN. Now, where to apply for the beta…

Visual Studio 2010 and .NET Framework 4.0 Overview

http://msdn.microsoft.com/en-us/vstudio/products/cc948977.aspx

posted @ Tuesday, October 07, 2008 8:11 AM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Saturday, October 04, 2008

Regular Expressions in MS SQL Server with CLR

 

I found a great post on how to add Regular Expression functions to SQL Server through CLR functions and extended the methods slightly. The functions allow queries such as the one below to be performed. I was concerned with performance initially, but a few tests and I am blown away by the speed. The original post can be found here, http://anastasiosyal.com/archive/2008/07/05/regular-expressions-in-ms-sql-server-using-clr.aspx, and my modified code below.

 

select dbo.RegExReturnMatch(UserName, '(\w+\d+)') match from aspnet_Users
where dbo.RegExMatch(UserName, '(\w+\d+)') > 0
order by match

 

The query above will return usernames from the aspnet_users table that start with words and are followed by numbers (Such as ccook123, but not ccook). Try doing this one with 'like' :)

 

 

using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;
using System.Data.SqlTypes;
using System;

public class TextFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlBoolean RegExMatch(SqlString input, SqlString pattern)
{
if (input.IsNull || pattern.IsNull) //nulls dont qualify for a match
return SqlBoolean.False;


//Use the static IsMatch method. This is more performant than creating a
// new instance of Regex as the static method also caches the last expressions we used.
return Regex.IsMatch(input.Value, pattern.Value, RegexOptions.IgnoreCase | RegexOptions.Multiline);
}
 
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExReturnMatch(SqlString input, SqlString pattern)
{
if (input.IsNull || pattern.IsNull)
return SqlString.Null;
 
return new SqlString(Regex.Match(input.Value, pattern.Value, RegexOptions.IgnoreCase |
RegexOptions.Multiline).Value);
}
 
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExReturnMatchN(string input, SqlString pattern, SqlInt32 n)
{
if (String.IsNullOrEmpty(input) || pattern.IsNull)
return SqlString.Null;
 
Match m = Regex.Match(input, pattern.Value, RegexOptions.IgnoreCase | RegexOptions.Multiline);
 
if (m.Groups.Count > n)
return (m.Groups[n.Value].Value);
else
return SqlString.Null;
}
 
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExReplace(SqlString input, SqlString pattern, SqlString replacement)
{
if (input.IsNull || pattern.IsNull || replacement.IsNull)
return SqlString.Null;
 
return new SqlString(Regex.Replace(input.Value, pattern.Value, replacement.Value, RegexOptions.IgnoreCase | RegexOptions.Multiline));
}
}
 
 
 
/*
*
 
CREATE Function RegExMatch(@Input NVARCHAR(512),@Pattern NVARCHAR(127))
RETURNS BIT
EXTERNAL NAME DatabaseMethods.TextFunctions.RegExMatch
GO
CREATE Function RegExReplace(@Input NVARCHAR(512),@Pattern NVARCHAR(127), @Replacement NVARCHAR(512))
RETURNS NVARCHAR(512)
EXTERNAL NAME DatabaseMethods.TextFunctions.RegExReplace
GO

CREATE Function RegExReturnMatchN(@Input NVARCHAR(MAX),@Pattern NVARCHAR(127), @N int)
RETURNS NVARCHAR(MAX)
EXTERNAL NAME DatabaseMethods.TextFunctions.RegExReturnMatchN
GO
CREATE Function RegExReturnMatch(@Input NVARCHAR(512),@Pattern NVARCHAR(127))
RETURNS NVARCHAR(512)
EXTERNAL NAME DatabaseMethods.TextFunctions.RegExReturnMatch
GO

*
*/

 

 

 

posted @ Saturday, October 04, 2008 2:05 PM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Tuesday, September 30, 2008

Sculpture – Model Your Life

A coworker referenced me to the Sculpture project. The project looks to be a huge time saver for Enterprise Application development. It removes the work of doing things such as the CRUDS through the use of models. "Model your life" or "Get your life back". :)

 

http://www.codeplex.com/Sculpture

 

What is Sculpture?

  • Sculpture is a .NET open source Model-Driven Development code generation framework ideal for creating and managing .NET Enterprise Applications.
  • With Sculpture you can model your application components, and then transform this model to deployable components for your favorite technology.
  • Sculpture comes with a host of ready-made Molds (The word "Molds" come from Molding) like (DAAB, NHibernate, LINQ, WCF, ASMX, SQL Server, MYSQL …).
  • Sculpture contains a Guidance Package for building your own Mold or customizes existing ones. If you have a custom architecture, using this Guidance Package, you can build a custom code generator with your favorite Technology to fit your needs.
  • Sculpture can generate any kind of text output using templates (source code, database scripts, web pages, XML, configuration files, etc.).
  • Sculpture raises the level of abstraction, for example the data access layer part in your model may be transformed to NHibernate implementation and with minor changes it can be transformed to LINQ implementation, and in the future can be transformed to "X" framework, which we don't know it now.

posted @ Tuesday, September 30, 2008 11:51 AM | Feedback (0) | Filed Under [ Web Programming ]

kick it on DotNetKicks.com

Powered by: