onDeleteCommand problem

Cascade

Guppy
Hello friends,
I was trying to write a little application with DataGrid and 'Delete' button, quering a authors table from pubs database on SQL Server. An html code looks like:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" OnDeleteCommand="DataGrid1_Delete" DataKeyField="au_id">
<Columns><asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn></Columns>
</asp:DataGrid></div>
</form>
</body>
</html>

There is also C3 code behind it:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
String myConnString = "Server=(LOCAL);Initial Catalog=pubs;User ID=;Password=;Connection Timeout=25;Integrated Security=SSPI";
SqlConnection myConn;
SqlDataReader myDataReader;
protected void Page_Load(object sender, EventArgs e)
{
ShowDataGrid();

}
public void ShowDataGrid()
{
SqlConnection myConn = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand("SELECT * FROM AUTHORS", myConn);
try
{
myConn.Open();
myDataReader = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
sDataBind();
}
finally
{
myConn.Close();
}
}
public void sDataBind()
{
if (!IsPostBack)
DataGrid1.DataSource=myDataReader;
DataGrid1.DataBind();
}

protected void DataGrid1_Delete(object source, DataGridCommandEventArgs E)
{
SqlCommand myCommand;
String SQLQuery;
SqlConnection myConn = new SqlConnection(myConnString);
SQLQuery = "DELETE FROM authors WHERE au_id=" + DataGrid1.DataKeys[E.Item.ItemIndex] + ";";
Response.Write(SQLQuery);
myCommand = new SqlCommand(SQLQuery, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();

}
finally
{
myConn.Close();
}
ShowDataGrid();
}
}

When I push a 'Delete' button I receivea blank page, just nothing. And a record is not deleted.
Please help what a problem there may be?
 
Have you tried to set a break point on the click event of your delete button, that way you should be able to see exactly whats happening with your code when you click delete
 
Back
Top