Results 1 to 9 of 9

Thread: Using UPDATE with a DATAGRID problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Using UPDATE with a DATAGRID problem

    Hi I am having problems making the update datagrid property working. It still uses the old value, and I have followed the example in "ASP.NET in three weeks" and it won't work... here is my code:

    firs the cs code behind:

    PHP Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb

    namespace 
    WebApplication12
    {
        
    /// <summary>
        /// Summary description for WebForm1.
        /// </summary>
        
    public class WebForm1 System.Web.UI.Page
        
    {
            protected 
    System.Data.DataSet ds;
            protected 
    System.Web.UI.WebControls.DataGrid DataGrid1;
        
            private 
    void Page_Load(object senderSystem.EventArgs e)
            {
                
                
                if(!
    Page.IsPostBack )
                {
                
                    
    BindData(); 
                }
            }

            
    #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.ds = new System.Data.DataSet();
                ((
    System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
                
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
                
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
                
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
                
    // 
                // ds
                // 
                
    this.ds.DataSetName "ds";
                
    this.ds.Locale = new System.Globalization.CultureInfo("sv-SE");
                
    this.Load += new System.EventHandler(this.Page_Load);
                ((
    System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();

            }
            
    #endregion

            
    public void DataGrid1_EditCommand(object sourceSystem.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                
    DataGrid1.EditItemIndex e.Item.ItemIndex;
                
    BindData();
            }

            public 
    void DataGrid1_CancelCommand(object sourceSystem.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                
    DataGrid1.EditItemIndex = -1;
                
    BindData();
            }

            public 
    void DataGrid1_UpdateCommand(object sourceSystem.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                
    ds.AcceptChanges(); 
     
                
    DataGrid1.DataSource ds

                
    BindData(); 
                 
            }

            public 
    void BindData()
            {
                
    OleDbConnection myConn = new OleDbConnection("Provider=SQLOLEDB;data source=4647117000N26;initial catalog=Northwind;persist security info=False;user id=logger;password=logger;workstation id=WS1321;packet size=4096"); 
                
    string sqlQuery "SELECT * FROM Customers";
                
                
    //DataSet ds = new DataSet(); 
                
    OleDbDataAdapter objCmd = new OleDbDataAdapter(sqlQuery,myConn);
                
    OleDbCommandBuilder cmdB = new OleDbCommandBuilder(objCmd);      
                
    objCmd.Fill(ds"Customers");
                
    DataGrid1.DataSource ds;
                
    Page.DataBind(); 
            }
        }

    And then there is my aspx page

    PHP Code:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication12.WebForm1" %>
    <!
    DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <
    HTML>
        <
    HEAD>
            <
    title>WebForm1</title>
            <
    meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
            <
    meta name="CODE_LANGUAGE" Content="C#">
            <
    meta name="vs_defaultClientScript" content="JavaScript">
            <
    meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        </
    HEAD>
        <
    body MS_POSITIONING="GridLayout">
            <
    form id="Form1" method="post" runat="server">
                <
    asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 149px; POSITION: absolute; TOP: 95px" runat="server" Width="627px" Height="428px" AutoGenerateColumns="False" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnCancelCommand="DataGrid1_CancelCommand" DataSource="<%# ds %>">
                    <
    AlternatingItemStyle BackColor="#C0C0FF"></AlternatingItemStyle>
                    <
    Columns>
                        <
    asp:BoundColumn DataField="CustomerID" HeaderText="Kund-ID"></asp:BoundColumn>
                        <
    asp:BoundColumn DataField="ContactName" HeaderText="Namn"></asp:BoundColumn>
                        <
    asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Uppdatera" HeaderText="Ändra" CancelText="Avbryt" EditText="Ändra"></asp:EditCommandColumn>
                    </
    Columns>
                </
    asp:DataGrid>
            </
    form>
        </
    body>
    </
    HTML
    This is C# syntax, but the pricipal is the same in vb.net.
    I am pretty sure I do something wrong in the update event, but I am not sure what to do really. I want to make the changes to my dataset and then use an OLEDBCOMMANDBUILDER object to update the database with the changes made to the local dataset. Then I want to update the datagrid with the new data...


    HEEELP!

    kind regards
    Henrik

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    slightly different way of doing it but it works:
    PHP Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    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 
    CSharp
    {
        public class 
    DataGridUpdate System.Web.UI.Page
        
    {        

            
    #region Web Form Designer generated code
            
    override protected void OnInit(EventArgs e)
            {
                
    InitializeComponent();
                
    base.OnInit(e);
            }
            private 
    void InitializeComponent()
            {    
                
    this.Load += new System.EventHandler(this.Page_Load);
                
    Customers.EditCommand += new DataGridCommandEventHandler(Customers_EditCommand);
            }
            
    #endregion
            
            
    protected DataGrid Customers;
            
            private 
    void Page_Load(object senderSystem.EventArgs e)
            {
                if (!
    Page.IsPostBack)
                    
    BindGrid();
            }
            
            protected 
    void Customers_EditCommand(object senderDataGridCommandEventArgs e)
            {
                
    Customers.EditItemIndex e.Item.ItemIndex;
                
    BindGrid();
            }
            protected 
    void Customers_CancelCommand(object senderDataGridCommandEventArgs e)
            {
                
    Customers.EditItemIndex = -1;
                
    BindGrid();
            }
            protected 
    void Customers_UpdateCommand(object senderDataGridCommandEventArgs e)
            {
                
    string connString "user id=sa;password=sa;" 
                                    
    "database=NorthWind;server=DeathAngel;";
                
    SqlConnection cn = new SqlConnection(connString);
                
    string cmdText "Update Customers Set CompanyName=@CompanyName, " +
                                 
    "ContactName=@ContactName, ContactTitle=@ContactTitle " +
                                 
    "Where CustomerID=@CustomerID";
                
    SqlCommand cmd = new SqlCommand(cmdText,cn);
                
                
    string customerId Customers.DataKeyse.Item.ItemIndex ].ToString();
                
    cmd.Parameters.Add(new SqlParameter"@CustomerID"customerId));

                
    string companyName = ((TextBox)e.Item.Cells].Controls]).Text;
                
    cmd.Parameters.Add(new SqlParameter"@CompanyName" companyName));
                
                
    string contactName = ((TextBox)e.Item.Cells].Controls]).Text;
                
    cmd.Parameters.Add(new SqlParameter"@ContactName"contactName));

                
    string contactTitle = ((TextBox)e.Item.Cells].Controls]).Text;
                
    cmd.Parameters.Add(new SqlParameter"@ContactTitle"contactTitle));
                
                
    cmd.Connection.Open();
                
    cmd.ExecuteNonQuery();
                
    cmd.Connection.Close();
                
    Customers.EditItemIndex = -1;
                
    BindGrid();

            }
            private 
    void BindGrid()
            {
                
    string connString "user id=sa;password=sa;" +
                                    
    "database=NorthWind;server=DeathAngel;";
                
    SqlConnection cn = new SqlConnection(connString);
                
    string cmdText "Select * From Customers";
                
    SqlCommand cmd = new SqlCommand(cmdText cn);
                
    cmd.Connection.Open();
                
    Customers.DataSource cmd.ExecuteReader();
                
    Customers.DataBind();
                
    cmd.Connection.Close();
            }
        }

    page code:
    VB Code:
    1. <%@ Page language="c#" Codebehind="DataGridUpdate.aspx.cs"
    2.     AutoEventWireup="false" Inherits="CSharp.DataGridUpdate" %>
    3. <html>
    4.     <body<>
    5.         <form runat="server" id="DataGridUpdate">
    6.             <asp:DataGrid
    7.                 ID="Customers"
    8.                 Runat="server"
    9.                 AutoGenerateColumns="False"
    10.                 OnEditCommand="Customers_EditCommand"
    11.                 OnUpdateCommand="Customers_UpdateCommand"
    12.                 OnCancelCommand="Customers_CancelCommand"
    13.                 DataKeyField="CustomerID">
    14.                 <Columns>
    15.                     <asp:BoundColumn
    16.                         ReadOnly="True"
    17.                         HeaderText="Customer ID"
    18.                         DataField="CustomerID"/>
    19.                     <asp:BoundColumn
    20.                         HeaderText="Company Name"
    21.                         DataField="CompanyName"/>
    22.                     <asp:BoundColumn
    23.                         HeaderText="Contact Name"
    24.                         DataField="ContactName"/>
    25.                     <asp:BoundColumn
    26.                         HeaderText="Contact Title"
    27.                         DataField="ContactTitle"/>
    28.                     <asp:EditCommandColumn
    29.                         EditText="Edit"
    30.                         UpdateText="Update"
    31.                         CancelText="Cancel"/>
    32.                 </Columns> 
    33.             </asp:DataGrid>
    34.         </form>
    35.     </body>
    36. </html>

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    It works, tanks. But my general idea was to utilize oledb methods, like commandbuilder and dataadapter... and then I need to put the data in a dataset... I will try it myself, and when I succeed I will post the results here.

    The good think with commandbuilder is that it makes the UPDATE query for you

    best regards
    Henrik

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Oh, I had two questions also, perhaps you can answer them as well?

    1) I am having problems with paging the datagrid when using the cmd.ExecuteReader() as datasource for the datagrid... can you make paging work?



    2) A silly question, but if I want a label to adapt to dynamical controls (like the datagrid), how do I set that? Cause right now it has fixed position, and is put on top of the datagrid...



    kind regards
    Henrik

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    If you're using MSSQL Server you'll want to use the SqlClient namespace, not OLEDB, SqlClient is optimized for SQL Server and has all of the DataAdapter and Commandbuilder stuff OLEDB has.

    I don't like auto generated sql myself, so i tend to stay away from the commandbuilder.

    For paging and sorting to work "easily" you'll want to use the dataset over the datareader. This guy's site - http://www.geocities.com/jeff_louie/NET_General.htm has some really good info and examples on how to accomplish that.

    I don't understand your second question on the Label, maybe you could elaborate.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Thanks for the reply

    On the second question, first I place a datagrid on the "work area" then I place a webservercontrol label under that datagrid.

    If the datagrid at runtime contains lots of info, it tend to get quite "long". When that happens, the label placed below the datagrid doesn't align itself under the datagrid, but it stays "on top" of the datagrid... It's position is fixed, no matter how much space the other dynamical controls take... I want the label to move if the datagrid "pushes" it downwards...

    do you understand?


    best regards
    Henrik

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well if you drag the label onto the designer(i'm not a huge fan of the designer for this reason) you're gonna get absolute positioning. Just need to modify the style attribute of that label control and remove the absolute position style.

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Right click on the web form and select properties. Then, change the layout property. I can't remember the exact selection, but there are only two (flow I believe) selections.....That should fix your absolute position problem.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Hi, and sorry for my late reply. I couldn't find any properties that allowed me to change the positioning. So I solved it the good old fashioned way, with tables. Still working on the paging problem though.



    best regards
    Henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width