您当前的位置: 百科371电脑/网络C#/.NET → 电脑/网络内容 退出登录 用户管理 用户注册
本类热门文章
相关文章
中原建材网
"未将对象引用设置到对象的实例"

减小字体 增大字体

问题:int temp=0; int number=0; string sql = string.Format(select number from orderinfo where FlightNo={0}and LeaveDate={1}, textBox11.Text.Trim(),textBox9.Text.Trim()); SqlCommand com = new SqlCommand(sql, DBHelp.con); try { number = int.Parse(textBox10.Text.Trim()); DBHelp.con.Open(); temp = (int)com.ExecuteScalar(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); }运行的时候 查的东西要是数据库里有就没事,没有的话就会出现未将对象引用设置到对象的实例 请哪位帮帮我啊!  

因为没有符合条件记录,com.ExecuteScalar(); 执行的结果为空,导致对null进行向int类型的转换出错。
建议修改
temp = (int)com.ExecuteScalar();

string temp = com.ExecuteScalar();
if(temp!=null)
{
temp = (int)temp;
}

如果你对int temp=0; int number=0; string sql = string.Format(select number from orderinfo where FlightNo={0}and LeaveDate={1}, textBox11.Text.Trim(),textBox9.Text.Trim()); SqlCommand com = new SqlCommand(sql, DBHelp.con); try { number = int.Parse(textBox10.Text.Trim()); DBHelp.con.Open(); temp = (int)com.ExecuteScalar(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelp.con.Close(); }运行的时候 查的东西要是数据库里有就没事,没有的话就会出现未将对象引用设置到对象的实例 请哪位帮帮我啊!这个问题有好的意见或 建议,请留言
[] [返回上一页] [打 印]
电脑/网络评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码

中原建材网 简单版 站长QQ:382546553