catch er.message中指定的强制转换无效

本文关键字:转换 无效 er message catch | 更新日期: 2023-09-27 17:58:40

我试图在页面中显示这个dropdownlist对应的数据,但出现了错误。

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Select * from BookRequests where BookRequestId='" + DropDownList1.SelectedValue + "'", con);
    SqlDataReader reader;
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();
        TextBox1.Text = reader["BookRequestId"].ToString();
        TextBox2.Text = reader["BookRequest"].ToString();
        TextBox3.Text = reader["StudId"].ToString();
        TextBox4.Text = reader["Date"].ToString();
        CheckBox1.Checked = (bool)reader["Availability"];
    }
    catch (Exception er)
    {
        Response.Write(er.Message);
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, true, false, true);
    }

catch er.message中指定的强制转换无效

由于Availability可能为null,您应该检查null,如:

if (!reader.IsDBNull(reader.GetOrdinal("Availability")))
   CheckBox1.Checked = reader.GetBoolean(reader.GetOrdinal("Availability"));