我想在绑定到Gridview之前,更新从数据库中检索的DataTable。
然而,当我更新十进制字段时,小数点后面的部分被设置为零。我错过了什么?
if (HttpContext.Current.Request.IsAuthenticated)
{
// Get additional price matches
using (SqlConnection stockConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
{
// Check for trade match offer
SqlCommand tradePCCheck = new SqlCommand("getAllMyPriceMatches", stockConn);
tradePCCheck.CommandType = CommandType.StoredProcedure;
SqlParameter email = tradePCCheck.Parameters.Add("@email", SqlDbType.NVarChar);
try
{
email.Value = this.Context.User.Identity.Name;
}
catch
{
email.Value = " ";
}
SqlParameter thedate = tradePCCheck.Parameters.Add("@theDate", SqlDbType.DateTime);
thedate.Value = DateTime.Now.AddHours(-50);
stockConn.Open();
SqlDataReader pcReader = tradePCCheck.ExecuteReader();
pms.Load(pcReader);
pcReader.Close();
stockConn.Close();
}
}
//Set Connection, Open the DB & Fill Data Set
using (SqlConnection stockConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
{
SqlCommand stockCommand = new SqlCommand("getTISearchResults", stockConn);
stockCommand.CommandType = CommandType.StoredProcedure;
SqlParameter keyword = stockCommand.Parameters.Add("@keyword", SqlDbType.NVarChar);
keyword.Value = prefixText;
stockConn.Open();
SqlDataReader rd = stockCommand.ExecuteReader();
searchResults.Load(rd);
stockCommand.Dispose();
rd.Dispose();
}
// Update Results with elevated prices...
foreach (DataRow dr in searchResults.Rows)
{
// Check for PMS
DataRow[] thePMS = pms.Select("tpc_stockid = '" + dr["stockitem_number"].ToString() + "'");
if (thePMS.Length > 0)
{
decimal px = 0;
decimal cash = 0;
if (thePMS[0]["tpc_pricepx"] != null && !thePMS[0]["tpc_pricepx"].ToString().Equals(""))
{
px = Convert.ToDecimal(thePMS[0]["tpc_pricepx"]);
}
if (thePMS[0]["tpc_price"] != null && !thePMS[0]["tpc_price"].ToString().Equals(""))
{
cash = Convert.ToDecimal(thePMS[0]["tpc_price"]);
}
// update table and accept changes
DataRow[] theRows = searchResults.Select("stockitem_number = '" + dr["stockitem_number"].ToString() + "' ");
if (theRows.Length > 0)
{
theRows[0]["stockitem_pxprice"] = px;
theRows[0]["stockitem_cashprice"] = cash;
searchResults.AcceptChanges();
}
}
}
gvSearchResults.DataSource = searchResults;
gvSearchResults.DataBind();
在分配之前,我已经输出了PX和现金,并且它们持有正确的值800.19和500.12,但是在AcceptChanges之后并绑定后,输出为800.00和500.12。
theRows[0]["stockitem_pxprice"]
和theRows[0]["stockitem_cashprice"]
都是从填充搜索结果的数据库上的decimal(5,2)
类型。
非常感谢您的任何帮助。
谢谢。
Convert.ToDecimal()
调用之后,px
和cash
的中间值是什么?我会考虑这里可能存在文化问题,但一个能正常工作而另一个不能似乎非常奇怪。 - userSqlReader
加载searchResults
之前声明了其结构(架构)(schema)?也许您的stockitem_pxprice
被声明为typeof(int)
? - Alex FilipovicisearchResults
变量在哪里声明的?它是如何创建的? - Ann L.