因此,對於一個項目,我試圖從數據庫中檢索一個值,並在單擊編輯按鈕時將其存儲在佔位符中。我已經嘗試了許多事情來實現這個功能,但它只返回第一行第一列的值。我希望它從我選擇編輯的行返回值。這是我迄今在代碼背後的。使用OleDB檢索數據庫中的行
protected void grdEditPersonnel_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//sets the value of the row selected for possible use??
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = grdEditPersonnel.Rows[index];
try
{
OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["RiskManager_DBConnectionString"].ConnectionString);
OleDbCommand command = new OleDbCommand("SELECT [ID] FROM [tblProjects]", connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);
while (reader.Read())
{
//sends the value of the ID to the Project ID Place Holder
Session["ProjIDPH"] = reader.IsDBNull(reader.GetOrdinal("ID")) ? null :
reader["ID"].ToString();
}
reader.Close();
//Redirects user to the Add Project page
Response.Redirect("frmProjects.aspx");
}
catch
{
}
}
}
你怎麼看CommandBehavior.SingleRow呢? – Tim
你的sql字符串沒有where子句。 –
我知道它做了什麼,我的問題是它不能正常工作。另外,就WHERE子句而言,我已經嘗試了很多事情,包括使用行號,但似乎並不奏效。我應該如何設置WHERE子句? –