|
Subject:
|
deleting row in a datagrid * nightmare :-(
|
|
Posted By:
|
chrismogz
|
Post Date:
|
12/22/2005 5:57:19 PM
|
I've been having alot of problems trying to delete row's from my datagrid in asp.net C#. Just can not get it to work and it's driving me mad and really need some help on this. Am new to c# and asp.net as well
My code
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM ["+dtable+"]",objOleDbConn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);
DataSet custDS = new DataSet();
myDataAdapter.Fill(custDS,"+dtable+");
int rowToDelete = e.Item.ItemIndex;
custDS.Tables["+dtable+"].Rows[rowToDelete].Delete();
myDataAdapter.Update(custDS,"+dtable+");
myDataGrid.DataSource=custDS;
try
{
myDataGrid.DataBind();
}
catch
{
myDataGrid.CurrentPageIndex=0;
myDataGrid.DataBind();
}
Getting error's like :
Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.
for
myDataAdapter.Update(custDS,"+dtable+");
Or
Update unable to find TableMapping['Table'] or DataTable 'Table'.
for
myDataAdapter.Update(custDS);
Can some one help help me solve this ?
thanks for ur time and help
|
|
Reply By:
|
chrismogz
|
Reply Date:
|
12/22/2005 7:30:37 PM
|
Right i've had a little brake through but got stuck again.
i can get the code below to delete a row if a enter the table name which is stored in the db e.g peopletable
But want be able to delete many different tables rather than one.
tryed to use "dtable" which is a string that gets the name of the table being used and to show the correct datagrid.
But getting an error which am stuck on . any idea's and help would be great thanks for ur time.
Code used to get the table name from a dropdown menu on the poge:
if (!(DropDownList1.SelectedIndex == 0))
{
myDataGrid.Visible = true;
OleDbDataReader myDataReader;
OleDbCommand myCommand = new OleDbCommand("Select * From ["+DbId+DropDownList1.SelectedItem.Value+"]", objOleDbConn);
try
{
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
myDataGrid.DataSource = myDataReader;
myDataGrid.DataBind();
// dtable=DbId+DropDownList1.SelectedItem.Value;
TextBox1.Text=DbId+DropDownList1.SelectedItem.Value;
// creating a dataset
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("SELECT * FROM ["+DbId+DropDownList1.SelectedItem.Value+"]",objOleDbConn);
//DataSet TempDataSet = new DataSet();
myDataAdapter.Fill(TempDataSet, "dbconn");
DataTable TempDataTable = TempDataSet.Tables["["+DbId+DropDownList1.SelectedItem.Value+"]"];
OleDbCommandBuilder TempCmdBuilder = new OleDbCommandBuilder(myDataAdapter);
//foreach (DataRow mydataRow in TempDataSet.Tables["dbconn"].Rows)
}
catch (Exception myException)
{
Response.Write("An error has occurred: " + myException.ToString());
}
}
else
{
myDataGrid.Visible = false;
}
dtable=TextBox1.Text;
the delete code:
myDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM ["+dtable+"]",objOleDbConn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);
DataSet custDS = new DataSet();
myDataAdapter.Fill(custDS,"+dtable+");
int rowToDelete = e.Item.ItemIndex;
//custDS.Tables["2uni"].Rows[rowToDelete].Delete();
custDS.Tables["+dtable+"].Rows[rowToDelete].Delete();
myDataAdapter.Update(custDS,"+dtable+");
myDataGrid.DataSource=custDS;
error
Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.
on
myDataAdapter.Update(custDS,"+dtable+");
thanks for ur time
|
|
Reply By:
|
chrismogz
|
Reply Date:
|
12/22/2005 8:50:02 PM
|
no worries i've solved it :D WAHHHOOOOO . sorry just took me a long time to work out :)
|
|
Reply By:
|
suneel445
|
Reply Date:
|
10/30/2006 3:01:00 PM
|
Hi chrismogz,
iam also experiencing the same problem wen trying to delete row's from my datagrid in asp.net VB.
Can u plz let me know how u have solved it.
expecting a reply from u. Bye
|
|
Reply By:
|
peace95
|
Reply Date:
|
10/30/2006 4:47:43 PM
|
chrismogz:
One thing that I noticed in this statement: myDataAdapter.Fill(TempDataSet, "dbconn"); if I am reading that correctly the second parameter should be a data table instead of a database connection, i.e. dbconn....and I did not see where dbconn was defined as a table....
Hope this helps.
|