This is a migrated thread and some comments may be shown as answers.

Multiple Admission letters report

0 Answers 42 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert Jakech
Top achievements
Rank 1
Robert Jakech asked on 07 May 2010, 01:04 PM
Hi there,

I don't know whether this is the right forum to address my issue:
I want to produce reports/admission letters for students who have been admitted.
I thought of using MS Word to design a template, then use it into my project C# to insert into the template.
NOTE: That the format for the letters is the same, except NAMES, COURSE ADMITTED, GENDER, which i want to pick from my db.
I googled out and found this piece of code. when i tried to modify it, it does produce me what i want, but trying to open the outputted file, i get error of 'not enough memory...' of sort.
Can someone help me with a code that can do this: PLEASE CHECK THE CODE BELOW, and let me know
public void GetOutput()

string strFilePath = null;

string strvalue = null;

// Declare a file system object say td.
var td = Server.CreateObject("Scripting.FileSystemObject");

// Copy the template file temp2.rtf into file2.rtf.
File.Copy(Server.MapPath("Templates/Template.rtf"), Server.MapPath("file2.rtf"));

// Set file path to file2.rtf. 
strFilePath = Server.MapPath(".") + "/file2.rtf";

//Open file2.rtf in read mode. 
FileStream fs2 = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);

//Declare d as a stream Reader 
StreamReader d = new StreamReader(fs2);

//Initialise the stream reader d to the begining of the file. 
d.BaseStream.Seek(0, SeekOrigin.Begin);

//A string variable used to store all the values received from the file file2.rtf. 
string swrtarget = null;

//Read the file from Start to End in one go.
swrtarget = d.ReadToEnd();

// Close the Stream Reader

//grab the portion of the template that needs to be repeated
//look at the rtf file and determine which section will repeat the data you want repeated
//using page breaks in the template, I was able to key off of "page" to find my indexes
int beginInsert = swrtarget.IndexOf("page }") + 7;
int endInsert = swrtarget.Length - 7;
int insertLength = endInsert - beginInsert;
string insert = swrtarget.Substring(beginInsert, insertLength);


//I used a dataset and SQLClient instead of OleDb and ExecuteReader
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = conn;
sqlComm.CommandType = CommandType.StoredProcedure;
sqlComm.CommandText = "Stored Proc Here";

SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(sqlComm);
mySqlDataAdapter.Fill(ds, "Table"); 
catch (Exception ex)
Response.Write(" SQL error = " + ex.Message);

//Set the path to open file2.rtf 
strFilePath = Server.MapPath(".") + "/file2.rtf";

//Open file file2.rtf in Read Write Mode
FileStream fs1 = new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite);

//Declare a Stream Writer.
StreamWriter s = new StreamWriter(fs1);

//This variable str will store all the values from the 2nm till final. 
string str = null;

//Replace the values with the values in the string read from file2.rtf
int i = 0;

foreach (DataRow row in ds.Tables[0].Rows)
//make str equal to the template file
if (i == 0)
str = str + swrtarget;
//inserts the repeatable portion of the TemplateBuilder that was caluclated above
str = str.Insert(str.Length - 7, insert);

strvalue = row["dateline"].ToString();
str = str.Replace("##Address##", strvalue);

strvalue = row["lastname"].ToString();
str = str.Replace("##Name##", strvalue);

strvalue = row["occupation"].ToString();
str = str.Replace("##desig##", strvalue);


// After replacing all the values write these values in the file2.rtf via the file write (s).

//After writing the data in the stream writer the values must be flushed.

//Close the stream writer.


catch (Exception ex)
Response.Write("" + ex.Message);



------------------------------END OF CODE-------------------------------------------------

No answers yet. Maybe you can help?

General Discussions
Asked by
Robert Jakech
Top achievements
Rank 1
Share this question