I
Iain
I have the following .net page with a pushbutton in a datagrid. This
pushbutton should delete the selected record.
It works in the same manner in many other pages of the same format (I
actually copied and pasted most of this code from another form).
Can anyone enlighten me please as to why this happens randomly and how
to evercome it as It is driving me nuts.
Thanks in advance for any assistance offered
Iain
This is the aspx page.
<%@ Page Language="c#" Debug="true" Codebehind="WebForm25.aspx.cs"
AutoEventWireup="false" Inherits="WorkInstruction.WebForm25" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Work Instruction Signoff Management</title>
<link rel="stylesheet" type="text/css" href="..\MainCSS.css" />
</head>
<body>
<form runat="server">
<center>
<div id="elButtonb">
<font size="3">
Work Instruction Management<br>
Workstation Management
</font>
<br>
</div>
<br>
<p>
<h1>
<font size="4">Workstation
</font>
<ASPropDownList id="Operation" runat="server"
AppendDataBoundItems="True" AutoPostBack="True"
OnSelectedIndexChanged="Operation_SelectedIndexChanged">
</ASPropDownList>
<asp:TextBox id="WorkstationName" Runat="Server" />
<ASP:Button id="InsertWorkstation" runat="server" width="170px"
text="Insert New Workstation" align="center">
</ASP:Button>
</h1>
<br>
<div runat="server"
style="DISPLAY: inline; WIDTH: 486px; POSITION: relative; HEIGHT:
15px"
ms_positioning="FlowLayout"
id="ErrorMessage">Label
</div>
<br>
<br>
<div align="center">
<asp:datagrid id="Workstation" runat="server" width="92%"
borderwidth="1px" borderstyle="Solid" bordercolor="#C0FFFF"
cellpadding="5" font-size="8pt" autogeneratecolumns="false"
headerstyle-backcolor="black" headerstyle-forecolor="white"
headerstyle-font-bold="true" itemstyle-verticalalign="top"
enableviewstate="true" allowsorting="True"
ondeletecommand="Workstation_DeleteCommand">
<EditItemStyle borderstyle="Dashed" bordercolor="#0000C0"
backcolor="#FFFFC0">
</EditItemStyle>
<ItemStyle verticalalign="Top">
</ItemStyle>
<HeaderStyle font-bold="True" horizontalalign="Center"
borderwidth="2px" forecolor="White" borderstyle="Double"
bordercolor="Red" backcolor="Black">
</HeaderStyle>
<Columns>
<ASP:BoundColumn datafield="OPERNAME" headertext="Operation
Name">
</ASP:BoundColumn>
<ASP:BoundColumn datafield="WORKSTATN" headertext="Workstation">
</ASP:BoundColumn>
<asp:ButtonColumn
HeaderText="Delete item"
ButtonType="PushButton"
Text="Delete"
CommandName="Delete" />
</Columns>
</asp:datagrid>
</div>
<p></p>
<p></p>
<p></p>
<p></p>
<br><br>
<div id="elButtonb">
<a id="Return" onclick="javascript:history.go(-1)">Return</a>
</div>
<p></p>
<p></p>
</center>
</form>
<h1></h1>
</body>
</html>
/* ******************************************* */
The following is the code behind page
/* ******************************************* */
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WorkInstruction
{
/// <summary>
/// Summary description for WebForm25.
/// </summary>
public class WebForm25 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Operation;
protected System.Web.UI.WebControls.DataGrid Workstation;
protected System.Web.UI.WebControls.TextBox WorkstationName;
protected System.Web.UI.WebControls.Button InsertWorkstation;
protected System.Web.UI.HtmlControls.HtmlGenericControl
ErrorMessage;
protected void BindTheData(string LOperation)
{
OleDbConnection con = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XX.XX.XX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
string LSql = "Select * From WRKINSTRUC.WORKSTATN Where Opername =
'" + LOperation + "' Order By Opername";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSql;
OleDbDataReader dsWorkstation = cmd.ExecuteReader();
Workstation.DataSource = dsWorkstation;
Workstation.DataBind ( );
con.Close();
}
private bool WorkstationExists (string POperation, string
PWorkstation)
{
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=XX.XX.XX.XX;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
string LSqlString = "Select * From WRKINSTRUC.WORKSTATN Where
OPERNAME = '" + POperation + "' and WORKSTATN = '" + PWorkstation +
"'";
conoper.Open();
cmdoper.Connection = conoper;
cmdoper.CommandType = CommandType.Text;
cmdoper.CommandText = LSqlString;
OleDbDataReader reader = cmdoper.ExecuteReader();
if (reader.HasRows)
{
return true;
}
else
{
return false;
}
}
protected void Operation_SelectedIndexChanged(object sender,
EventArgs e)
{
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=10.159.97.220;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
string LWSSql, LProdSQL, LDocSql, LCatSql;
string LOpSelected;
if (Operation.SelectedItem.Text == "Please Select An Operation")
{
LOpSelected = "*";
LDocSql = "";
LDocSql = "<script language=JavaScript>alert('No Operation
Selected. \n Please Select An Operation');</script>";
RegisterStartupScript("startupScript", LDocSql);
}
else
{
BindTheData(Operation.SelectedItem.Text);
}
}
/*
================================================================== */
/* This event does not
fire
*/
/*
================================================================== */
protected void Workstation_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OleDbConnection conn = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XX.XX.XX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
string LOperation = e.Item.Cells[0].Text;
string LWorkStation = e.Item.Cells[1].Text;
int LRow = e.Item.ItemIndex;
conn.Open();
cmd.Connection = conn;
string LSqlString = "DELETE FROM WRKINSTRUC.WORKSTATN Where
OPERNAME = '" + LOperation + "' and WORKSTATN = '" + LWorkStation +
"'";
cmd.CommandText = LSqlString;
cmd.ExecuteNonQuery();
conn.Close();
BindTheData(LOperation);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string LSql = "Select * From WRKINSTRUC.OPERATIONS Order By
OPERNAME ASC";
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=XX.XX.XX.XX;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
if ((! IsPostBack))
{
conoper.Open();
cmdoper.Connection = conoper;
cmdoper.CommandType = CommandType.Text;
cmdoper.CommandText = LSql;
OleDbDataReader dsOperation = cmdoper.ExecuteReader();
Operation.DataSource = dsOperation;
Operation.DataTextField = "OPERNAME";
Operation.DataValueField = "OPERNAME";
Operation.DataBind();
conoper.Close();
Operation.Items.Insert(0, new ListItem("Please Select An
Operation", "0"));
Operation.SelectedIndex = 0;
BindTheData(Operation.SelectedItem.Text);
}
else
{
BindTheData(Operation.SelectedItem.Text);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.InsertWorkstation.Click += new
System.EventHandler(this.InsertWorkstation_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void InsertWorkstation_Click(object sender, System.EventArgs
e)
{
// Insert the new workstation
OleDbConnection conn = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XXX.XX.XXX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
// Add the new record to the database
string LWorkstationName = "";
string LDocSql;
string LOperation;
string LSql = "Select * From WRKINSTRUC.OPERATIONS Order By
OPERNAME ASC";
try
{
LWorkstationName = WorkstationName.Text.ToUpper();
LOperation = Operation.SelectedItem.Text;
if (Operation.SelectedItem.Text == "Please Select An Operation")
{
LDocSql = "";
LDocSql = "<script language=JavaScript>alert('No Operation
Selected. \n Please Select An Operation');</script>";
RegisterStartupScript("startupScript", LDocSql);
}
else
{
if (LWorkstationName != "")
{
if (WorkstationExists(Operation.SelectedItem.Text,
WorkstationName.Text.ToUpper()))
{
ErrorMessage.InnerText = "Operation Already Exists";
string LString = "Operation Already Exists";
LString = "<script language=JavaScript>alert('Existing
Workstation. \n Please Enter A Valid Workstation');</script>";
RegisterStartupScript("startupScript", LString);
}
else
{
string LSqlString = "Insert Into WRKINSTRUC.WORKSTATN
(OPERNAME, WORKSTATN) VALUES ('" + Operation.SelectedItem.Text + "',
'" + WorkstationName.Text.ToUpper() + "')";
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSqlString;
OleDbDataReader dsOperation = cmd.ExecuteReader();
Operation.DataSource = dsOperation;
Operation.DataBind ( );
conn.Close();
BindTheData(LOperation);
WorkstationName.Text = "";
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSql;
OleDbDataReader dsOperation2 = cmd.ExecuteReader();
Operation.DataSource = dsOperation2;
Operation.DataTextField = "OPERNAME";
Operation.DataValueField = "OPERNAME";
Operation.DataBind();
conn.Close();
Operation.Items.Insert(0, new ListItem("Please Select An
Operation", "0"));
Operation.SelectedIndex = 0;
}
}
}
}
catch
{
string LString = "Unable To Insert A Blank Operation Record";
LString = "<script language=JavaScript>alert('No Workstation Data
Entered. \n Please Enter A Valid Workstation');</script>";
RegisterStartupScript("startupScript", LString);
}
}
pushbutton should delete the selected record.
It works in the same manner in many other pages of the same format (I
actually copied and pasted most of this code from another form).
Can anyone enlighten me please as to why this happens randomly and how
to evercome it as It is driving me nuts.
Thanks in advance for any assistance offered
Iain
This is the aspx page.
<%@ Page Language="c#" Debug="true" Codebehind="WebForm25.aspx.cs"
AutoEventWireup="false" Inherits="WorkInstruction.WebForm25" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Work Instruction Signoff Management</title>
<link rel="stylesheet" type="text/css" href="..\MainCSS.css" />
</head>
<body>
<form runat="server">
<center>
<div id="elButtonb">
<font size="3">
Work Instruction Management<br>
Workstation Management
</font>
<br>
</div>
<br>
<p>
<h1>
<font size="4">Workstation
</font>
<ASPropDownList id="Operation" runat="server"
AppendDataBoundItems="True" AutoPostBack="True"
OnSelectedIndexChanged="Operation_SelectedIndexChanged">
</ASPropDownList>
<asp:TextBox id="WorkstationName" Runat="Server" />
<ASP:Button id="InsertWorkstation" runat="server" width="170px"
text="Insert New Workstation" align="center">
</ASP:Button>
</h1>
<br>
<div runat="server"
style="DISPLAY: inline; WIDTH: 486px; POSITION: relative; HEIGHT:
15px"
ms_positioning="FlowLayout"
id="ErrorMessage">Label
</div>
<br>
<br>
<div align="center">
<asp:datagrid id="Workstation" runat="server" width="92%"
borderwidth="1px" borderstyle="Solid" bordercolor="#C0FFFF"
cellpadding="5" font-size="8pt" autogeneratecolumns="false"
headerstyle-backcolor="black" headerstyle-forecolor="white"
headerstyle-font-bold="true" itemstyle-verticalalign="top"
enableviewstate="true" allowsorting="True"
ondeletecommand="Workstation_DeleteCommand">
<EditItemStyle borderstyle="Dashed" bordercolor="#0000C0"
backcolor="#FFFFC0">
</EditItemStyle>
<ItemStyle verticalalign="Top">
</ItemStyle>
<HeaderStyle font-bold="True" horizontalalign="Center"
borderwidth="2px" forecolor="White" borderstyle="Double"
bordercolor="Red" backcolor="Black">
</HeaderStyle>
<Columns>
<ASP:BoundColumn datafield="OPERNAME" headertext="Operation
Name">
</ASP:BoundColumn>
<ASP:BoundColumn datafield="WORKSTATN" headertext="Workstation">
</ASP:BoundColumn>
<asp:ButtonColumn
HeaderText="Delete item"
ButtonType="PushButton"
Text="Delete"
CommandName="Delete" />
</Columns>
</asp:datagrid>
</div>
<p></p>
<p></p>
<p></p>
<p></p>
<br><br>
<div id="elButtonb">
<a id="Return" onclick="javascript:history.go(-1)">Return</a>
</div>
<p></p>
<p></p>
</center>
</form>
<h1></h1>
</body>
</html>
/* ******************************************* */
The following is the code behind page
/* ******************************************* */
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WorkInstruction
{
/// <summary>
/// Summary description for WebForm25.
/// </summary>
public class WebForm25 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Operation;
protected System.Web.UI.WebControls.DataGrid Workstation;
protected System.Web.UI.WebControls.TextBox WorkstationName;
protected System.Web.UI.WebControls.Button InsertWorkstation;
protected System.Web.UI.HtmlControls.HtmlGenericControl
ErrorMessage;
protected void BindTheData(string LOperation)
{
OleDbConnection con = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XX.XX.XX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
string LSql = "Select * From WRKINSTRUC.WORKSTATN Where Opername =
'" + LOperation + "' Order By Opername";
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSql;
OleDbDataReader dsWorkstation = cmd.ExecuteReader();
Workstation.DataSource = dsWorkstation;
Workstation.DataBind ( );
con.Close();
}
private bool WorkstationExists (string POperation, string
PWorkstation)
{
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=XX.XX.XX.XX;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
string LSqlString = "Select * From WRKINSTRUC.WORKSTATN Where
OPERNAME = '" + POperation + "' and WORKSTATN = '" + PWorkstation +
"'";
conoper.Open();
cmdoper.Connection = conoper;
cmdoper.CommandType = CommandType.Text;
cmdoper.CommandText = LSqlString;
OleDbDataReader reader = cmdoper.ExecuteReader();
if (reader.HasRows)
{
return true;
}
else
{
return false;
}
}
protected void Operation_SelectedIndexChanged(object sender,
EventArgs e)
{
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=10.159.97.220;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
string LWSSql, LProdSQL, LDocSql, LCatSql;
string LOpSelected;
if (Operation.SelectedItem.Text == "Please Select An Operation")
{
LOpSelected = "*";
LDocSql = "";
LDocSql = "<script language=JavaScript>alert('No Operation
Selected. \n Please Select An Operation');</script>";
RegisterStartupScript("startupScript", LDocSql);
}
else
{
BindTheData(Operation.SelectedItem.Text);
}
}
/*
================================================================== */
/* This event does not
fire
*/
/*
================================================================== */
protected void Workstation_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OleDbConnection conn = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XX.XX.XX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
string LOperation = e.Item.Cells[0].Text;
string LWorkStation = e.Item.Cells[1].Text;
int LRow = e.Item.ItemIndex;
conn.Open();
cmd.Connection = conn;
string LSqlString = "DELETE FROM WRKINSTRUC.WORKSTATN Where
OPERNAME = '" + LOperation + "' and WORKSTATN = '" + LWorkStation +
"'";
cmd.CommandText = LSqlString;
cmd.ExecuteNonQuery();
conn.Close();
BindTheData(LOperation);
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string LSql = "Select * From WRKINSTRUC.OPERATIONS Order By
OPERNAME ASC";
OleDbConnection conoper = new
OleDbConnection("provider=IBMDA400;Data Source=XX.XX.XX.XX;User
Id=XXX;password=delphiexe");
OleDbCommand cmdoper = new OleDbCommand();
if ((! IsPostBack))
{
conoper.Open();
cmdoper.Connection = conoper;
cmdoper.CommandType = CommandType.Text;
cmdoper.CommandText = LSql;
OleDbDataReader dsOperation = cmdoper.ExecuteReader();
Operation.DataSource = dsOperation;
Operation.DataTextField = "OPERNAME";
Operation.DataValueField = "OPERNAME";
Operation.DataBind();
conoper.Close();
Operation.Items.Insert(0, new ListItem("Please Select An
Operation", "0"));
Operation.SelectedIndex = 0;
BindTheData(Operation.SelectedItem.Text);
}
else
{
BindTheData(Operation.SelectedItem.Text);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.InsertWorkstation.Click += new
System.EventHandler(this.InsertWorkstation_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void InsertWorkstation_Click(object sender, System.EventArgs
e)
{
// Insert the new workstation
OleDbConnection conn = new OleDbConnection("provider=IBMDA400;Data
Source=XX.XXX.XX.XXX;User Id=XXX;password=delphiexe");
OleDbCommand cmd = new OleDbCommand();
// Add the new record to the database
string LWorkstationName = "";
string LDocSql;
string LOperation;
string LSql = "Select * From WRKINSTRUC.OPERATIONS Order By
OPERNAME ASC";
try
{
LWorkstationName = WorkstationName.Text.ToUpper();
LOperation = Operation.SelectedItem.Text;
if (Operation.SelectedItem.Text == "Please Select An Operation")
{
LDocSql = "";
LDocSql = "<script language=JavaScript>alert('No Operation
Selected. \n Please Select An Operation');</script>";
RegisterStartupScript("startupScript", LDocSql);
}
else
{
if (LWorkstationName != "")
{
if (WorkstationExists(Operation.SelectedItem.Text,
WorkstationName.Text.ToUpper()))
{
ErrorMessage.InnerText = "Operation Already Exists";
string LString = "Operation Already Exists";
LString = "<script language=JavaScript>alert('Existing
Workstation. \n Please Enter A Valid Workstation');</script>";
RegisterStartupScript("startupScript", LString);
}
else
{
string LSqlString = "Insert Into WRKINSTRUC.WORKSTATN
(OPERNAME, WORKSTATN) VALUES ('" + Operation.SelectedItem.Text + "',
'" + WorkstationName.Text.ToUpper() + "')";
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSqlString;
OleDbDataReader dsOperation = cmd.ExecuteReader();
Operation.DataSource = dsOperation;
Operation.DataBind ( );
conn.Close();
BindTheData(LOperation);
WorkstationName.Text = "";
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = LSql;
OleDbDataReader dsOperation2 = cmd.ExecuteReader();
Operation.DataSource = dsOperation2;
Operation.DataTextField = "OPERNAME";
Operation.DataValueField = "OPERNAME";
Operation.DataBind();
conn.Close();
Operation.Items.Insert(0, new ListItem("Please Select An
Operation", "0"));
Operation.SelectedIndex = 0;
}
}
}
}
catch
{
string LString = "Unable To Insert A Blank Operation Record";
LString = "<script language=JavaScript>alert('No Workstation Data
Entered. \n Please Enter A Valid Workstation');</script>";
RegisterStartupScript("startupScript", LString);
}
}