星期日, 7月 12, 2009

DataList裡FindControl


參考文章:
請問ItemDataBound是什麼意思
你有用 FindControl 時找不到 Control (控制項) 的經驗嗎
[ASP.NET]Page.FindControl
遞迴方式的 FindControl (進階版)
ASP.NET問題解決,書中第4-18


發生問題:


並未將物件參考設定為物件的執行個體
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個體

原始程式錯誤:

可能原因:

原來寫法如下:
//以下為隱藏部分的文章

protected void ChgLvl(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Label temp = Page.FindControl("user_levelLabel") as Label;
switch (temp.Text)
{
case "1":
temp.Text = "關懷員";
break;
case "2":
temp.Text = "資料輸入人員";
break;
case "3":
temp.Text = "1級主管";
break;
case "4":
temp.Text = "2級主管";
break;
case "5":
temp.Text = "管理員";
break;
}
}
}

解法方法:


修改為以下寫法

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//原來寫法為
//Label temp = Page.FindControl("user_levelLabel") as Label;
//改為來取得控制項
Label lbl = e.Item.FindControl("user_levelLabel") as Label;

switch (lbl.Text)
{
case "1":
lbl.Text = "關懷員";
break;
case "2":
lbl.Text = "資料輸入人員";
break;
case "3":
lbl.Text = "1級主管";
break;
case "4":
lbl.Text = "2級主管";
break;
case "5":
lbl.Text = "管理員";
break;
}

}
}


GridView匯出至Excel(GridView Export To Excel)


參考文章:
export to excel from dataview
在CodeFile中取得控制項將輸出Clinet端的HTML內容
Solving GridView Export Paging Problems
解決GridView使用RenderControl取得HTML出現的問題
KB-About Event Validation of ASP.NET 2.0




方法一:


protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=FileName1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView g = new GridView();
g.DataSource = SqlDataSource1;
g.DataBind();
g.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

}


方法二:

public override void VerifyRenderingInServerForm(Control control)
{

// Confirms that an HtmlForm control is rendered for
//the specified ASP.NET server control at run time.
//不加會有型別 'GridView' 的控制項 'GridView1' 必須置於有 runat=server 的表單標記之中的錯誤發生。
}

protected void ExporToExcel(object sender, EventArgs e)
{
Response.Clear();

Response.AddHeader("content-disposition",
"attachment;filename=sdf.xls");

Response.Charset = "";

// If you want the option to open the Excel file without saving than

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);

// turn off paging & sorting
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.DataBind();

GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

// turn the paging on again
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
GridView1.DataBind();

}

如果GridView的分頁如排序無事前關閉再匯出,會發生
RegisterForEventValidation 只能在 Render(); 期間呼叫
的錯誤發生,解決方法有2種

1、請在html加上EnableEventValidation="false"修改如下
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="1003.aspx.cs" Inherits="_1003_1003" %>(儘量別用此方法,原因點我)

2、

// turn off paging & sorting
//匯出前先將排序與分頁關閉
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.DataBind();

GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

// turn the paging on again
//匯出後再將排序與分頁開啟
GridView1.AllowPaging = true;
GridView1.AllowSorting = true;
GridView1.DataBind();

星期四, 7月 09, 2009

同一個 IIS 處理序中不能執行兩個不同版本的 ASP.NET

不知為什麼發生下列問題。可能是移檔案的時候不小心又開到windows的應用程式了吧。


同一個 IIS 處理序中不能執行兩個不同版本的 ASP.NET。請使用 IIS 系統管理工具重新設定伺服器使用不同的處理序來執行應用程式。


開啟IIS至該站台按右鍵選擇內容>ASP.NET>將版本改成網站所使用的.NET版本
即可。
如果還是有問題試看看先設成別的版本確定後再改回網站使用的版本

星期三, 7月 08, 2009

Crystal Report 在 Web 應用程式無法登入到 SQL Server

使用VS2008里的Crystal Report連SQL-Server時,可能會發生以下問題


在 '/' 更應用程式伺服器錯誤
-----------------------------------------------------------
離線失敗。.

說明: 在執行的目前 Web 要求期間發生未處理的例外狀況發生。 請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。.

例外狀況詳細資料: CrystalDecisions.CrystalReports.Engine.LogOnException: 登入失敗

或是需要使用者打入登入資訊。發生原因如下(引述http://support.microsoft.com/kb/319264/zh-tw)

基於安全理由, SQL Server 密碼在 Run Time 不會保存在 Crystal Report。

這時就可以在執行階段變更資料庫位置的功能。來解決此問題。
在顯示的程式碼裡加入以下程式碼。



//隱藏部分的文章

//記得要引入這兩個命名空間
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

protected void Page_Load(object sender, EventArgs e)
{
//
ConnectionInfo connectionInfo = new ConnectionInfo();
SetDBLogonForReport(connectionInfo);
//主機位置
connectionInfo.ServerName = "XXX.XXX.XXX.XXX";
//資料庫名稱
connectionInfo.DatabaseName = "Adventure Works";
//登入帳號
connectionInfo.UserID = "XX";
//登入密碼
connectionInfo.Password = "XXXXX";
}
private void SetDBLogonForReport(ConnectionInfo connectionInfo)
{
//這裡的CrystalReportView1是看你物件ID
TableLogOnInfos tableLogOnInfos = CrystalReportViewer1.LogOnInfo;

foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
{
tableLogOnInfo.ConnectionInfo = connectionInfo;
}
}


參考文章:
http://support.microsoft.com/kb/319264/zh-tw
http://msdn.microsoft.com/zh-tw/library/ms227536.aspx

多重步驟OLE DB 操作發生錯誤。請檢查每一個可用的OLE DB 狀態值

資料庫欄位為:
1.序號:int自動編號
2.名稱:nvarchar
3.類別:nvarchar


資料庫設計時,序號為自動編號,當使用DetailsView的新增時。
在aspx裡的原始碼會自動產生如下的InsterCommand與InsertParameters:


InsertCommand="INSERT INTO [支出品明細] ([序號],[名稱],[類別]) VALUES (?,?,?)"






只要把資料庫自動編號的欄位去除掉就不會有問題了。
修改後如下:

InsertCommand="INSERT INTO [支出品明細] ( [名稱], [類別]) VALUES (?, ?)"




IDENTITY_INSERT 設為 OFF 時

問題:在使用DetailsView新增時發生
IDENTITY_INSERT 設為 OFF 時



原來.NET自動產生的InsertCommand為:

InsertCommand="INSERT INTO [支出品明細] ( [序號], [名稱], [類別]) VALUES ( ?, ?, ?)"



因為序號在資料庫設計時為自動編號,所以要將InsertCommand改為

InsertCommand="INSERT INTO [支出品明細] ( [名稱], [類別]) VALUES (?, ?)"

問題就解決了



星期一, 7月 06, 2009

C# 匯出 Excel

一些參考文章



A Very Easy to Use Excel XML Export Library
http://www.codeproject.com/KB/office/excelxmllibrary.aspx
How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
http://support.microsoft.com/kb/306023/en-us
如何將資料傳送至 Excel 活頁簿藉由使用 Visual C# 2005 或 Visual C# . NET
http://support.microsoft.com/kb/306023/zh-tw
Considerations for server-side Automation of Office
http://support.microsoft.com/kb/257757/en-us
Office 伺服器端自動化的考量因素
http://support.microsoft.com/kb/257757/zh-tw
How to automate Microsoft Excel from Visual Basic .NET
http://support.microsoft.com/kb/301982/en-us
如何從 Visual Basic . NET Microsoft Excel 自動化
http://support.microsoft.com/kb/301982/zh-tw
http://support.microsoft.com/kb/301982/zh-tw
ASP.NET 2.0 匯出資料到Excel的簡單作法
http://blog.miniasp.com/post/2008/02/12/Export-data-to-Excel-with-ease-using-ASPNET.aspx


上述轉自http://blog.miniasp.com/

目前的安全設定並不允許您從這個地方下載檔案

當使用2003要下載檔案發生以下錯誤訊息:
目前的安全設定並不允許您從這個地方下載檔案
可以嘗試下列方法解決,


至瀏覽器裡的工具>網際網路選項>安全性>信任的網站>將此網站加到該區域
將下載的網址打入按下新增。
問題應該就解決了。


Analytics