pass dictionary key and value to public void RRS feed

All replies

  • User753101303 posted


    Could you be more explicit? Keep in mind we have no other context than what you told us.

    Friday, December 8, 2017 8:40 AM
  • User181930479 posted

    im trying to generate a word file based on template , and im replacing using the dictionary in c# , but if i have 2 keys , the word file is generated twice... : 

    below is my code :

     protected void Page_Load(object sender, EventArgs e)
            Dictionary<string, string> d1 = new Dictionary<string, string>();
            d1.Add("%APPLICANT%", "Bill");
            d1.Add("%BBB%", "AIO");
            foreach (var pair in d1)
                CreateRejectionLetter(pair.Key, pair.Value);
        private DocX GetRejectionLetterTemplate()
            // path
            string fileName = @"D:\DocXExample.docx";
            // Create the document in memory:
            var doc = DocX.Load(fileName);
            return doc;
        public void CreateRejectionLetter(string applicantField, string applicantName)
            // We will need a file name for our output file (change to suit your machine):
            string fileNameTemplate = @"D:\Rejection-Letter-{0}-{1}.docx";
            // Let's save the file with a meaningful name, including the applicant name and the letter date:
            string outputFileName = string.Format(fileNameTemplate, applicantName, DateTime.Now.ToString("MM-dd-yy"));
            // Grab a reference to our document template:
            DocX letter = this.GetRejectionLetterTemplate();
            // Perform the replace:
            letter.ReplaceText(applicantField, applicantName);
            // Save as New filename:
            // Open in word:
            Process.Start("WINWORD.EXE", "\"" + outputFileName + "\"");
    Friday, December 8, 2017 8:54 AM
  • User753101303 posted

    My understanding is that you are calling a Word document for each entry found in a dictionary when you actually want to pass the whole dictionary and loop on entries INSIDE your method ie something like (iuntested) :


    and inside your CreateRejectionLetter you would have :

     // Perform the replace:
    foreach (var pair in d1)

    This is just a programming logic error.

    Not directly related but Process.Start won't work once installed on a real server. Your C# code runs server side and so it would try to open the file on the server side. You would rather need to send the file to the browser so that it is downloaded and opened client side (but see first if your first issue is solved).

    Friday, December 8, 2017 12:10 PM