使用C#在ms访问查询中指定的强制转换无效异常(Specified cast is not valid exception in ms access query using C#)

编程入门 行业动态 更新时间:2024-10-28 02:25:59
使用C#在ms访问查询中指定的强制转换无效异常(Specified cast is not valid exception in ms access query using C#)

嘿伙计们有这个奇怪的转换异常虽然我的数据类型在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.

更多推荐

本文发布于:2023-07-20 11:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   cast   ms   valid   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!