I am trying to update data directly in a datagrid using
the UpdateCommand. However when I click the update button
in my datagrid nothing seems to happen. I keep going
round in circles and can't seem to work out whats wrong.
I've included the html code and the info from the code
behind page. Could someone please please help me work out
where i;ve went wrong.
HTML
<asp:datagrid id="DataGrid2" style="Z-INDEX: 101; LEFT:
12px; POSITION: absolute; TOP: 239px" runat="server"
CellPadding="3" BackColor="White" BorderWidth="1px"
BorderStyle="None" BorderColor="#CCCCCC" Width="653px"
AutoGenerateColumns="False" Font-Bold="True"
AllowSorting="True" DataKeyField="BANK_ID">
<Columns>
<asp:BoundColumn DataField="BANK_ID"
SortExpression="PER_LNAME" HeaderText="BankID"
ReadOnly="True">
</asp:BoundColumn>
<asp:BoundColumn DataField="PER_FNAME"
SortExpression="PER_LNAME" HeaderText="First Name"
ReadOnly="True">
</asp:BoundColumn>
<asp:BoundColumn DataField="PER_LNAME"
SortExpression="PER_LNAME" HeaderText="Surname"
ReadOnly="True">
</asp:BoundColumn>
<asp:BoundColumn DataField="PER_NIN"
SortExpression="PER_LNAME" HeaderText="NI Number">
</asp:BoundColumn>
<asp:BoundColumn DataField="BANK_TNAME"
SortExpression="PER_LNAME" HeaderText="Trading Name">
</asp:BoundColumn>
<asp:BoundColumn DataField="BANK_CDATE"
SortExpression="PER_LNAME" HeaderText="Ceased Trading">
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
CODE-BEHIND PAGE
private void DataGrid2_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string InBankkey = DataGrid2.DataKey
[e.Item.ItemIndex].ToString();
string TradeNames= " ";
TextBox TradeName = null;
TradeName = (TextBox)e.Item.Cells[4].Controls[0];
TradeNames = TradeName.Text.Trim();
string sqlUpdate = string.Format( "Update BANK set
BANK_TNAME='{0}' Where BANK_ID =
{1}",TradeNames,InBankkey);
SqlConnection conn = new SqlConnection("data
source=IRERESM12; initial catalog=TestDB; user
id=sa;password=rugby");
conn.Open();
SqlCommand s = new SqlCommand (sqlUpdate,conn);
s.ExecuteNonQuery ();
conn.Close();
DataGrid2.EditItemIndex = -1;
DisplayRes();
}
//Page Load and DisplayRes Below
private void Page_Load(object sender, System.EventArgs e)
{
DisplayRes();
}
private void DisplayRes()
{
SqlConnection conn = new SqlConnection("data
source=IRERESM12; " +
"initial catalog=TestDB; " +
"user id=sa; " +
"password=rugby");
DataSet ds = new DataSet();
SqlParameter @PER_LNAME = new SqlParameter
("@PER_LNAME", System.Data.SqlDbType.Char);
@PER_LNAME.Direction = ParameterDirection.Input;
@PER_LNAME.Value = Request.QueryString ["SN"];
SqlParameter @PER_FNAME = new SqlParameter
("@PER_FNAME", System.Data.SqlDbType.Char);
@PER_FNAME.Direction = ParameterDirection.Input;
@PER_FNAME.Value = Request.QueryString["FN"];
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "BankSearch2";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add(@PER_LNAME);
da.SelectCommand.Parameters.Add(@PER_FNAME);
ds = new DataSet();
da.Fill(ds, "BANK");
DataGrid2.DataSource = ds;
DataGrid2.DataMember = "BANK";
DataGrid2.DataBind();
}