請看下面的代碼。asp.net數據庫連接
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand();
if (DropDownList3.SelectedItem.Text == "Economy")
{
seats = Convert.ToInt32(DropDownList1.SelectedItem.Text);
cmd.Connection = con;
con.Open();
cmd.CommandText = "select easeats from flight where fno='" + fn + "'";
int eds = Convert.ToInt32(cmd.ExecuteScalar());
if (eds > seats)
{
Panel2.Visible = true; //seats available
cl = DropDownList3.SelectedItem.Text;
seat = seats.ToString();
seats = eds;
}
else
{
Panel3.Visible = true; // seats not available
}
con.Close();
}
}
我收到錯誤行:int eds = Convert.ToInt32(cmd.ExecuteScalar());
和錯誤是
誤差轉換爲varchar值定義了int類型
什麼不對這個代碼?
數據庫中的easeats的類型是什麼? ExecuteScalar通常用於獲取count,它返回結果集第一行的第一列,可能是您得到的結果爲null,這就是您遇到問題的原因。請告訴我們在db中的easeats的類型以及它在數據庫中運行查詢時返回的內容 – Habib 2012-04-22 14:12:01
@user ...哇代碼質量在這裏很差,我希望我永遠不必採用任何平面該公司正在使網站的... :)開玩笑...:D – 2012-04-22 14:15:25