ASP.net MVC Archives - Page 5 of 5 - Digital Tool Factory blog ASP.net MVC Archives - Page 5 of 5 - Digital Tool Factory blog

The Digital Tool Factory Blog

A simple way to create a Microsoft Word document from a template in Asp.net/C#

Will code for food I recently had the need to create a Microsoft Word document from a template. I initially tried using Office Web Components and Interop but all that really wasn’t worth the trouble.  I wound up doing global replacements in the html of html   Here is how I did it:

  1. Open up your template document in word, and put any target areas in special Brackets, the text would read something like “I, [FULLNAME] do hereby affirm that”.  I found that the target area had to be in uppercase, I’m not sure why
  2. Go to “Save As Html, Filtered”, and save it in a writeable directory.
  3. In your C# code, first load the entire document into a string
  4. Convert that string into an instance of StringBuilder
  5. Use the StringBuilder Replace function, it would look something like this –  sb.Replace(“[FULLNAME]”, strFullName);
  6. Create a TextWriter, and write the StringBuilder to it – make sure you’re saving it with a “.doc” extension.
  7. Close the TextWriter

All told, the code looks like this

//Create a new filename
string strFileName = System.Guid.NewGuid().ToString();
string strPathRoot = “d:domainsMyDomainWord”;
string strPath = strPathRoot + “TemplateHtml.doc”;
string str = System.IO.File.ReadAllText(strPath);
StringBuilder sb = new StringBuilder();
sb.AppendLine(s);
//replace the text
sb.Replace(“[FULLNAME]”, strFullName);
//save it out
TextWriter tw = new StreamWriter(strPathRoot + strFileName + “.doc”);
// write a line of text to the file
tw.WriteLine(sb.ToString());
// close it
tw.Close();
tw.Dispose();

That’s it!

Creative Commons License
photo credit: pvera

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


06
Jul 10


Written By Steve French

 

How to Fix: Canonical Urls with IIS 7’s Url Rewrite feature for https

SATOR Magic squareThe Problem: You want to make your website all SEO friendly by creating a single, canonical url.  For example, if someone types in http://www.stronico.com, you want them to be redirected to http://stronico.com (Google likes it this way).  You do some research and discover that all of the default code and documentation for handling canonical Urls in IIS 7 uses web.config files and the URL Rewrite application program.  All is well and good so far, but what if you use SSL?  The stock code will always redirect you to http://stronico.com/Signup/ even if the original url was https://stronico.com/Signup/ (note the https). Continue reading →


01
Jun 10


Written By Steve French

 

How to fix the No connection could be made because the target machine actively refused it 127.0.0.1:25 error

The Problem: You have a brand new Windows 2008 server and you are testing your web application and trying to send an email.  Every time you try to send an email via the web application you get the following error

“System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25”

The Cause: SMTP services are not installed on the server, they do not seem to be installed by default. Continue reading →


12
Feb 10


Written By Steve French

 

What is the best jQuery modal popup method?

I do not know that much about jQuery, or any of it’s offshoots (jQueryUI, etc) but I do have a need for modal popups.  As I am building the website in ASP.net MVC, instead of ASP.net webforms I  have decided to use the jQuery platform instead of the standard Ajax Toolkit.

I have looked over many, many modal popup schemes and decided to use the Queness modal popup method.  It is  the most usable of the methods I’ve seen.   Look for it on the main Stronico site when it launches!

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


01
Feb 10


Written By Steve French

 

How to fix invisible view in ASP.net mvc

This is not the most significant I know, but the goal is to document every problem that took more than 15 minutes to solve, and I did try the more complicated methods before I tried the obvious one, so here it is.

The Problem: When programming in ASP.net MVC in Visual Studio 2008 you create a new folder, in the views directory, then create a new view in that directory called Index. For example I was working on the Stronico public site (coming soon, really) and created a directory called “Contact”, and a view called “Index.aspx” so the url could be http://www.Stronico.com/Contact/. I also created the Controller “ContactController.cs”. When I went to the staging site on LocalHost, I got the “File Not Found” error.

The Cause: I never did find out. I tried changing the verbiage slightly, in case contact was a protected word, I reset IIS, I did several other things. None of the usual methods worked.

The Solution: I finally just closed the solution and restarted it and I was able to get to the contact page. No idea why that worked, but it did.

Sort of a a lame post, but I did spend slightly more than fifteen minutes trying to see why the page would not appear, only to find it to be a simple Visual Studio error.

 

This post originally appeared on the Stronico blog – with the absorption of Stronico into Digital Tool Factory this post has been moved to the Digital Tool Factory blog


05
Dec 09


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.