Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 17th, 2006, 03:04 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update command in Datagrid

Hi all,
I'm new to this group.
My update command of Datagrid doesn't work

here is the coding:
-------------------------------------------------------------
public class display : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        RegBLLCom objBLL;

        private string OrderBy
        {
            get
            {
                return ViewState["ORDERBY"] != null ? ViewState["ORDERBY"].ToString():"ASC";
            }
            set
            {
                ViewState["ORDERBY"] = value;
            }
        }

        private void makeConnection()
        {
            objBLL = new RegBLLCom(Config.connect2);
        }

        void Page_Load(object sender, System.EventArgs e)
        {
                makeConnection();
                DataGrid1.DataSource=objBLL.PopulateGrid();;
                DataGrid1.DataBind();
        }

        #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.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_cancel);
            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_edit);
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.gd_sort);
            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dg_Update);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void dg_edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex =(int) e.Item.ItemIndex;
            DataGrid1.DataBind();
        }

        private void dg_cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex = -1;
            DataGrid1.DataBind();
        }

        private void dg_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            //makeConnection();
            SqlConnection con = new SqlConnection(Config.connect2);
            string updateCement = "Update sample SET invid=@invid,itemno=@itemno,itemname=@itemname,qua ntity=@quantity WHERE invid=@invid";
            SqlCommand com = new SqlCommand(updateCement,con);
            com.Parameters.Add(new SqlParameter("@invid",SqlDbType.VarChar,50));
            com.Parameters.Add(new SqlParameter("@itemno",SqlDbType.VarChar,50));
            com.Parameters.Add(new SqlParameter("@itemname",SqlDbType.VarChar,50));
            com.Parameters.Add(new SqlParameter("@quantity",SqlDbType.NVarChar,50));
            com.Parameters["@invid"].Value = DataGrid1.DataKeys[(int) e.Item.ItemIndex];
            com.Parameters["@itemno"].Value = ((TextBox) e.Item.Cells[2].Controls[0]).Text;
            com.Parameters["@itemname"].Value = ((TextBox) e.Item.Cells[3].Controls[0]).Text;
            com.Parameters["@quantity"].Value = ((TextBox) e.Item.Cells[4].Controls[0]).Text;
            try
            {
                com.Connection.Open();
                com.ExecuteNonQuery();
                DataGrid1.EditItemIndex = -1;
                DataGrid1.DataSource=objBLL.PopulateGrid();
                DataGrid1.DataBind();
                con.Close();
            }
            catch(Exception x)
            {
                throw x;
            }
}
}
---------------------------------------------------------------
//HTML coding

<%@ Page language="c#" Codebehind="display.aspx.cs" AutoEventWireup="false" Inherits="RND.Registration.display" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>display</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <table height="200" cellSpacing="0" cellPadding="0" width="300" align="center" border="0">
                <tr vAlign="middle" width="100%">
                    <td vAlign="middle" align="center" width="50%">
                    <asp:datagrid DataKeyField=invid id="DataGrid1" runat="server" Height="161px" Width="251px" AllowSorting="True">
                            <Columns>
                                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                            </Columns>
                        </asp:datagrid></td>
                </tr>
            </table>
        </form>
    </body>
</HTML>
------------------------------------------------------------------------------------------

Can anyone tell me where's the problem ?
Kindly do respond ASAP.
Thanks,
with warm regards,
S.Chandra Mohan

Whatever happens, happens for good.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Update command yogeshyl Oracle 0 November 28th, 2007 08:05 AM
Invalid UPDATE command Mr. Vage SQL Language 4 April 14th, 2007 05:59 PM
Update/Cancel Command not responding in DataGrid acebookend ASP.NET 1.0 and 1.1 Professional 2 January 30th, 2006 03:00 PM
Update Command In DataGrid RPG SEARCH ASP.NET 1.0 and 1.1 Basics 9 February 21st, 2005 09:20 AM
ADO Command Update hcweb Classic ASP Basics 2 January 14th, 2004 06:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.