how to construct a template dynamically using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonasharma10
    New Member
    • Sep 2010
    • 1

    #1

    how to construct a template dynamically using c#

    I need to construct a template such that it will contain both static and dynamic fields. For instance we can think of a employee registration form

    ----------------------------------
    First Name: Friend1_Name
    Last Name: Friend1_Surname

    .
    .
    so on
    -----------------------------------

    As you see left part (first Name, Last Name, Phone No) is static, so
    always the same, but the right portion of the stickers are filled by the
    data that comes from the database.

    Is there a way to implement it

    => Administrators will construct the template, put some graphics in a
    Word Template and, somehow, specify the dynamic parts of the template. After
    constructing the tamplate admin will upload the template to a web directory.
    I will write an C#.NET code in order to connect to database
    and create a Word document from the template that is uplaoded.

    How may i fill the dynamic fields programmaticall y at the web server?
    Just tell me away to solve this problem in c#, a I need an answer.here is my code

    using System;
    using System.Data;
    using System.Data.Sql Client;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Text;
    using System.Configur ation;
    using System.Web.Secu rity;
    using System.Globaliz ation;
    using System.IO;
    using Word;
    using System.Drawing;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Web.Dyna micData;
    using System.Web.Sess ionState;
    using Microsoft.Offic e.Core;




    protected void Button2_Click(o bject sender, EventArgs e)
    {
    string sql = ("Insert into regis ( first_name, last_name, date_of_birth, sex, addres, suburb, postcode, transport, telephone, mobile, other, health, english_fluency , heighest_level_ edu, bank, account_type, branch_number, account_number, company_name, company_address , company_postcod e, company_from, company_to, referee_name_ti tle, referee_phone, position_duties , reason_for_leav ing, company_name1, company_address 1, company_from1, company_to1, referee_name_ti tle1, referee_phone1, position_duties 1, reason_for_leav ing1 ) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox5.Text + "','" + DropDownList1.S electedValue + "','" + TextBox4.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox14.Text + "','" + DropDownList3.S electedValue + "','" + DropDownList2.S electedValue + "','" + TextBox10.Text + "','" + TextBox15.Text + "','" + TextBox16.Text + "','" + TextBox17.Text + "','" + TextBox18.Text + "','" + TextBox20.Text + "','" + TextBox21.Text + "','" + TextBox22.Text + "','" + TextBox30.Text + "','" + TextBox31.Text + "','" + TextBox24.Text + "','" + TextBox25.Text + "','" + TextBox23.Text + "','" + TextBox26.Text + "','" + TextBox32.Text + "','" + TextBox33.Text + "','" + TextBox35.Text + "','" + TextBox36.Text + "','" + TextBox37.Text + "','" + TextBox38.Text + "','" + TextBox39.Text + "','" + TextBox40.Text + "')");
    string constr = "server=SQLPROD ; uid=fb; password=Cato12 3; database=regist ration";

    SqlConnection conn = new SqlConnection(c onstr);
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.ExecuteNonQ uery();
    //Response.Redire ct("Default1.as px");
    conn.Close();
    }
    protected void Button1_Click(o bject sender, EventArgs e)
    {
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox5.Text = "";
    DropDownList1.S electedValue = "false";
    TextBox4.Text = "";
    TextBox7.Text = "";
    TextBox8.Text = "";
    TextBox9.Text = "";
    TextBox12.Text = "";
    TextBox13.Text = "";
    TextBox14.Text = "";
    DropDownList3.S electedValue = "false";
    DropDownList2.S electedValue = "false";
    TextBox10.Text = "";
    TextBox15.Text = "";
    TextBox16.Text = "";
    TextBox17.Text = "";
    TextBox18.Text = "";
    TextBox20.Text = "";
    TextBox21.Text = "";
    TextBox22.Text = "";
    TextBox30.Text = "";
    TextBox31.Text = "";
    TextBox24.Text = "";
    TextBox25.Text = "";
    TextBox23.Text = "";
    TextBox26.Text = "";
    TextBox32.Text = "";
    TextBox33.Text = "";
    TextBox35.Text = "";
    TextBox36.Text = "";
    TextBox37.Text = "";
    TextBox38.Text = "";
    TextBox39.Text = "";



    }
    protected void Button3_Click(o bject sender, EventArgs e)
    {

    /*
    Word.Applicatio n wrdApp;
    Word_.Document wrdDoc;
    Object oMissing = System.Reflecti on.Missing.Valu e;
    Object oFalse = false;
    Object oTrue = true;
    Object oTemplate = "C:\\Users\\sde vi\\Documents\\ Visual Studio 2010\\WebSites\ australian staffing agency\\Applica tion For Employment.dot" ;
    Object oFileName = "C:\\Users\\sde vi\\Documents\\ Visual Studio 2010\\WebSites\ australian staffing agency\\Applica tion For Employment.doc" ;

    try
    {
    System.IO.File. Delete("Applica tion For Employment.doc" );
    System.IO.File. Create("Applica tion For Employment.doc" );
    }
    catch(Exception ex)
    {
    }

    wrdApp = new Word.Applicatio n( );
    wrdApp.Visible = true;
    wrdDoc = wrdApp.Document s.Add(ref oTemplate,ref oMissing,ref oMissing,ref oMissing);

    foreach(Word.Fo rmField frmField in wrdDoc.FormFiel ds)
    {

    frmField.Result = "2";
    }

    wrdDoc.SaveAs(r ef oFileName ,ref oMissing ,ref oTrue ,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oTru,ref oTrue,ref
    oMissing);
    wrdDoc.Close(re f oTrue,ref oFalse,ref oMissing);
    wrdDoc = null;
    wrdApp = null;
    }
    */

    }
    }
Working...