Solved

Custom headers

Posted on 2016-09-28
9
42 Views
Last Modified: 2016-09-29
Hi,

I was wondering if it is possible to display custom headers. I need to create an Excel report that display "user-friendly" headers.
What I use is something like this

Select username as "User Name", firstname as "First Name".....

It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks
0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 57
ID: 41820690
<<It works fine to display all fields.  What if user can select the headers they want?  Is there a way to accomplish this goal?  thanks>>

 Not sure I understand the question, but there are two methods to do this:

1. Build the SQL on the fly, aliasing the field names as you showed.

2. Use a querydef, and set the Caption property on each of the fields.

  You can see this in the query designer by right clicking on a field to get it's properties.

<<What if user can select the headers they want? >>

 You'd use a form to collect that input and use for #1 or #2 above.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41820837
At this point, users can pick their fields that they want. I combine all the fields into a comma delimited string and store them in a table. They can actually get the Excel report. The problem, again, is the headers.

I think I will need more directions.   In this case, which approach do you think is better?

If I have a string like this  userid, username, firstname, lastname, address

How can I alias the fields with different names?  thanks
0
 
LVL 57
ID: 41821505
<<   In this case, which approach do you think is better? >>

  Their both more or less equivenlant.  Depends on what your doing in code already as to which would work better.

<< I combine all the fields into a comma delimited string and store them in a table. >>

  You would probably change that part.   When you combine the strings to form the SELECT, that's the point where you would add the alias.

 strSQL = "SELECT " & <users first field choice> & " As " & <what user wants as a header> & ", " & <users 2nd field choice> & " As " & <what user wants as a header> & " FROM "

 and you'd end up with an SQL statement that looks just like what you posted in your question.

 If you post the code that is doing this and creates the spreadsheet, I can be more specific.

Jim.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:mcrmg
ID: 41821554
I am not sure if this is enough, user can select the fields they want from a listbox in asp.net page. Then I loop through the fields by adding "," in between, then save that string to a field in sql table. So, when generating Excel report, it will be like this

strSQL =  "SELECT " & db(field) & " FROM table"

    // Save Report
    protected void SaveReportbtn_Click(object sender, EventArgs e)
    {
        string TimeslotItems = "";
        ReportNameMsg.Text = "";

        if (ReportName.Text == "")
        {
            ReportNameMsg.ForeColor = System.Drawing.Color.Red;
            ReportNameMsg.Text = "Please enter a report name";
        }
        else
        {
            foreach (ListItem item in mainlist.Items)
            {
                TimeslotItems += item.ToString() + ","; // /n to print each item on new line or you omit /n to print text on same line
            }
            //testBox.Text = TimeslotItems;

            CreateReport(TimeslotItems);
        }
    }

Open in new window

0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41821574
Not sure why I thought this was a Access/VBA question....no wonder you didn't understand what I was saying!  My apologies.

But you'd need to modify line 16 in the above adding in the aliases and then possibly modify CreateReport(), although I'm assuming your just passing the select list and it's building an SQL statement with that, so it will most likely work with no changes other than to line 16 of what you posted.

Jim.
0
 

Author Comment

by:mcrmg
ID: 41821590
No worries....     :)

Do you think it would work?  In the dropdown list, there will be a value and a text (where user will see on the page).  In order to get both fields of table and the "user friendly" fields, using a "*" in between them.  For example,
                    <asp:listbox ID="FollowUpReason" runat="server" Width="150px">
                    <asp:ListItem Value="">Select</asp:ListItem>
                    <asp:ListItem Value="username*User Name">User Name</asp:ListItem>
                    </asp:listbox>

I will have to parse this once pass to the function.  Is there a better way? thanks
0
 
LVL 57
ID: 41821615
<<Is there a better way? thanks>>

 Don't believe so.

<<username*User Name>>

  Your only job then would be to replace the * with " as "

Jim.
0
 

Author Closing Comment

by:mcrmg
ID: 41821617
thank you very much
0
 

Author Comment

by:mcrmg
ID: 41821631
oops...I left out another issue, Please let me know if I need to open a new question.

What if I need to do some data type conversion? such as

convert(varchar(50),cast(a.abc as money),1) as 'ABC',

--OR--

case a.abc                        
        when '1' then 'Yes'
        when '2' then 'No'  
        else ''
        end as 'ABC',


thanks
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question