嘿伙计们有这个奇怪的转换异常虽然我的数据类型在db中是正确的:
string sql = string.Format( @"select aim_network_id,aim_network_name,oxinetwork_id,pack_id,pack_name,p_face_value,pm_prefix from Operator where aim_network_id='{0}'", gridbackOffice["aim_network_id", gridbackOffice.CurrentCell.RowIndex].Value); OleDbCommand getSelectedGridDatecmd = new OleDbCommand(sql, conn); OleDbDataReader reader = getSelectedGridDatecmd.ExecuteReader(); while (reader.Read()) { txtAimNetworkID.Text = reader.GetString(0); txtAimNetworkName.Text = reader.GetString(1); txtPARNetworkID.Text = reader.GetString(2); txtPARFaceValue.Text = reader["p_face_value"].ToString(); //in above line if i'm doing this `reader.GetString(5)` then i'm getting specified cast exception and that to randomly i.e some time it works fine and suddenly sometime gives this exception txtPARPackID.Text = reader.GetString(3); txtPARPackName.Text = reader.GetString(4); txtPARPMPrefix.Text = reader["pm_prefix"].ToString(); }我有点困惑,如果我使用这个reader["p_face_value"].ToString()然后我的代码运行非常顺利,但是使用这个reader.GetString(5) ,根据我的方法返回字符串,nebody b4遇到了这个错误? ....错误是while循环中的第4行和第7行。
例外 :指定的强制转换无效(InvalidCastException未处理)
hey guys m having this wierd exception of cast though my datatypes are correct in db:
string sql = string.Format( @"select aim_network_id,aim_network_name,oxinetwork_id,pack_id,pack_name,p_face_value,pm_prefix from Operator where aim_network_id='{0}'", gridbackOffice["aim_network_id", gridbackOffice.CurrentCell.RowIndex].Value); OleDbCommand getSelectedGridDatecmd = new OleDbCommand(sql, conn); OleDbDataReader reader = getSelectedGridDatecmd.ExecuteReader(); while (reader.Read()) { txtAimNetworkID.Text = reader.GetString(0); txtAimNetworkName.Text = reader.GetString(1); txtPARNetworkID.Text = reader.GetString(2); txtPARFaceValue.Text = reader["p_face_value"].ToString(); //in above line if i'm doing this `reader.GetString(5)` then i'm getting specified cast exception and that to randomly i.e some time it works fine and suddenly sometime gives this exception txtPARPackID.Text = reader.GetString(3); txtPARPackName.Text = reader.GetString(4); txtPARPMPrefix.Text = reader["pm_prefix"].ToString(); }I'm little bit confused if m using this reader["p_face_value"].ToString() then my code is running very smoothly but whats the issue with using this reader.GetString(5) , according to me both method return string, nebody had faced this error b4 ? ....Error is at 4th and 7th line in while loop.
Exception:Specified cast is not valid (InvalidCastException unhandled)
最满意答案
根据MSDN, OleDbDataReader.GetString()在尝试强制转换为字符串之前不执行任何转换 - 因此检索的数据必须已经是字符串。
如果该列中的值可能为null ,则文档建议您应首先检查该值是否为null :
if ( !reader.IsDBNull(5) ) { txtPARFaceValue.Text = reader.GetString(5); }在null值上调用reader["p_face_value"]将返回DBNull - 当您在DBNull上调用ToString()时 ,您将获得一个空字符串。
According to MSDN, OleDbDataReader.GetString() does not perform any conversions before attempting to cast to a string - therefore the data retrieved must already be a string.
If there is a chance that the value in that column could be null, the docs suggest that you should check if the value is null first:
if ( !reader.IsDBNull(5) ) { txtPARFaceValue.Text = reader.GetString(5); }Calling reader["p_face_value"] on a null value returns DBNull - and when you call ToString() on DBNull, you get an empty string.
更多推荐
发布评论