以下代码来自res.aspx.cs
我无法从该字段中读取任何内容,此代码行导致我的错误:
strStatus = rdr["rStatus"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/App_Data/Database.accdb");
OleDbCommand cmd; OleDbDataReader rdr;
string strUserId = (string)Session["sUserId"];
string strStatus;
string strSql = "SELECT rStatus FROM Reservation WHERE rUserId =" + strUserId
+ " ORDER BY rStatus DESC;";
cmd = new OleDbCommand(strSql, mDB);
cmd.Parameters.Add("@UserId", OleDbType.Char).Value = strUserId;
mDB.Open();
rdr = cmd.ExecuteReader();
strStatus = rdr["rStatus"].ToString();
while (rdr.Read() == true)
// string strRStatus = (string)Session["srStatus"];
// while (rdr.Read() == true)
if (strStatus == "-1")
{
DetailsView1.Visible = false;
Label2.Visible = false;
btnBuy.Visible = false;
DropDownList1.Visible = false;
btnCancel.Visible = false;
lblReserve.Text = "There is no resevation for user " + Session["sUserId"];
}
else
{
lblReserve.Visible = false;
Label1.Visible = true;
Label2.Visible = true;
DetailsView1.Visible = true;
btnBuy.Visible = true;
DropDownList1.Visible = true;
btnCancel.Visible = true;
}
}
答案 0 :(得分:4)
尝试移动
strStatus = rdr["rStatus"].ToString();
到你打电话后
rdr.Read()
编辑:
如果要读取SQL位值,可以使用阅读器上的方法GetBoolean
:
bool myBoolean;
if (!rdr.IsDBNull(columnIndex))
myBoolean = rdr.GetBoolean(columnIndex);