Dynamic Retrieving from webpage

Hi,
In my sql server table i have a row with data,
ColumnName - Data
Header1Text - Name,
Header1Value - Shiva,
Header2Text - Age,
Header2Value - 25
Header3Text - Sex
Header3Value - Male
Header4Text - Address (Optional)
Header4Value - NULL
so on to 14 headers…
------------------------
I will have to retrieve to a word document those are NOT NULL dynamically… I cant use bookmarks for the headers as all 14headers may be not be having data in the database… those are entered will have to show in the document… Can you please assisst me on how to go ahead with my requirement. Thanks in advance!

Hi

Thanks for your inquiry. I think you can use mail merge to fill your document with data. Please follow the links to learn more:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Please let me know in case of any issues, I will be glad to help you.
Best regards,

Hi, thanx for ur prompt resonse… I’m bit confused with this mail merge option… can you please send me template and steps to follow on how to go ahead with mail merge option? plz find the attached screenshot on the format i’m looking for… In the database the values for the Name, Sex, Age and LastName are given… and so only these records have been retrieved in the document… So my requirement is to retrieve the header and values as the format shown in the screenshot. Hope the information is clear. thanx a lot once again!

Hi, Is this mail merge option can be used using C#.Net application? coz i did some research with the mail merge options with execl sheet but i dont knw hw feasible it is to do with the application… plz help me with my requirement…

Hi

Thanks for your inquiry. I think Dinner Invitation demo is exactly what you are looking for.
https://github.com/aspose-words/Aspose.Words-for-.NET
Please see the following link to learn how to prepare template documents:
https://github.com/aspose-words/Aspose.Words-for-.NET
Also, all demos and required document are included into Aspose.Words installation package. After installing Aspose.Words, you can find templates in the following folder:
C:\Program Files\Aspose\Aspose.Words\Demos\Common\Documents
Hope this helps.
Best regards,

my fren… hope my requirement is unclear… in my database table there are columns like Name, Location, Sex, Age, Designation, Languages expert, Database and so on which have to be displayed in the word document (Only those values are entered in the application)… However the Name and Location are standard. And so I have bookmarked for these two columns in word doc. And everytime these columns are to be displayed by default. Other than these columns, those are values entered (like designation, sex etc.,) have to be fetched dynamically (cannot use bookmarks as these are dynamic and not standard ones)… Please find the attached Intro template… In the attached file, the Name and Location are bookmarked and fetched directly from the application n database tables… but the other fields like Age (Header) - 26 (Value), Designation (Header) - Application Developer (Value), Sex, Languages, Database, Web Application should be fetched dynamically both header and values.(can not use bookmark as these are not standard). I have the header text and values in a database table… Hope this gives you clear picture… thanx for understand my scenario… pls help me

Hi

Thank you for additional information. Here is the scenario:

  1. You have data source, DataTabe for example, which has the following columns: Name, Location, Age, Designation, Sex, Languages, Database, WebApplication. Let’s create method which will return this DataTable (dummy data).
private DataTable GetInfo()
{
    // Create data table
    DataTable table = new DataTable("Info");
    table.Columns.Add("Name");
    table.Columns.Add("Location");
    table.Columns.Add("Age");
    table.Columns.Add("Designation");
    table.Columns.Add("Sex");
    table.Columns.Add("Languages");
    table.Columns.Add("Database");
    table.Columns.Add("WebApplication");
    // Add some dummy data.
    table.Rows.Add(new object[] { "Andrey Noskov", "NZ", "25", "Support Developer", "Male", "C#, VB, Java", "MS SQL", "Test Application" });
    return table;
}
  1. Now, let’s create a template. We should insert merge fields where data from Db are supposed to be inserted. Final template will look like the following:
Name: «Name» Location: «Location»
Age: «Age» Designation: «Designation»
Sex: «Sex» Languages: «Languages»
Database: «Database» Web Application: «WebApplication»

I attached template document.
3. Open template using Aspose.Words and fill it with data using mail merge:

// Open template document.
Document doc = new Document(@"Test001\in.doc");
// Get data.
DataTable data = GetInfo();
// Execute mail merge.
doc.MailMerge.Execute(data);
// Save output document.
doc.Save(@"Test001\out.doc");

I attached the output document.
Hope this helps,

Hi, thanx for understanding my requirement and the sample code… however, the code as u given is not tht feasible coz I cant hardcode the values of the headers… they should be fetched dynamically… wt i mean is, from the header “Age”, all headers should be fetched from the application… (the reason is, few users may not enter the value for Age/Designation etc… so those are not entered that cant be shown)… so finally in my output i should show only those are entered in the application… hope this time it is more clear… thanx once again!

pls someone help me :-(…

Hi

Thank you for additional information. So, do you need to fill the document once user opens it in MS Word? Why do you need this? If you can identify user in your application, you can get an appropriate data from DB, fill template with data and send the filled document to end user. If value of some field does not exist in DB (e.g is NULL or empty string), the appropriate merge field will be empty too.
Also, if you would like to process the document during mail merge, you can use MergeField event handler to achieve this. Please follow the link to learn more:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/fieldmerging/
Best regards,

Hi, guess my explanation confused u… let me say hw ASPOSE word/pdf i’m using in my application… I’m using C#.net as front end. So user has ability to select the headers to enter… So those users wish to enter the details will select appropriate headers (Age, Sex, Designation etc… these header selection varies with users)… After saving the data, my application uses this word template and converts to pdf file and displays… At backend, using the word template all the fields will be placed as per the bookmarked other than these headers… as the headers are given as dynamic selection in application i cant hardcode all the headers in template and so i want to generate the report dynamically… pls let me know if u it is still confusing…

Hi

Yes, your explanation is confusing. So, if I understand you correctly, you have set of fields, some of these fields are optional. Is that right?
And if some field does not have value, you do not need to display it at all. In this case, maybe you should consider option of building whole table from scratch. For example, see the following code:

// Create docuemnt and DocumentBuilder object, which will help us to build the table.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set default formation of table cells.
builder.CellFormat.Width = 200;
// Get data.
DataTable data = GetInfo();
// Loop through all columns in the datatable.
// If value of the current cell is not empty, we will insert lable and value into the Word document.
// Otherwise we will ignore column.
int insertedCols = 0;
foreach (DataColumn column in data.Columns)
{
    // Each row in the table will contain two cells.
    // So if insertedCols is even, we should end row.
    if (insertedCols != 0 && insertedCols % 2 == 0)
    {
        insertedCols = 0;
        builder.EndRow();
    }
    // Check whether there is data for the current column.
    string colData = data.Rows[0][column].ToString();
    if (!string.IsNullOrEmpty(colData))
    {
        builder.InsertCell();
        builder.PushFont();
        builder.Font.Bold = true;
        builder.Write(column.ColumnName + ": ");
        builder.PopFont();
        builder.Write(colData);
        insertedCols++;
    }
}
builder.EndRow();
builder.EndTable();
// Save output document.
doc.Save(@"Test001\out.doc");

=========================================================

private DataTable GetInfo()
{
    // Create data table
    DataTable table = new DataTable("Info");
    table.Columns.Add("Name");
    table.Columns.Add("Location");
    table.Columns.Add("Age");
    table.Columns.Add("Designation");
    table.Columns.Add("Sex");
    table.Columns.Add("Languages");
    table.Columns.Add("Database");
    table.Columns.Add("WebApplication");
    // Add some dummy data.
    table.Rows.Add(new object[] { "Andrey Noskov", "NZ", "", "Support Developer", "Male", null, "MS SQL", "" });
    return table;
}

Hope this helps.
Best regards,

hi… m really sorry abt my improper explanation… Yes, the optional fields that are not entered, should not be shown in the report… seems the above sample code works to me… however if we need to build the table from scratch wt happens to the mandatory fields (Name & Location)… As i mentioned before… Name and Location are stable and the rest headers are optional… Anywayz i’ll try with the above code and get back to u… meanwhile if there is any other possibility way as per my requirement (keeping 2 stable headers and bringing the rest as optional) plz post… thanx once again for ur help! Appreciate it…

Thanks for your request. I think in this case you can try using two tables, first with static, second with dynamic columns.

Bets regards,

hey hi… seems the above sample code works to me… but in the code we are creating a table… can we start fetching records where an table is there? which means in the template i have an table table at a particular place… can we fetch the data to tht table? pls let me knw… thanx once again!

Hi

Thanks for your request. It is perfect, that you already resolved the problem. I think in this case you can put a bookmark to your document at place where you would like to see the table. From the code you can use documentBuilder.MoveToBookmark(“BookMarkName”) and start creating the table there.
Hope this helps.
Best regards,

hey thanx man! let me try and get back to u :-)…