我寫的程式如下:
SqlDataReader reader1; //宣告一個DataReader
SqlConnection connStr1 = new SqlConnection(WebConfigurationManager.ConnectionStrings["glory01ConnectionStringERP"].ConnectionString);
String select1 = "select Count(*) AS acount,convert(nvarchar(10),Sum((CASE WHEN 完工碼 ='Y' THEN 1 Else 0 End))) as Y,convert(nvarchar(10),Sum((CASE WHEN 完工碼 ='N' THEN 1 Else 0 End))) as N";
select1 += " from [glory01].[dbo].[SFCTA-VIEW] where (ID ='" + aID + "')";
try
{
connStr1.Open();
SqlCommand cmd1 = new SqlCommand(select1, connStr1);
reader1 = cmd1.ExecuteReader();
while (reader1.Read())
{
string a = reader1[0].ToString();
string b = reader1[1].ToString();
string c = reader1[2].ToString();
Response.Write("a=" + a + " ," + "b=" + b + " ," + "c=" + c + "");
int j = Convert.ToInt32(a);//轉換字串改數字筆數
int k = Convert.ToInt32(b);//完工
int h = Convert.ToInt32(c);//未完工
Response.Write("j=" + j + " ," + "k=" + k + " ," + "h=" + h + "");
}
但執行後:「輸入字串格式不正確。」指向:int k = Convert.ToInt32(b);//完工
不了解為何無法轉換為int
請大大能幫我解題,謝謝。
表示b無法轉成整數啊,你檢查一下內容。
嗯,找到原因,是我抓取資料庫資料,沒去注意是否有null的關係。
找到原因就好 ...這個其實還蠻常碰到的XD
^^嗯嗯,感謝