问题: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(); }运行的时候 查的东西要是数据库里有就没事,没有的话就会出现未将对象引用设置到对象的实例 请哪位帮帮我啊!这个问题有好的意见或
建议,请留言
|