This is a simple mail merge program (Part 2). This program reads from the access database (by default) from a specified table. It then populates the theTags.txt and the theList.txt for the program (part 1) to use it. Additional function - creating a xml file if requested.

The contents of the text files are as follows:

theTags.txt - This file contains only the recognised tags
theList.txt - This file contains the user info in "key:value" format
MailMerge2.cs - The program

After running this program, place only the exe file from part 1 into the current directory and run it (This assumes that the template.txt is also present in the same directory)

Best wishes and good luck ! Say Gin 

Source Code:

/*
Author : Say Gin Teh
Date : 22/3/2001
Copyright
Free to distribute this program only for educational purpose
This is part 2 of 2 of my simple sgmailmerge program. This program
will get info from the database and write to the txt files. These txt
files will be used the (part 1) program
to merge the mails Additional features include creating an xml file
*/
namespace SGMailMerge
{
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
using System.Data.ADO;
using System.Data;
using System.IO;
using System.Text;
///
/// Summary description for Win32Form1.
///
public class MailMerge2 : System.WinForms.Form
{
///
/// Required by the Win Forms designer
///
private System.ComponentModel.Container components;
private System.WinForms.CheckBox checkBox1;
private System.WinForms.Button button3;
private System.WinForms.Button button2;
private System.WinForms.Label label1;
private System.WinForms.TextBox textBox4;
private System.WinForms.Label label4;
private System.WinForms.TextBox textBox1;
private System.WinForms.Button button1;
private System.WinForms.DataGrid dataGrid;
private System.WinForms.Panel panel1;
private System.WinForms.TextBox textBox3;
private System.WinForms.Label label3;
private System.WinForms.TextBox textBox2;
private System.WinForms.Label label2;
private int VIEWTABLE = 1;
private int EXECUTE = 2;
private string database_name = "";
private string table_name = "";
private string tagfile_name = "";
private string listfile_name = "";
private DataSet ds = null;
private StringBuilder sb = new StringBuilder();
private int forTagFile = 1;
private int forListFile = 2;
StringBuilder msgSB =
new StringBuilder();
public MailMerge2()
{
InitializeComponent();
}
///
/// Clean up any resources being used
///
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
///
/// The main entry point for the application.
///
public static void Main(string[] args)
{
Application.Run(
new MailMerge2());
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label4 = new System.WinForms.Label();
this.checkBox1 = new System.WinForms.CheckBox();
this.textBox4 = new System.WinForms.TextBox();
this.label3 = new System.WinForms.Label();
this.label2 = new System.WinForms.Label();
this.label1 = new System.WinForms.Label();
this.button3 = new System.WinForms.Button();
this.textBox2 = new System.WinForms.TextBox();
this.dataGrid = new System.WinForms.DataGrid();
this.textBox1 = new System.WinForms.TextBox();
this.textBox3 = new System.WinForms.TextBox();
this.button2 = new System.WinForms.Button();
this.button1 = new System.WinForms.Button();
this.panel1 = new System.WinForms.Panel();
dataGrid.BeginInit();
//@design this.TrayHeight = 90;
//@design this.TrayLargeIcon = false;
//@design this.DrawGrid = false;
//@design this.TrayAutoArrange = true;
label4.Location = new System.Drawing.Point(16, 48);
label4.Text = "Table Name";
label4.Size =
new System.Drawing.Size(80, 23);
label4.TabIndex = 9;
checkBox1.Location =
new System.Drawing.Point(16, 312);
checkBox1.Text = "Produce xml file";
checkBox1.Size =
new System.Drawing.Size(120, 24);
checkBox1.TabIndex = 13;
textBox4.Location =
new System.Drawing.Point(120, 272);
textBox4.Text = "theList.txt";
textBox4.TabIndex = 5;
textBox4.Size =
new System.Drawing.Size(120, 20);
label3.Location =
new System.Drawing.Point(16, 272);
label3.Text = "Writing to List file";
label3.Size =
new System.Drawing.Size(100, 23);
label3.TabIndex = 4;
label2.Location =
new System.Drawing.Point(16, 232);
label2.Text = "Writing toTag file";
label2.Size =
new System.Drawing.Size(100, 23);
label2.TabIndex = 2;
label1.Location =
new System.Drawing.Point(16, 8);
label1.Text = "Access database name (mdb)";
label1.Size =
new System.Drawing.Size(88, 32);
label1.TabIndex = 1;
button3.Location =
new System.Drawing.Point(280, 272);
button3.Size =
new System.Drawing.Size(75, 23);
button3.TabIndex = 12;
button3.Text = "Close";
button3.Click +=
new System.EventHandler(button3_Click);
textBox2.Location =
new System.Drawing.Point(120, 48);
textBox2.Text = "Friends";
textBox2.TabIndex = 8;
textBox2.Size =
new System.Drawing.Size(144, 20);
dataGrid.Location =
new System.Drawing.Point(16, 80);
dataGrid.Size =
new System.Drawing.Size(328, 120);
dataGrid.DataMember = "";
dataGrid.TabIndex = 6;
textBox1.Location =
new System.Drawing.Point(120, 8);
textBox1.Text = "MM1.mdb";
textBox1.TabIndex = 0;
textBox1.Size =
new System.Drawing.Size(144, 20);
textBox3.Location =
new System.Drawing.Point(120, 232);
textBox3.Text = "theTags.txt";
textBox3.TabIndex = 3;
textBox3.Size =
new System.Drawing.Size(120, 20);
button2.Location =
new System.Drawing.Point(280, 232);
button2.Size =
new System.Drawing.Size(75, 23);
button2.TabIndex = 11;
button2.Text = "Extract Info";
button2.Click +=
new System.EventHandler(button2_Click);
button1.Location =
new System.Drawing.Point(272, 32);
button1.Size =
new System.Drawing.Size(75, 23);
button1.TabIndex = 7;
button1.Text = "View Table";
button1.Click +=
new System.EventHandler(button1_Click);
panel1.Location =
new System.Drawing.Point(16, 8);
panel1.Size =
new System.Drawing.Size(360, 208);
panel1.TabIndex = 10;
this.Text = "SGMailMerge2";
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 349);
panel1.Controls.Add(label1);
panel1.Controls.Add(textBox1);
panel1.Controls.Add(label4);
panel1.Controls.Add(textBox2);
panel1.Controls.Add(button1);
panel1.Controls.Add(dataGrid);
this.Controls.Add(checkBox1);
this.Controls.Add(button3);
this.Controls.Add(button2);
this.Controls.Add(panel1);
this.Controls.Add(textBox3);
this.Controls.Add(label3);
this.Controls.Add(textBox4);
this.Controls.Add(label2);
dataGrid.EndInit();
}
// VIEW Table
protected void button1_Click(object sender, System.EventArgs e)
{
//***************
this.Cursor = System.Drawing.Cursors.WaitCursor;
if(Check_TextField_Input(VIEWTABLE))
{
Connect_To_Db();
}
Cursor = Cursors.Arrow;
}
private bool Check_TextField_Input(int type)
{
bool valid = true;
if (type == VIEWTABLE || type == EXECUTE)
{
if (textBox1.Text.Trim().Length ==0)
{
MessageBox.Show("Database name must
not be blank or spaces","Information");
valid =
false;
}
else
{
database_name = textBox1.Text.Trim();
}
if (textBox2.Text.Trim().Length ==0)
{
MessageBox.Show("Table name must
not be blank or spaces","Information");
valid =
false;
}
else
{
table_name = textBox2.Text.Trim();
}
}
if (type == EXECUTE)
{
if (textBox3.Text.Trim().Length ==0)
{
MessageBox.Show("Tag File name must
not be blank or spaces","Information");
valid =
false;
}
else
{
tagfile_name = textBox3.Text.Trim();
}
if (textBox4.Text.Trim().Length ==0)
{
MessageBox.Show("List File name must
not be blank or spaces","Information");
valid =
false;
}
else
{
listfile_name = textBox4.Text.Trim();
}
}
return valid;
}
private DataSet Connect_To_Db()
{
ADOConnection conn;
ADODataSetCommand command;
try
{
string theProvider="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source="+database_name;
string theSQL = "SELECT * FROM "+table_name;
//make a ADOConnection
conn = new ADOConnection(theProvider) ;
conn.Open() ;
// Set the command
command = new ADODataSetCommand(theSQL ,theProvider);
ds =
new DataSet();
command.FillDataSet(ds,table_name);
dataGrid.DataSource = ds.Tables[table_name].DefaultView ;
dataGrid.Show();
//Close the ADOConnection
conn.Close() ;
}
catch(Exception ex)
{
MessageBox.Show("Error = Either Database
or the table
do not exist."+
"\nPlease re-enter."+
"\n "+ex,"Information");
dataGrid.Hide();
return null;
}
return ds;
}
// EXTRACT Info
protected void button2_Click(object sender, System.EventArgs e)
{
this.Cursor = System.Drawing.Cursors.WaitCursor;
if(Check_TextField_Input(EXECUTE))
{
if( Connect_To_Db() !=null)
{
Write_to_file(forTagFile,tagfile_name);
Write_to_file(forListFile,listfile_name);
msgSB.Append("Information from "+database_name+"
database and table "+table_name+
" \nextracted to "+tagfile_name+" and "
+listfile_name);
if(checkBox1.Checked)
{
ds.WriteXml(table_name+".xml");
msgSB.Append("\nXML file "
+table_name+".xml produced");
}
MessageBox.Show(msgSB.ToString(),"Information");
msgSB.Remove(0,msgSB.Length);
}
}
Cursor = Cursors.Arrow;
}
// Write to file
private void Write_to_file(int fileType, string fileName)
{
File f =
new File(fileName);
if(f.Exists)
{
f.Delete();
}
f =
null;
FileStream fs =
new FileStream(fileName,FileMode.OpenOrCreate,
FileAccess.Write);
StreamWriter sw =
new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
DataTable [] tables = ds.Tables.All;
for(int i=0;i

Next Recommended Readings