PC Review


Reply
Thread Tools Rate Thread

DataGrid UpdateCommand

 
 
Leona Smyth
Guest
Posts: n/a
 
      9th Jul 2004
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();
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
updatecommand not firing on DataGrid logan Microsoft ASP .NET 0 28th Jun 2005 06:37 PM
DataGrid + BoundColumn + UpdateCommand niki Microsoft ASP .NET 1 17th Jun 2005 01:56 PM
Datagrid UpdateCommand not taking new value Brett Hargreaves Microsoft C# .NET 3 2nd Sep 2004 05:02 PM
RE: Datagrid UpdateCommand not taking new value Brett Hargreaves Microsoft C# .NET 0 2nd Sep 2004 02:51 PM
DataGrid.UpdateCommand I can't get updated values =?Utf-8?B?T3plcg==?= Microsoft ASP .NET 0 12th Jun 2004 11:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:42 AM.