DataGrid.DeleteCommand

  • Thread starter Thread starter Duane Leem, BSIT
  • Start date Start date
D

Duane Leem, BSIT

Hi guys, I'm receiving this error:

'System.Web.UI.WebControls.BaseDataList.DataKeys' Denotes a 'property' where a 'method' was expected

It appears the error is at the DataKeys part... I highlighted it in red. Anyone know why this is causing an error?

------------------------------------------------------------------------------------------------------------------------
private void dgToDoList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int ID = dgToDoList.DataKeys(e.Item.ItemIndex);

OleDbConnection cnINEXP = new OleDbConnection(sConnectionString);

OleDbCommand oCommand = new OleDbCommand();

string sQuery = "DELETE FROM [To Do List] WHERE ID=" + "ID";

oCommand = new OleDbCommand(sQuery, cnINEXP);


cnINEXP.Open();

oCommand.ExecuteNonQuery();

cnINEXP.Close();

}
 
Hi Duane,

DataKeys is a collection, therefore you have to access it like:

int ID = dgToDoList.DataKeys [ e.Item.ItemIndex ];


but unfortunally the above line will fail too , you cannot cast it to int, it depends of the keys from your datasource. take a look at DataKeyCollection class in the MSDN it give you an example of how to work with it.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Hi guys, I'm receiving this error:

'System.Web.UI.WebControls.BaseDataList.DataKeys' Denotes a 'property' where a 'method' was expected

It appears the error is at the DataKeys part... I highlighted it in red. Anyone know why this is causing an error?

------------------------------------------------------------------------------------------------------------------------
private void dgToDoList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int ID = dgToDoList.DataKeys(e.Item.ItemIndex);

OleDbConnection cnINEXP = new OleDbConnection(sConnectionString);

OleDbCommand oCommand = new OleDbCommand();

string sQuery = "DELETE FROM [To Do List] WHERE ID=" + "ID";

oCommand = new OleDbCommand(sQuery, cnINEXP);


cnINEXP.Open();

oCommand.ExecuteNonQuery();

cnINEXP.Close();

}
 
Wow! That fixed the problem... I should have known that hehe... I'm a newbie... You been very helpful! Now I need to figure out how I can make the code work with DataKeys[e.Item.ItemIndex];


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message Hi Duane,

DataKeys is a collection, therefore you have to access it like:

int ID = dgToDoList.DataKeys [ e.Item.ItemIndex ];


but unfortunally the above line will fail too , you cannot cast it to int, it depends of the keys from your datasource. take a look at DataKeyCollection class in the MSDN it give you an example of how to work with it.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Hi guys, I'm receiving this error:

'System.Web.UI.WebControls.BaseDataList.DataKeys' Denotes a 'property' where a 'method' was expected

It appears the error is at the DataKeys part... I highlighted it in red. Anyone know why this is causing an error?

------------------------------------------------------------------------------------------------------------------------
private void dgToDoList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int ID = dgToDoList.DataKeys(e.Item.ItemIndex);

OleDbConnection cnINEXP = new OleDbConnection(sConnectionString);

OleDbCommand oCommand = new OleDbCommand();

string sQuery = "DELETE FROM [To Do List] WHERE ID=" + "ID";

oCommand = new OleDbCommand(sQuery, cnINEXP);


cnINEXP.Open();

oCommand.ExecuteNonQuery();

cnINEXP.Close();

}
 
Back
Top