Anil Jain

Anil Jain

  • 1.5k
  • 232
  • 7.1k

System.NullReferenceException: Object reference not set to an instance of an object.

Jul 31 2012 1:35 AM
    protected void Page_Load(object sender, EventArgs e)
    {

        @base obj = new @base();
        int Mpid;
        SqlTransaction tr;
        int Cid;
        Word.Application wrdApp;
        Word._Document wrdDoc;
        Object oMissing = System.Reflection.Missing.Value;
        Object oFalse = false;



    }

    private void InsertLines(int LineNum)
    {
        int iCount;

        // Insert "LineNum" blank lines.    
        for (iCount = 1; iCount <= LineNum; iCount++)
        {
            wrdApp.Selection.TypeParagraph();
        }
    }

    private void FillRow(Word._Document oDoc, int Row, string Text1,
        string Text2, string Text3, string Text4, string Text5, string Text6, string Text7,
        string Text8, string Text9, string Text10)
    {
        // Insert the data into the specific cell.
        oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(Text1);
        oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(Text2);
        oDoc.Tables[1].Cell(Row, 3).Range.InsertAfter(Text3);
        oDoc.Tables[1].Cell(Row, 4).Range.InsertAfter(Text4);
        oDoc.Tables[1].Cell(Row, 5).Range.InsertAfter(Text5);
        oDoc.Tables[1].Cell(Row, 6).Range.InsertAfter(Text6);
        oDoc.Tables[1].Cell(Row, 7).Range.InsertAfter(Text7);
        oDoc.Tables[1].Cell(Row, 8).Range.InsertAfter(Text8);
        oDoc.Tables[1].Cell(Row, 9).Range.InsertAfter(Text9);
        oDoc.Tables[1].Cell(Row, 10).Range.InsertAfter(Text10);
        
    }

    private void CreateMailMergeDataFile()
    {
        Word._Document oDataDoc;
        int iCount;

        Object oName = "C:\\DataDoc.doc";
        Object oHeader = "FirstName, LastName, Address, CityStateZip, Flatno, ProjectName, ProjectAddress,Amount,Worklevel, Companyname ";
        wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,
            ref oMissing, ref oHeader, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing);
         
        // Open the file to insert data.
        oDataDoc = wrdApp.Documents.Open(ref oName, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

        for (iCount = 1; iCount <= 2; iCount++)
        {
            oDataDoc.Tables[1].Rows.Add(ref oMissing);
        }
        // Fill in the data.
     
        string aa2 = "select * from View_noticefinal where date between '2012-06-01' and '2012-07-28' and projectcode = 38 and outstanding > 0";
        DataSet ds = SqlHelper.ExecuteDataset(con, CommandType.Text, aa2);
        if (ds.Tables[0].Rows.Count > 0)
        {
            int j =2 ;
            int i;
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                string m1 = ds.Tables[0].Rows[i]["Name"].ToString();
                string m2 = ds.Tables[0].Rows[i]["add1"].ToString();
                string m3 = ds.Tables[0].Rows[i]["add2"].ToString();
                string m4 = ds.Tables[0].Rows[i]["add3"].ToString();
                string m5 = ds.Tables[0].Rows[i]["City"].ToString();
                string m6 = ds.Tables[0].Rows[i]["FlatNo"].ToString();
                string m7 = ds.Tables[0].Rows[i]["ProjectName"].ToString();
                string m8 = ds.Tables[0].Rows[i]["Remarks"].ToString();
                string m9 = ds.Tables[0].Rows[i]["Amount"].ToString();
                string m10 = ds.Tables[0].Rows[i]["Particulars"].ToString();
                string m11 = ds.Tables[0].Rows[i]["Comname"].ToString();
                
                FillRow(oDataDoc, j, m1, m2, m3 + m4, m5, m6, m7, m8, m9, m10, m11);
                j++;
            }
        }
        // Save and close the file.
        oDataDoc.Save();
        oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
    }



    protected void btnShow_Click(object sender, EventArgs e)
    {
        Word.Selection wrdSelection;
        Word.MailMerge wrdMailMerge;
        Word.MailMergeFields wrdMergeFields;
        Word.Table wrdTable;
        string StrToAdd;
        string StrToAdd1;
        string StrToAdd2;
        string StrToAdd3;
        string StrToAdd4;
        string StrToAdd5;
        string StrToAdd6;
        string StrToAdd7;
        string StrToAdd8;
        string StrToAdd9;


        // Create an instance of Word  and make it visible.
        wrdApp = new Word.Application();
        wrdApp.Visible = true;

        // Add a new document.
        wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,
            ref oMissing, ref oMissing);
        wrdDoc.Select();

        wrdSelection = wrdApp.Selection;
        wrdMailMerge = wrdDoc.MailMerge;

        // Create a MailMerge Data file.
        CreateMailMergeDataFile();

        // Create a string and insert it into the document.
        
        InsertLines(4);

        wrdSelection.ParagraphFormat.Alignment =  Word.WdParagraphAlignment.wdAlignParagraphLeft;

        Object objDate = "dddd, MMMM dd, yyyy";
        wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,
            ref oMissing, ref oMissing);
        InsertLines(1);

        // Insert merge data.
        
        wrdMergeFields = wrdMailMerge.Fields;
       
        wrdMergeFields.Add(wrdSelection.Range, "FirstName");
       
        wrdMergeFields.Add(wrdSelection.Range, "LastName");
        wrdSelection.TypeParagraph();

        wrdMergeFields.Add(wrdSelection.Range, "Address");
        wrdSelection.TypeParagraph();
        wrdMergeFields.Add(wrdSelection.Range, "CityStateZip");

        InsertLines(1);
        StrToAdd9 = " ";
        // Right justify the line and insert a date field
        // with the current date.
                
        // Justify the rest of the document.
        wrdSelection.ParagraphFormat.Alignment =
            Word.WdParagraphAlignment.wdAlignParagraphJustify;

        //oWord.Selection.Font.Bold = 1;

        //oWord.Selection.Font.Color = Word.WdColor.wdColorAqua;

        //oWord.Selection.Font.Italic = 1;

        //oWord.Selection.Font.Underline = Word.WdUnderline.wdUnderlineDashHeavy;

        wrdApp.Selection.Font.Bold = 1;

        wrdSelection.TypeText("Sub: Release the Payment against Booking of Flat/Villa No. ");
        wrdMergeFields.Add(wrdSelection.Range, "Flatno");
        wrdSelection.TypeText(StrToAdd9);
        wrdMergeFields.Add(wrdSelection.Range, "ProjectName");
        wrdSelection.TypeText(StrToAdd9);
        wrdMergeFields.Add(wrdSelection.Range, "ProjectAddress");

        InsertLines(1);
        wrdApp.Selection.Font.Bold = 0;

        wrdSelection.TypeText("Dear Sir ");
       // wrdMergeFields.Add(wrdSelection.Range, "FirstName");
        wrdSelection.TypeText(",");
        InsertLines(1);

        // Create a string and insert it into the document.
        StrToAdd = "In the above Matter it is requested that you have booked Villa/Flat no ";
            StrToAdd8 = " in " ;
        StrToAdd1 = " Your Payment of Rs. " ;
        StrToAdd2 = " is due " ;
        StrToAdd3 = " as per terms and conditions " ;
        StrToAdd4 = "You are requested to please make arrange the payment to us. " ;
        StrToAdd5 = "Thanking You " ;
        StrToAdd6 = "For " ;
        StrToAdd7 = "Director " ;
        

    
            
        wrdSelection.TypeText(StrToAdd);
        wrdMergeFields.Add(wrdSelection.Range, "Flatno");
        wrdSelection.TypeText(StrToAdd8);
        wrdMergeFields.Add(wrdSelection.Range, "ProjectName");
        wrdSelection.TypeText(StrToAdd9);
        wrdMergeFields.Add(wrdSelection.Range, "ProjectAddress");
        wrdSelection.TypeText(StrToAdd1);
        wrdApp.Selection.Font.Bold = 1;
        wrdMergeFields.Add(wrdSelection.Range, "Amount");
        wrdApp.Selection.Font.Bold = 0;
        wrdSelection.TypeText(StrToAdd2);
        wrdApp.Selection.Font.Bold = 1;
        wrdMergeFields.Add(wrdSelection.Range, "Worklevel");
        wrdApp.Selection.Font.Bold = 0;
        wrdSelection.TypeText(StrToAdd3);

        InsertLines(1);

        wrdSelection.TypeText(StrToAdd4);

        InsertLines(1);

        wrdSelection.TypeText(StrToAdd5);

        InsertLines(1);

        wrdSelection.TypeText(StrToAdd6);
        wrdMergeFields.Add(wrdSelection.Range, "CompanyName");

        InsertLines(2);
        wrdSelection.TypeText(StrToAdd7);


        InsertLines(2);

       
        // Fill each row of the table with data.
       
       
        // Create a string and insert it into the document.
       
        // Perform mail merge.
        wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
        wrdMailMerge.Execute(ref oFalse);

        // Close the original form document.
        wrdDoc.Saved = true;
        wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);


        // Release References.
        wrdSelection = null;
        wrdMailMerge = null;
        wrdMergeFields = null;
        wrdDoc = null;
        wrdApp = null;

        // Clean up temp file.
        System.IO.File.Delete("C:\\DataDoc.doc");

    }