无法将数字插入文本框
Unable to insert Number into a text box
我有一个使用 Oracle 作为数据库的 Web 应用程序。我有一个查询 returns 编号到前端,我需要将其插入到文本框中。
当返回的数字是整数时,文本框会被填充,但当返回的数字是小数时,它会抛出错误"Specified cast is not valid."
这是代码。
string isbn=Request.QueryString["code"];
OracleConnection con = new OracleConnection();
con.ConnectionString = connectionString;
con.Open();
string sql = "SELECT distinct B.TITLE,B.BOOK_CATEGORY,B.ISBN,A.AUTHOR_NAME,P.PUBLICATION_HOUSE_NAME,B.YEAR_OF_PUBLICATION,(SELECT AVG(AVERAGE_RATING) FROM RATING_TABLE GROUP BY ISBN HAVING ISBN = '" + isbn + "') AS AVERAGE from BOOK_TABLE B JOIN AUTHOR_TABLE A ON B.AUTHOR_ID = A.AUTHOR_ID JOIN PUBLISHER_TABLE P ON B.PUBLISHER_ID = P.PUBLISHER_ID JOIN RATING_TABLE R ON B.ISBN = R.ISBN WHERE B.ISBN = '" + isbn + "'";
OracleCommand cmd = new OracleCommand(sql, con);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtBook.Text = Convert.ToString(dr["TITLE"]);
txtGenre.Text = Convert.ToString(dr["BOOK_CATEGORY"]);
txtISBN.Text = Convert.ToString(dr["ISBN"]);
txtAuthor.Text = Convert.ToString(dr["AUTHOR_NAME"]);
txtPublisher.Text = Convert.ToString(dr["PUBLICATION_HOUSE_NAME"]);
txtyop.Text = Convert.ToString(dr["YEAR_OF_PUBLICATION"]);
txtRating.Text =dr["AVERAGE"].ToString();
}
dr.Close();
我是不是漏掉了什么?
希望 txtRating.Text =dr["AVERAGE"].ToString();
导致错误,这不是因为您指定的 decimal
到 string
转换。这可能是由于 dr["AVERAGE"]
是 null
,因此 null.ToString()
在这里给出了错误:所以你可以使用:
txtRating.Text = dr["AVERAGE"] == null || dr["AVERAGE"] is DBNull ? "" : dr["AVERAGE"].ToString();
在 PLSQL 转换中
SELECT CAST (null AS DOUBLE PRECISION) From dual;
SELECT CAST ('100.233' AS DOUBLE PRECISION) From dual;
Select AVG( CAST(AVERAGE_RATING AS DOUBLE PRECISION)) FROM RATING_TABLE;
或在 C# 中
try
{
If(dr["AVERAGE"]!= null)
{
txtRating.Text = (!String.IsNullOrEmpty(dr["AVERAGE"].ToString()) ? Convert.ToDecimal(dr["AVERAGE"].ToString()) : 0);
}
}
catch{}
代码没有问题,但是dr["AVERAGE"]
不应为空。
否则你会得到 Object reference error
我找到了问题所在。正在计算的我的平均值在某些情况下最终会成为非经常性值并且不会结束。因此在转换时曾经出现错误。
我在查询中使用 TO_CHAR 函数解决了它。
string sql = "SELECT distinct B.TITLE,B.BOOK_CATEGORY,B.ISBN,A.AUTHOR_NAME,P.PUBLICATION_HOUSE_NAME,B.YEAR_OF_PUBLICATION,(SELECT TO_CHAR((AVG(AVERAGE_RATING)),'99.99') FROM RATING_TABLE GROUP BY ISBN HAVING ISBN = '" + isbn + "') AS AVERAGE from BOOK_TABLE B JOIN AUTHOR_TABLE A ON B.AUTHOR_ID = A.AUTHOR_ID JOIN PUBLISHER_TABLE P ON B.PUBLISHER_ID = P.PUBLISHER_ID JOIN RATING_TABLE R ON B.ISBN = R.ISBN WHERE B.ISBN = '" + isbn + "'";
我有一个使用 Oracle 作为数据库的 Web 应用程序。我有一个查询 returns 编号到前端,我需要将其插入到文本框中。
当返回的数字是整数时,文本框会被填充,但当返回的数字是小数时,它会抛出错误"Specified cast is not valid."
这是代码。
string isbn=Request.QueryString["code"];
OracleConnection con = new OracleConnection();
con.ConnectionString = connectionString;
con.Open();
string sql = "SELECT distinct B.TITLE,B.BOOK_CATEGORY,B.ISBN,A.AUTHOR_NAME,P.PUBLICATION_HOUSE_NAME,B.YEAR_OF_PUBLICATION,(SELECT AVG(AVERAGE_RATING) FROM RATING_TABLE GROUP BY ISBN HAVING ISBN = '" + isbn + "') AS AVERAGE from BOOK_TABLE B JOIN AUTHOR_TABLE A ON B.AUTHOR_ID = A.AUTHOR_ID JOIN PUBLISHER_TABLE P ON B.PUBLISHER_ID = P.PUBLISHER_ID JOIN RATING_TABLE R ON B.ISBN = R.ISBN WHERE B.ISBN = '" + isbn + "'";
OracleCommand cmd = new OracleCommand(sql, con);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
txtBook.Text = Convert.ToString(dr["TITLE"]);
txtGenre.Text = Convert.ToString(dr["BOOK_CATEGORY"]);
txtISBN.Text = Convert.ToString(dr["ISBN"]);
txtAuthor.Text = Convert.ToString(dr["AUTHOR_NAME"]);
txtPublisher.Text = Convert.ToString(dr["PUBLICATION_HOUSE_NAME"]);
txtyop.Text = Convert.ToString(dr["YEAR_OF_PUBLICATION"]);
txtRating.Text =dr["AVERAGE"].ToString();
}
dr.Close();
我是不是漏掉了什么?
希望 txtRating.Text =dr["AVERAGE"].ToString();
导致错误,这不是因为您指定的 decimal
到 string
转换。这可能是由于 dr["AVERAGE"]
是 null
,因此 null.ToString()
在这里给出了错误:所以你可以使用:
txtRating.Text = dr["AVERAGE"] == null || dr["AVERAGE"] is DBNull ? "" : dr["AVERAGE"].ToString();
在 PLSQL 转换中
SELECT CAST (null AS DOUBLE PRECISION) From dual;
SELECT CAST ('100.233' AS DOUBLE PRECISION) From dual;
Select AVG( CAST(AVERAGE_RATING AS DOUBLE PRECISION)) FROM RATING_TABLE;
或在 C# 中
try
{
If(dr["AVERAGE"]!= null)
{
txtRating.Text = (!String.IsNullOrEmpty(dr["AVERAGE"].ToString()) ? Convert.ToDecimal(dr["AVERAGE"].ToString()) : 0);
}
}
catch{}
代码没有问题,但是dr["AVERAGE"]
不应为空。
否则你会得到 Object reference error
我找到了问题所在。正在计算的我的平均值在某些情况下最终会成为非经常性值并且不会结束。因此在转换时曾经出现错误。
我在查询中使用 TO_CHAR 函数解决了它。
string sql = "SELECT distinct B.TITLE,B.BOOK_CATEGORY,B.ISBN,A.AUTHOR_NAME,P.PUBLICATION_HOUSE_NAME,B.YEAR_OF_PUBLICATION,(SELECT TO_CHAR((AVG(AVERAGE_RATING)),'99.99') FROM RATING_TABLE GROUP BY ISBN HAVING ISBN = '" + isbn + "') AS AVERAGE from BOOK_TABLE B JOIN AUTHOR_TABLE A ON B.AUTHOR_ID = A.AUTHOR_ID JOIN PUBLISHER_TABLE P ON B.PUBLISHER_ID = P.PUBLISHER_ID JOIN RATING_TABLE R ON B.ISBN = R.ISBN WHERE B.ISBN = '" + isbn + "'";