Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2004, 03:04 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default Deleting a row from a datagrid

Guys,

I get this error: -

Compiler Error Message: CS1513: } expected

Heres my code: -

private void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
          DataGridFill();
    }
}

private void DataGridFill()
{

    DataSet objDataSet = new DataSet();
    objConnection = new OleDbConnection(strConnection);
    objAdapter = new OleDbDataAdapter(Select, objConnection);


    try
    {
        objConnection.Open();
        objAdapter.Fill(objDataSet, "Comments");
        dgComments.DataSource = objDataSet.Tables["Comments"];

    }
    catch(Exception Ex)
    {
        Response.Write(Ex.Message);
        Response.End();
    }
    finally
    {
        dgComments.DataBind();
        objConnection.Close();
    }
}

//public void EditRecord(object sender, DataGridCommandEventArgs e)
//{
// dgComments.EditItemIndex = e.item.ItemIndex;
// DataGridFill();
//}

private void DeleteRecord(Object sender, DataGridCommandEventArgs e)
{
    //Retrieve the value in the table
    int CommentID = Convert.ToInt32(e.Item.Cells[0].Text);
    DataSet objDataSet = new DataSet();
    objConnection = new OleDbConnection(strConnection);

    dgComments.EditItemIndex=-1;

    UpdateDataGrid(CommentID);

    dgComments.DataSource = objDataSet.Tables["Comments"];
    dgComments.DataBind();
}



private void UpdateDataGrid(long CommentID)
{
    objConnection = new OleDbConnection(strConnection);
    objConnection.Open();
    objAdapter = new OleDbDataAdapter(Select, objConnection);
    DataSet objDataSet = new DataSet();
    objAdapter.Fill(objDataSet, "Comments");
    objConnection.Close();

    DataTable tbl = objDataSet.Tables["Comments"];
    tbl.PrimaryKey = new DataColumn[]{tbl.Columns[CommentID];}
    DataRow row = tbl.Rows.Find[CommentID];
    row.Delete();

    OleDbCommandBuilder objBuilder = new OleDbCommandBuilder(objAdapter);
    objConnection.Open();
    objAdapter.Update(objDataSet, "Comments");
    objConnection.Close();
}

Any ideas??

Adz - The World is not enough
__________________
Adz - Learning The J2EE Ways.
 
Old February 25th, 2004, 03:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Something doesn't look right with this line:

    tbl.PrimaryKey = new DataColumn[]{tbl.Columns[CommentID];}

Not sure what, I work with VB more than C#, but something just strikes me as odd with it.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 25th, 2004, 05:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Not a C# developer either but move the ';' to the other side of the '}' and try that.

 
Old February 25th, 2004, 07:27 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

guys,

I have tried another method but it is not deleting my records: -

private void DeleteRecord(Object sender, DataGridCommandEventArgs e)
{
  objConnection = new OleDbConnection(strConnection);
  String strSQL = "DELETE FROM Feedback WHERE CommentID = '?' ";
  OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
  objConnection.Open();
  objCommand.Parameters.Add("Comment", OleDbType.VarChar, 32, "Comment");
  int CommentID = Convert.ToInt32(e.Item.Cells[0].Text);
  objCommand.Parameters["Comment"].Value = CommentID.ToString();
  DataGridFill();
  objConnection.Close();

any ideas??

Adz - The World is not enough
 
Old February 25th, 2004, 07:35 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

I take it back, that will definently not work, right now how to I get this to delete from the data grid??

Adz - The World is not enough
 
Old February 26th, 2004, 01:35 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Guys,

I have tried a different method but Igot this error: -

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

This is my code: -
private void DeleteRecord(Object sender, DataGridCommandEventArgs e)
{
    objConnection = new OleDbConnection(strConnection);
    string strSQL = "DELETE FROM FeedBack WHERE CommentID = '" + dgComments.DataKeys[e.Item.ItemIndex] + "'";
    OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
    objConnection.Open();

      objCommand.ExecuteNonQuery();
      dgComments.EditItemIndex=-1;
      DataGridFill();
      objConnection.Close();

any ideas??



Adz - The World is not enough
 
Old February 26th, 2004, 01:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

This almost looks like the datakeys collection is empty. But that doesn't make any sense. You got me stumped.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 26th, 2004, 02:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Is the datakeys coming from the dataset or the datagrid? In one of my apps I had to tell the datagrid which one was the datakey like this:

<asp:DataGrid DataKeyField="id" ...

 
Old February 26th, 2004, 07:26 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to aadz5
Default

Nice one Stu,

think that that has solved the proble, right the next error I get is the following: -

Data type mismatch in criteria expression.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Data type mismatch in criteria expression.

Source Error:


Line 62: objConnection.Open();
Line 63:
Line 64: (This line is highlighted)objCommand.ExecuteNonQuery();
Line 65: dgComments.EditItemIndex=-1;
Line 66: DataGridFill();

Any ideas




Adz - The World is not enough
 
Old December 3rd, 2004, 11:32 AM
Authorized User
 
Join Date: Dec 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem is in the SQL Statment. You probably are missing some quotes or something.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a row of an array Singh591 VB.NET 2002/2003 Basics 1 July 23rd, 2007 04:51 PM
Deleting selected row in ListBox YoungFools Excel VBA 1 April 19th, 2007 09:14 AM
deleting row in a datagrid * nightmare :-( chrismogz ADO.NET 4 October 30th, 2006 05:47 PM
problem in deleting the row from datagrid swati_joshi ASP.NET 1.0 and 1.1 Basics 10 March 24th, 2006 11:14 AM
Deleting every other row Russ Thompson Beginning VB 6 1 December 15th, 2004 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.