response.redirect in a method

liunx

Guest
Right i've created a datagrid, with an on item command and basically it deletes the selected row from the database

public void Detail(object sender, DataGridCommandEventArgs e)
{
//Build connection object //
int vpcode;
vpcode = Convert.ToInt32(e.Item.Cells[0].Text);

OleDbConnection conn = new OleDbConnection() ;
string connStr;
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;";
connStr += "Data Source= " + Server.MapPath("..\\..\\data\\dvds.mdb");
conn.ConnectionString = connStr;

// Build command object //
string queryString;
queryString = "Delete * from tCart where ProductCode ="+ vpcode;
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = conn;

// Open the connection //
conn.Open();

dbCommand.ExecuteNonQuery();

}

What Im trying to do is make it so that the page refreshes itself after it has deleted the desired record so the database updates automatically to show in the web page, but currently I have a seperate button which you have to click AFTER you have deleted the record to refresh the page using:
{
Response.Redirect("viewcart.aspx");
}
Does anybody know How I can put the response.redirect into the code which deletes the record so upon deletion the page automaticallyrefreshes itself, Ive tried just putting the Response.Redirect at the bottom of Public Void Detail but it doesnt seem to like itI think that you should put the response.redirect at the end of the deletion of the recod like this
// Open the connection //
conn.Open();

dbCommand.ExecuteNonQuery();
Response.Redirect("viewcart.aspx");
}

so when the code finished the execution of the query it will start the response.redirect

if this is the case you're looking for its more than easy, if not i think i do missunderstands youYeah, Mike is right. You said you got an error when you tried that. Lets see the error.

An alternative to response.redirect would be to create a method that fetches the datasource from your DB, binds the datasource to your datagrid, and the does a databind on the datagrid. Set the page.load event handler to call the method when not postback and then call the method at the end of your delete method. Here is some pseudo code.. In VB...


private sub page_Load()
If Not page.ispostback then
bindData()
End If
End Sub

private sub bindData()
'''Sql Code which gets you a datasource'''

datagrid.datasource = datasource 'From just above
datagrid.databind()
End Sub

private sub delete()
'''Sql Code for deleting row '''

bindData()
end Sub


Also, off the subject, you should probably close the connection and dispose of the resources.
 
Back
Top