November 10,2005
[ASP.Net]呼叫函式出現「指定的轉換無效」的錯誤
ASP.NET在呼叫函式時,有時候會出現「指定的轉換無效」這個錯誤,其出現原因可能是整隻專案在不同函式當中,出現的變數名稱過多重覆,ASP.NET在compile 產生 assembly code 的時候,.net framework常會出現「指定的轉換無效」的錯誤;所以不同副程式之間,變數的命名不宜重複性過高。另外呼叫函式亦儘量不要用到強制轉換。
HTML
<%# getDownload(DataBinder.Eval(Container.DataItem,"COURSE_NO")) %>
裡頭儘量不要用到強制轉換,在副程式當中再行轉換
Codebehind C#
protected string getDownload(object getCourse_no)
{
string download=null;
SqlConnection myConnection = new SqlConnection( Config.CONN_STRING );
DataSet myDS = new DataSet();
string sql = "select * from COURSE_HANDOUT where COURSE_NO = '"+getCourse_no.ToString() +"'";
SqlDataAdapter myDA = new SqlDataAdapter(sql, myConnection);
myDA.Fill(myDS,"DOWN");
if(myDS.Tables["DOWN"].Rows.Count>0)
{
for(int i=0;i< myDS.Tables["DOWN"].Rows.Count;i++)
{
download += "<a href=\"Document\\"+myDS.Tables["DOWN"].
Rows[i]["PATH"].ToString()+"\">文件"+(i+1)+"</a>"+"|";
}
}
else { download = "無提供下載文件";}
return download;
}
HTML
<%# getDownload(DataBinder.Eval(Container.DataItem,"COURSE_NO")) %>
裡頭儘量不要用到強制轉換,在副程式當中再行轉換
Codebehind C#
protected string getDownload(object getCourse_no)
{
string download=null;
SqlConnection myConnection = new SqlConnection( Config.CONN_STRING );
DataSet myDS = new DataSet();
string sql = "select * from COURSE_HANDOUT where COURSE_NO = '"+getCourse_no.ToString() +"'";
SqlDataAdapter myDA = new SqlDataAdapter(sql, myConnection);
myDA.Fill(myDS,"DOWN");
if(myDS.Tables["DOWN"].Rows.Count>0)
{
for(int i=0;i< myDS.Tables["DOWN"].Rows.Count;i++)
{
download += "<a href=\"Document\\"+myDS.Tables["DOWN"].
Rows[i]["PATH"].ToString()+"\">文件"+(i+1)+"</a>"+"|";
}
}
else { download = "無提供下載文件";}
return download;
}
引用URL
http://cgi.blog.roodo.com/trackback/705715