哪来的异常?
string typeName = DropDownList1.SelectedValue.ToString();
string sql = "select * from POWERTYPE where TYPENAME='" + typeName + "'";
if (dataOperate.seleSQL(sql) > 0)
{
SqlDataReader reader = dataOperate.getRow(sql);
reader.Read();
TextBox3.Text = (string)reader["PRICE0"];
}
public static int seleSQL(string sql)
{
SqlConnection con = createCon();
con.Open();
SqlCommand com = new SqlCommand(sql, con);
try
{
int i = Convert.ToInt32(com.ExecuteScalar()); //调试此处有异常
//int i = (int)com.ExecuteScalar();
con.Close();
return i;
}
catch //(Exception e)
{
con.Close();
return 0;
}
}
大家帮忙看看,该怎么修改?
------解决方案--------------------
你要是判断是否有记录,直接
string sql = "select * from POWERTYPE where TYPENAME='" + typeName + "'";
SqlDataReader reader = dataOperate.getRow(sql);
if(reader.HasRows)
{
reader.Read();
TextBox3.Text = (string)reader["PRICE0"];
}
或者
if(reader.Read())
{
TextBox3.Text = (string)reader["PRICE0"];
}
没有必要执行seleSQL啊
string sql = "select * from POWERTYPE where TYPENAME='" + typeName + "'";
if(reader.Read())
{
TextBox3.Text = (string)reader["PRICE0"];
}
第一行第一列的数据是数字或者返回null 才能进行Convert.ToInt32,其他都不能执行Convert.ToInt32的