Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 July 31st, 2004, 07:49 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

jabby I know you are a chinese citizen
(I got it from you code this.dataSet21.Locale = new
System.Globalization.CultureInfo("zh-CN");)
and I'm a iranian,now just at this time there is a football match between
Korea-Republic and Iran and the winner will face china
now I am giving up everything for watching this football match,
Hope Iran wins......
(Moderators,excuse me for this discussion!).

--------------------------------------------
Mehdi.
 
Old July 31st, 2004, 09:39 AM
Authorized User
 
Join Date: Jul 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hehe,I hopes Iran will win too!Hope so!

 
Old July 31st, 2004, 09:48 AM
Authorized User
 
Join Date: Jul 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So there's my code after modified:
---------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace TestDelDataset
{
public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        protected SqlDataAdapter adapter;
        protected SqlConnection myConnection;
        protected DataSet ds;
        protected DataTable dt;
        protected DataRow dr;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                BindData();

            }
        }

        private void BindData()
        {
            myConnection = new SqlConnection("server=localhost;database=Northwind ;Trusted_Connection=yes;user id=sa;password=;") ;
            adapter=new SqlDataAdapter("select * from Categories",myConnection);
            ds=new DataSet();
            adapter.Fill(ds,"Categories");
            dt=ds.Tables["Categories"];
            this.DataGrid1.DataSource=dt;
            this.DataGrid1.DataBind();

            dt.PrimaryKey=new DataColumn[]{dt.Columns["CategoryID"]};

            ViewState["dt"]=dt;
            ViewState["pk"]=dt.PrimaryKey;
        }

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_Delete);
            this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
            this.Load += new System.EventHandler(this.Page_Load);

        }

    public void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
        {

        }
        public void DataGrid1_Delete(object sender,DataGridCommandEventArgs e)
        {
            try
            {
            DataTable dt=(DataTable)ViewState["dt"];
                Response.Write(dt.Rows.Count-1);

            int key=(int)this.DataGrid1.DataKeys[(int)e.Item.ItemIndex];
                Response.Write("Click "+key.ToString()+" Column");

                    dr=dt.Rows.Find(key);
                    if(dr.Equals(""))
                        Response.Write("null");
                    else
                        Response.Write("no null");
                dt.Rows.Remove(dr);


                //dr.Delete();
                //dr.AcceptChanges();
                //BindData();
                DataGrid1.DataBind();
            }
        catch(System.NullReferenceException pp){Response.Write(pp.Message);}
            catch(System.ArgumentException ae){Response.Write(ae.Message);}
        }
    }
}
------------------------------------------
I found the reason that when I delete a row from datagrid,and it will appear again i delete another row again,it may be save the DataTable and the PrimaryKey Columns to the ViewState.But the primaryKey must be serializable ,I don't know how to do it !
----------------------------------------------
By the way,I watch the football match too,because I don't hope China will face the Korea-Republic ,so I hope Iran will be the winner :)


 
Old July 31st, 2004, 10:35 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yoooooooooohoooooooooo
I'm happy,
Iran 4-3 Korea
in the next stage
Iran-China(host)
my prediction is Iran 2-1 China :))
what is your prediction?tell me!
Iran alway defeats China :))
(I will think off you problem and will answer you)
Regards to China Football Team .


--------------------------------------------
Mehdi.
 
Old July 31st, 2004, 11:43 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think your problem is due to your DataGrid1_Delete method
change it like below
-----
//...
//dr.Delete();
//dr.AcceptChanges();
//BindData();
ViewState["dt"]=dt;//***This line was added***
DataGrid1.DataSource=dt;//***This line was added***
DataGrid1.DataBind();
-----
I think now it should work correctly.



--------------------------------------------
Mehdi.
 
Old July 31st, 2004, 12:17 PM
Authorized User
 
Join Date: Jul 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HOHO,congratulation!So I am happy too,becase my problem had been solved!I use a ViewState save the sate of the datatable and then all be ok!
By the way,let's wait for the football match,and i welcome the Iran come to China!

 
Old July 31st, 2004, 11:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

u simply will use
Code:
urDataSet.urDataTable.Rows[N].[u]Delete</u>()
Thats it!

Always:),
Hovik Melkomian.
 
Old August 1st, 2004, 01:29 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Hovik("hamoon SALAMe khodemoon")
he didn't want to delete the row from datasource,he just wanted to delete it from DataSet,Therefore Rows[N].Remove() is better.
Please,refer previous messages.


--------------------------------------------
Mehdi.
 
Old August 1st, 2004, 11:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Oops so I dindt get it clear. Tnx for ur mess & sorry for wrong post.

Always:),
Hovik Melkomian.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete all the records in a table Kai Lai Access VBA 2 March 15th, 2016 07:20 AM
Delete Duplicate Records prasanta2expert SQL Server 2000 9 December 15th, 2006 10:44 PM
How to delete one of two duplicate records aarkaycee SQL Server 2000 0 August 20th, 2006 07:27 PM
delete records in database shoakat Classic ASP Databases 9 October 26th, 2004 08:31 PM
How to use the DELETE method in a DataSet thomaz C# 2 January 10th, 2004 02:28 PM





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