2012年2月11日 星期六

report service 動態欄位寬度



經過研究之後,發現依照目前 Reporting 現有的功能,很難達成您的需求,需要客制化,因此目前有兩大類的做法:

 

Note:以下作法需需啟用 Reporting Services 中 [My Reports] 功能

 

1)    修改原有報表的 RDL 檔案的 XML,依據欄位的數量,平均分配每個欄位的寬度。修改完之後,需要另透過程式碼發佈至 Reporting 上,然後產生報表。如以下的做法:

 

·         Here's a simple workaround if you absolutely need to get this working.

It's a hack, and you'll have to run this program every time you change the report, but it works.

1.    You've probably already changed the visibility of all your dynamic columns to be an expression.

1.    Make all your column widths in your table a weird number that doesn't occur elsewhere in your report: i.e. "1.23456in"

2.    Save your report

3.    Figure out the min and max number of dynamic-width columns, say 1 to 5 columns

4.    Figure out the cumulitive width of dynamic-width columns: say 5 inches

5.    Write a simple program (or just use the code below) to open the RDL or RDLC as a textfile, loop 5 times, do the text replace of "1.23456in" to (MAX_WIDTH / i), save as "dudeguy" + i + ".rdlc", where i is your loop counter

6.    Change the logic of your interface program/webapp to reference "dudeguy" + num_columns

Imports System.IO

Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, _

                                ByVal e As System.EventArgs) Handles Button1.Click

     

      Const MAX_WIDTH As Double = 5

      Dim contents As String = File.OpenText("c:\dudeguy.rdlc").ReadToEnd

      For i As Integer = 1 To 5

         File.WriteAllText("c:\dudeguy" & i & ".rdlc", _

         contents.Replace("1.23456in", FormatNumber(MAX_WIDTH / i, 5) & "in"))

      Next

   End Sub

End Class

以下是討論群組的文章:

Dynamic change of the column size and location of a matrix

http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/446dfbf5-b5d5-43b8-8e2e-18691c0e1cdf

 

Dynamic Column width for a report

http://social.msdn.microsoft.com/forums/en-US/sqlreportingservices/thread/9e6043f1-c458-4540-be59-d37b02feab8a/

 

2)    進階作法:直接根據報表資料來源的欄位多寡,動態產生整個 RDL 檔案,包含設定每個欄位的寬度等屬性,發佈至 Reporting Services,然後產生報表(可是用於多份報表)。詳細製作的細節如下,請參考:

 

使用报告服务 (Reporting Services) 创建即席报告应用程序 (Ad Hoc Report Application)

http://msdn.microsoft.com/zh-cn/library/aa902634(v=sql.80).aspx 

 

用reporting service创建动态报表

http://blog.csdn.net/shankaipingo/article/details/590583

 

      以下節錄片段程式碼:

 

' Table Columns

           writer.WriteStartElement("TableColumns")

            For i = 0 To m_Fields.Count - 1

                writer.WriteStartElement("TableColumn")

                Select Case m_Type(i)

                    Case "Decimal", "Int16", "Int32", "Int64", "Boolean"

                        writer.WriteElementString("Width", ".5in")

                    Case "DateTime"

                        writer.WriteElementString("Width", ".75in")

                    Case Else

                        writer.WriteElementString("Width", "1.5in")

                End Select

                writer.WriteEndElement() ' TableColumn

            Next 'fieldName

            writer.WriteEndElement() ' TableColumns

 

 

Best Regards,

2012年1月21日 星期六

ASP .net 客製化錯誤畫面

當你在撰寫ASP.net的程式時,有時候程式會發生你不預期的例外,因而沒有去try .. catch去處理。這時候錯誤畫面就會傳到前端。這對使用者而言不是一個很好的使用經驗。因此,讓使用者看到統一而一致的錯誤畫面會比那ASP.net的黃色畫面好多了。
有時候你是在維護之前人寫得程式,如果要在事後為那些程式碼加上try ... catch這樣工程很浩大。因此,ASP.net提供了一個機制,當你的網站發生未處理的錯誤時,則會自動呼叫Global.asax的Application_Error函數。


   Application_Error函數裡要取得程式發生錯誤的例外,只要呼叫Server.GetLastError().GetBaseException()即可。exception中有用的資訊大概就message和StackTrace這兩個屬性。要取得發生錯誤的網址則用Request.RawUrl。要取得cookie或者form等資訊,則可以利用Request.Params。

程式碼大概就像下面那樣



void Application_Error(object sender, EventArgs e)
{         writeErrorLog(Server.GetLastError().GetBaseException(),Request,Session);
Response.Redirect("error.html")//轉到你要去的錯誤畫面
 
 
void writeErrorLog(Exception ex,System.Web.HttpRequest Request,System.Web.SessionState.HttpSessionState Session)
{
   //這邊你可以做你想做的事情,如紀錄錯誤到資料庫之類的。
}