Hello, I tried to display a DataGrid and have a 'Delete' link button inside a grid.
When execute, it displays perfectly but when I click a 'Delete' button - it shows a blank page and not delete a record. Please help me. There is a code:
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs" Inherits="_Defa ult" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" OnDeleteCommand ="DataGrid1_Del ete" DataKeyField="a u_id">
<Columns><asp:B uttonColumn Text="Delete" CommandName="De lete" /></Columns>
</asp:DataGrid>&n bsp;</div>
</form>
</body>
</html>
It is also a C# code behind it.
using System;
using System.Data;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
public partial class _Default : System.Web.UI.P age
{
String myConnString = "Server=(LOCAL) ;Initial Catalog=pubs;Us er ID=;Password=;C onnection Timeout=25;Inte grated Security=SSPI";
SqlConnection myConn;
SqlDataReader myDataReader;
protected void Page_Load(objec t sender, EventArgs e)
{
ShowDataGrid();
}
public void ShowDataGrid()
{
SqlConnection myConn = new SqlConnection(m yConnString);
SqlCommand myCommand = new SqlCommand("SEL ECT * FROM AUTHORS", myConn);
try
{
myConn.Open();
myDataReader = myCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection);
sDataBind();
}
finally
{
myConn.Close();
}
}
public void sDataBind()
{
if (!IsPostBack)
DataGrid1.DataS ource=myDataRea der;
DataGrid1.DataB ind();
}
public void DataGrid1_Delet e(object source, DataGridCommand EventArgs E)
{
SqlCommand myCommand;
String SQLQuery;
SqlConnection myConn = new SqlConnection(m yConnString);
SQLQuery = "DELETE FROM authors WHERE au_id='" + DataGrid1.DataK eys[E.Item.ItemInde x] + "';";
Response.Write( SQLQuery);
myCommand = new SqlCommand(SQLQ uery, myConn);
try
{
myConn.Open();
myCommand.Execu teNonQuery();
}
catch(Exception Ex){
Response.Write( "There is an error occured " + Ex.ToString() + ";");
}
finally
{
myConn.Close();
}
ShowDataGrid();
}
}
When execute, it displays perfectly but when I click a 'Delete' button - it shows a blank page and not delete a record. Please help me. There is a code:
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs" Inherits="_Defa ult" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="DataGrid1" runat="server" OnDeleteCommand ="DataGrid1_Del ete" DataKeyField="a u_id">
<Columns><asp:B uttonColumn Text="Delete" CommandName="De lete" /></Columns>
</asp:DataGrid>&n bsp;</div>
</form>
</body>
</html>
It is also a C# code behind it.
using System;
using System.Data;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
public partial class _Default : System.Web.UI.P age
{
String myConnString = "Server=(LOCAL) ;Initial Catalog=pubs;Us er ID=;Password=;C onnection Timeout=25;Inte grated Security=SSPI";
SqlConnection myConn;
SqlDataReader myDataReader;
protected void Page_Load(objec t sender, EventArgs e)
{
ShowDataGrid();
}
public void ShowDataGrid()
{
SqlConnection myConn = new SqlConnection(m yConnString);
SqlCommand myCommand = new SqlCommand("SEL ECT * FROM AUTHORS", myConn);
try
{
myConn.Open();
myDataReader = myCommand.Execu teReader(System .Data.CommandBe havior.CloseCon nection);
sDataBind();
}
finally
{
myConn.Close();
}
}
public void sDataBind()
{
if (!IsPostBack)
DataGrid1.DataS ource=myDataRea der;
DataGrid1.DataB ind();
}
public void DataGrid1_Delet e(object source, DataGridCommand EventArgs E)
{
SqlCommand myCommand;
String SQLQuery;
SqlConnection myConn = new SqlConnection(m yConnString);
SQLQuery = "DELETE FROM authors WHERE au_id='" + DataGrid1.DataK eys[E.Item.ItemInde x] + "';";
Response.Write( SQLQuery);
myCommand = new SqlCommand(SQLQ uery, myConn);
try
{
myConn.Open();
myCommand.Execu teNonQuery();
}
catch(Exception Ex){
Response.Write( "There is an error occured " + Ex.ToString() + ";");
}
finally
{
myConn.Close();
}
ShowDataGrid();
}
}