Skip to main content

Posts

How to get the raw data posted to a Java Servlet

While working in porting GeneCMS to Google App Engine and Silverlight , I stumble at one annoying problem: how to get the raw data that was sent by the browser via a POST. In this GeneCMS port, the silverlight client actually generates all the HTML that the server used to generate. The raw generated HTML is then sent to the GAE application, with some metadata about the generated pages. The GAE servlet then reads the posted data as a string and extract the individual generated pages. Each generated page is then saved into the GAE database to allow the CMS to server the pages. After tweking around in the wrong places, I finally found out that HttpServletRequest’s getInputStream method is an input stream to the raw data posted. So I modified my servlet to get the POST data into a string like this: String posted_data = ""; BufferedReader in = new BufferedReader(new InputStreamReader( request.getInputStream())); String line = in.readLine(); while (line != null) { posted...

How to watch for a file in a folder in VBS and handle locked files

The following Visual Basic Script allows you to  monitor a folder for new files. The script will try to process the file if the file is still locked by the application that created the file, an error will occur and the script will handle it by waiting a growing amount of time and then retrying to use the file. The script will also send an email alert about the error raised: strComputer = "." strPathToWatch = "C:\backups\" Sub SendMailMessage(to_address, subject, msg) dim sch, cdoConfig, cdoMessage sch = "http://schemas.microsoft.com/cdo/configuration/" Set cdoConfig = CreateObject("CDO.Configuration") With cdoConfig.Fields .Item(sch & "sendusing") = 2 ' cdoSendUsingPort .Item(sch & "smtpserver") = "yoursmtpserver" .update End With Set cdoMessage = CreateObject("CDO.Message") With cdoMessage Set .Configuration = cdoConfig ...

Class to get Live Photo and Album Information in .NET

Thanks to the Windows Live Spaces Photo Album plugin project at CodePlex, I was able to abstract the code to get Album and Pictures information from a Live Spaces account. This class serves as a helper when generating javascript code for slideshows containing photos stored at Live Spaces. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using HtmlAgilityPack; using System.Web; namespace LivePhotoDataProvider { public class AlbumInfo { public string URL { get; set; } public string Name { get; set; } } public class PhotoInfo { public string FullSizeURL { get; set; } } public class LivePhotoData { public List<AlbumInfo> GetAlbums(string live_spaces_link) { List<AlbumInfo> albums = new List<AlbumInfo>(); XmlDocument doc = new XmlDocument(); try { doc.Load(live_spaces_link); ...

Using SQL Server’s XML features as a fast ORM

When you need load POCO objects from a SQL Server database in a faster way than LinqToSQL, you may consider using  SQL Server’s XML features.  The FOR XML PATH command can be a really powerful tool to convert your relational data into XML that C# can de-serialize into C# plain objects. The key is to find how C# serializes objects to XML. In order to do that, I did a small test: public class Store { public string Name { get; set; } public int Sales { get; set; } public List<StoreContact> StoreContact { get; set; } } public class StoreContact { public int ContactId { get; set; } public int ContactTypeID { get; set; } } public static void TestSaveToXML(string path) { XmlSerializer serializer = new XmlSerializer(typeof(List<Store>)); TextWriter writer = new StreamWriter(path); List<Store> stores = new List...

Model Generator Helper Released

Model Generator Helper is a small program that will allow you to easily create view models from your logic models by copying the logic model's properties and the properties' attributes. The original purpose of the program was to copy the validation annotations to my view models so that I would not have to copy and paste them. The program can also read the ColumnAttribute of Linq2SQL generated classes and create DataAnnotation attributes for them (e.g.: Requires attribute and the StringLength attribute). Sometimes you want to pass to your views a class different from your raw classes generated from the database. For example, lets say we have this logical model classes: class Payment { [Max(1200)] public int Amount {get; set;} public int PersonID {get; set;} } class Person { [ StringLength (50)] public string Name {get; set;} public int PersonID {get; set;} } And you need a View model for your view that allows the user to edit the name of the pe...

Send Email from C# using Outlook's COM late binding

The following sample code shows how to send emails from Outlook and Exchange using C#. This code works with any version of Outlook because it uses Late Binding to automate Outlook. Parts of the code where taken from other websites. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Threading; namespace LateBindingTest { class OutlookEmailerLateBinding { private object oApp; private object oNameSpace; private object oOutboxFolder; public OutlookEmailerLateBinding() { Type outlook_app_type; object[] parameter = new object[1]; //Get the excel object outlook_app_type = Type.GetTypeFromProgID("Outlook.Application"); //Create instance of excel oApp = Activator.CreateInstance(outlook_app_type); //Set the parameter which u want to set parameter[0] = "MAPI...

Batch file to encrypt connection string section

Many times I have to use SQL Authentication instead of Windows Aunthentication when developing ASP.NET web apps. To encrypt the connection string used you can use the following batch file to encrypt the connection string sections of any asp.net web app: echo Please enter folder where webconfig is: :input set INPUT= set /P INPUT=Type input: %=% if "%INPUT%"=="" goto input echo Folder where webconfig is: %INPUT% aspnet_regiis.exe -pef connectionStrings %INPUT% pause Make sure you have aspnet_regiis.exe's folder in your PATH environment variable.

Sending/Receiving Faxes as Emails in Windows

Most Fax server software allows you to send/receive faxes through email. While looking at the alternatives, I could not find any free, Windows based solution that suited my needs. Most of the software I looked at requires you to have a dedicated smtp server or DNS settings in your local network. I developed Email-to-Fax Server to work with an already established SMTP and POP server (it requires an email account) and decided to make it open sourced at CodePlex . Email-to-Fax Server’s foundation is the excellent Fax .NET library . The program is developed in C# and uses the internal Windows's Fax services as its foundation. Email-to-Fax Server is a windows application that does the following: Sends email attachments as faxes. Sends faxes received as email attachments to a specified address or addresses. It obtains emails from a POP email account and sends the emails' attachments as faxes to the fax number specified in the subject line. It uses a SMTP account to ...

How to enter multiple users into ActiveDirectory 2008 from a CSV

To create multiple users into ActiveDirectory 2008 Server from one single CSV file containing the users login information, you can use the following wsf file: 1: <job id= "main" > 2:   3: <script language= "VBScript" > 4: dim text_out 5: text_out = "" 6: Dim oContainer 'Parent container of new user 7: Dim oUser 'Created user 8: 'Get parentcontainer 9: Set oContainer=GetObject( "LDAP://OU=yourouname, DC=your,DC=domain,DC=local" ) 10: 'Read the file with user information 11: Const ForReading = 1 12: Set objFSO = CreateObject( "Scripting.FileSystemObject" ) 13: Set objTextFile = objFSO.OpenTextFile _ 14: ( "users.txt" , ForReading) 15: D...

How to do control breaks in Excel

One of the problems I find when exporting from RenWeb is that the names of the staff are repeated in every row when exporting to Excel. To make control breaks on the staff name (i.e. show the name only once) in excel you can use the following formula: =IF(B1=B2,"",B2) Change the B to the column that contains the names. The formula will set the cell blank if the contents of the current row in the B column is the same as the one in the previous row.

Windows.Forms’ DialogResult does not have OK as a member

I found this code to show up a “file browse” dialog from C#: OpenFileDialog ofd = new OpenFileDialog(); DialogResult b = ofd.ShowDialog(); if (b == DialogResult.OK) { CopySourceFiles.Text = ofd.FileName; } The problem was that it was giving an error saying that DialogResult did not have a member named OK . So I had to fully qualify DialogResult as the following snippet shows: OpenFileDialog ofd = new OpenFileDialog(); System.Windows.Forms.DialogResult b = ofd.ShowDialog(); if (b == System.Windows.Forms.DialogResult.OK) { CopySourceFiles.Text = ofd.FileName; }

How to make Batch files run in Windows 2008 scheduled tasks

I was getting a 0x1 error every time Windows Server 2008 ran my task. I had programmed the task to run a bat file that would run a jruby script. The messages in the log did not help me at all, so I took a look at my script’s log. The log showed that the script was not being run at all. To fix this problem I had to make sure to enter the folder that contains the script in the start in field when configuring the scheduled task. So even it says it’s optional, make sure you enter a correct value in that field or else, the task will run with windows\system32 as the current working directory.

Problems changing the SSL port number on Tomcat in Windows

I tried changing the SSL port number on my Tomcat server in my Windows Server 2008 machine and I kept getting the following error when going to the https version of my webapp: Socket bind failed: [730048] Only one usage of each socket address (protocol/network address/port) is normally permitted. To solve the problem I did the following: Re-installed apache using the Windows service installer and specifying the JDK’s JRE as the JRE to use. Set the keystoreFile attribute in the SSL connector tag in the server.xml file to a relative path. The path is based in Tomcat’s installation folder, so if you intalled it in c:\apache and specify ./mykey.bin in the  keystoreFile attribute, Tomcat will try to find the c:\apache\mykey.bin file.

How to split a full name in Excel

RenWeb exports the formal name in the form "Smith, John". Sometimes I need to split the formal name field into first name (including middle name) and last name. To get the formula to split the name I found this link from Pearson Software Consulting, LLC . Their formula to get the last name works great for me, but they were splitting the middle name from the first name, so for the first name I changed their formula to the following: =TRIM(IF(ISERROR(FIND(",",A2,1)),A2,MID(A2,FIND(",",A2,1)+1,LEN(A2))))   To use the formula simply have the formal name in cell A2 and copy and paste the formula to another adjacent cell, such as B2. To get the first names of the rest of the people in the list simply use the auto fill values command from Excel.

How to split a full name in Excel

RenWeb exports the formal name in the form "Smith, John". Sometimes I need to split the formal name field into first name (including middle name) and last name. To get the formula to split the name I found this link from Pearson Software Consulting, LLC . Their formula to get the last name works great for me, but they were splitting the middle name from the first name, so for the first name I changed their formula to the following: =TRIM(IF(ISERROR(FIND(",",A2,1)),A2,MID(A2,FIND(",",A2,1)+1,LEN(A2))))   To use the formula simply have the formal name in cell A2 and copy and paste the formula to another adjacent cell, such as B2. To get the first names of the rest of the people in the list simply use the auto fill values command from Excel.

One more tool to convert MS Word text into clean HTML

Windows Live Writer is one of the best tools out there to convert MS Office Word formatted text into simple and clean HTML. To use it, just copy from a Word document and paste into Writer. Writer will covert the text to HTML without the obscure tags, FONTs and CSS styles that you would get if you were saving as HTML from Word. You can download it for free at http://download.live.com/writer .

Ruby and C# code to upload a file to a server that uses FrontPage extensions

I ran into a challenge last week. I had to write a script or program that creates an RSS file and upload it to a IIS server. The program would be be scheduled to run every so often. The IIS server did not supported DAV, but it supported Front Page Extensions 2002. I looked everywhere in the web for sample code on how to upload files to such servers. I found out that MS Internet Publishing is the technology used by Front Page Extensions. I also found out that there was an OLE provider for it and that I could connect to it using classic ADO 2.5. I tried the example provided in Microsoft site, but the code crashed when the ADODB.Stream object opened the stream from the record. The error said something about “Interface not supported” and “InvalidCast”. My old ASP 3.0 book has a sample of writing directly to the ADODB.Stream, so I tried that and it worked but it kept prompting a dialog asking for the username and password even though I had passed those in the Stream’s open method. To ...

FTP not working in a Linux virtual machine hosted in Windows

I have a Linux virtual machine that serves as an FTP server. My problem was that the computers in the network were able to connect to its FTP service only if the computer was part of the Windows Domain. I could not figure out what was going on, since those machines could PING but not FTP the Linux server. After some troubleshooting, I found out that I had enabled the Windows Firewall for the VMware connections and therefore firewalling the traffic to the Virtual Machines hosted in the Windows computer. To solve the problem I cleared the check boxes for the firewall for the VM connections like this:

How to add a MS SQL server 2005 database connection in Netbeans

In Netbeans IDE, I was trying to add a Database connection to a MS SQL server 2005 database. After entering the correct parameters and locating the right jdbc driver (sqljdbc.jar), I was able to connect; however none of the tables showed up. After reading this , I decided to set the value of the schema property to dbo and it worked! To change the value of the schema property in your Netbeans's database connection, do the following: Right click on the connection Click on Disconnect Right click again on the connection Click properties Change the value of the Schema property