About the page refresh !

K

Kylin

I write a page that delete the records from a dataset ,
and update to the data base ,
I click the delete button ,all that are right !
but when I refresh this page ,
a record will be deleted ! that's not I want .
why ?
and any ways about this

==============code under here !================

private SqlConnection myConn;
private SqlCommand myComm;
private SqlDataAdapter myAdapter;

private DataSet data=new DataSet() ;
private SqlCommandBuilder nameCmdBd ;

private void Page_Load(object sender, System.EventArgs e)
{
// ÔÚ´Ë´¦·ÅÖÃÓû§´úÂëÒÔ³õʼ»¯Ò³Ãæ
myConn=new SqlConnection("server=127.0.0.1;user
id=sa;pwd=sa;database=pubs");
myComm=new SqlCommand("Select a.* ,b.* From sales a , titles b Where
a.title_id=b.title_id",myConn);
myAdapter=new SqlDataAdapter();
myAdapter.SelectCommand=myComm;
myAdapter.Fill(data);

if(!this.IsPostBack)
{
DGBind();
}

}

private void BtnDelete_Click(object sender, System.EventArgs e)
{
for(int i=0;i<DataGrid1.Items.Count;i++)
{
CheckBox remove=(CheckBox)DataGrid1.Items.FindControl("remove");

if (remove.Checked==true)
{
// Response.Write(DataGrid1.DataKeys.ToString()+"</br>");
data.Tables[0].Rows.Delete();

}
}
myAdapter.SelectCommand.CommandText="Select * From sales";
nameCmdBd=new SqlCommandBuilder(myAdapter);
myAdapter.Update(data,"Table");

DGBind();

}
=========================code end ====================

Thanks a lot !
 
C

Cor Ligthert

Kylin,

This is very normal what you show.

After that you have deleted it, you bind the dataset again to your datagrid.

(Maybe it is better to save your dataset in a session by the way, this is a
very unsure way of processing)

I hope this helps,

Cor
 
G

Guest

Reading again and again from the database when you are using a Dataset, in
the Page_Load event is not such a good idea, as it will slow the page
response. Instead, read once in the if !Page.IsPostBack and refresh the
dataset data only when required. First, move the entire code in the Page_Load
event inside the if !Page.IsPostBack.

with regards,

J.v.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top