這讓我撓了撓頭。返回錯誤的String.Format
我有一個返回一個包含類JSON數據自動完成控制一個字符串的函數,但不能得到String.Format
工作:
private string GetUsers(string crit)
{
string result = "";
using (DataTable dt = dl.GetRecordSet("select ob_id, ob_desc from objects where ac_id = @acid and ob_desc like @crit and ot_id = 29 and ob_deleted = 0 order by ob_desc", new[] { MyApplicationSession.AccountId, "%" + crit + "%" }))
{
result = "[ ";
foreach (DataRow dr in dt.Rows)
{
result += string.Format("{ \"id\": \"{0}\", \"label\": \"{1}\", \"value\": \"{1}\" },", dr["ob_id"].ToString(), dr["ob_desc"].ToString());
}
result = result.Substring(0, result.Length - 1); // remove trailing comma
result += "]";
}
return result;
}
我總是收到錯誤「Input string was not in a correct format.
」。
dr["ob_id"].ToString()
是一個整數,dr["ob_desc"].ToString()
是在SQL Server 2008數據庫爲varchar。
Visual Studio還建議在「將字符串轉換爲DateTime時」的疑難解答提示下,不知道它從何處獲取。
我嘗試了各種變體,例如用「」替換「,用」替換「,並顯式將參數轉換爲(字符串)。
我發現的唯一工作是將值直接放在字符串中並省略string.Format。
後見之明目瞪口呆......謝謝 –