GridView with objectdatasource

Discussion in 'ASP .Net Web Controls' started by Chuck P, Nov 15, 2006.

  1. Chuck P

    Chuck P Guest

    I have a gridview with an objectdatasource which has a timestamp data field
    byte[]

    When I do an update my object gets passed backed to my data access layer but
    the timestamp value is null (the other fields all have the edited values).
    It should be the original value.

    It appears that the gridview is not passing the timestamp (named RowVersion)
    back.
    I put the the timestamp field in the grid as a not visible field.
    Is their something special I need to do?


    using System.ComponentModel;

    public class Discipline
    {

    public Discipline()
    {}


    public Discipline(string description,
    int disciplineID,
    bool isActive,
    byte[] rowVersion)
    {
    this.Description = description;
    this.DisciplineID = disciplineID;
    this.IsActive = isActive;
    this.RowVersion = rowVersion;
    }

    private string m_Description;
    [DataObjectField(false, false, false, 50) ]
    public string Description
    {
    get { return m_Description; }
    set { m_Description = value; }
    }

    private int? m_DisciplineID;
    [DataObjectField(true, true, false)]
    public int? DisciplineID
    {
    get { return m_DisciplineID; }
    set { m_DisciplineID = value; }
    }

    private bool m_IsActive;
    [DataObjectField(false, false, false)]
    public bool IsActive
    {
    get { return m_IsActive; }
    set { m_IsActive = value; }
    }

    private byte[] m_RowVersion;
    [DataObjectField(false, false, false)]
    public byte[] RowVersion
    {
    get { return m_RowVersion; }
    set { m_RowVersion = value; }
    }

    }



    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;


    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.ComponentModel;
    using System.Data.Common;
    using System.Data;

    [DataObject]
    public class lutDisciplinesDAL
    {
    public lutDisciplinesDAL()
    {
    }


    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public List<Discipline> GetOne(int ID)
    {

    List<Discipline> Disciplines = new List<Discipline>();

    using (SqlConnection conn = new
    SqlConnection(Util.DB.GetConnectionString("cnnWorkForceMobility")))
    {
    SqlCommand cmd = web_lutDisciplines_Sel(ID);
    cmd.Connection = conn;

    conn.Open();
    using (SqlDataReader dr = cmd.ExecuteReader())
    {

    int iDisciplineID = dr.GetOrdinal("DisciplineID");
    int iDescription = dr.GetOrdinal("Description");
    int iIsActive = dr.GetOrdinal("IsActive");
    int iRowVersion = dr.GetOrdinal("RowVersion");


    while (dr.Read())
    {
    Discipline pb = new Discipline();
    pb.DisciplineID = (int)dr[iDisciplineID];
    pb.Description = (string)dr[iDescription];
    pb.IsActive = (bool)dr[iIsActive];
    pb.RowVersion = (Byte[])dr[iRowVersion];


    Disciplines.Add(pb);
    }
    }
    }
    return Disciplines;
    }

    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public List<Discipline> GetAll()
    {

    List<Discipline> Disciplines = new List<Discipline>();

    using (SqlConnection conn = new
    SqlConnection(Util.DB.GetConnectionString("cnnWorkForceMobility")))
    {
    SqlCommand cmd = web_lutDisciplines_Sel();
    cmd.Connection = conn;

    conn.Open();
    using (SqlDataReader dr = cmd.ExecuteReader())
    {

    int iDisciplineID = dr.GetOrdinal("DisciplineID");
    int iDescription = dr.GetOrdinal("Description");
    int iIsActive = dr.GetOrdinal("IsActive");
    int iRowVersion = dr.GetOrdinal("RowVersion");


    while (dr.Read())
    {
    Discipline pb = new Discipline();
    pb.DisciplineID = (int)dr[iDisciplineID];
    pb.Description = (string)dr[iDescription];
    pb.IsActive = (bool)dr[iIsActive];
    pb.RowVersion = (Byte[])dr[iRowVersion];


    Disciplines.Add(pb);
    }
    }
    }
    return Disciplines;
    }

    [DataObjectMethod(DataObjectMethodType.Update, false)]
    public static int UpSert(Discipline pb)
    {
    if ( pb.DisciplineID == null)
    return Insert(pb);
    else
    return Update(pb);
    }



    [DataObjectMethod(DataObjectMethodType.Update, false)]
    public static int Update(Discipline pb)
    {
    using (SqlConnection conn = new
    SqlConnection(Util.DB.GetConnectionString("cnnWorkForceMobility")))
    {
    SqlCommand cmd = web_lutDisciplines_upd(pb);
    cmd.Connection = conn;
    conn.Open();
    return cmd.ExecuteNonQuery();
    }
    }

    [DataObjectMethod(DataObjectMethodType.Insert, false)]
    public static int Insert(Discipline pb)
    {
    using (SqlConnection conn = new
    SqlConnection(Util.DB.GetConnectionString("cnnWorkForceMobility")))
    {
    SqlCommand cmd = web_lutDisciplines_ins(pb);
    cmd.Connection = conn;
    conn.Open();
    return cmd.ExecuteNonQuery();
    }
    }

    [DataObjectMethod(DataObjectMethodType.Delete, true)]
    public static int Delete(int DisciplineID)
    {
    using (SqlConnection conn = new
    SqlConnection(Util.DB.GetConnectionString("cnnWorkForceMobility")))
    {
    SqlCommand cmd = web_lutDisciplines_del(DisciplineID);
    cmd.Connection = conn;
    conn.Open();
    return cmd.ExecuteNonQuery();
    }
    }

    private static SqlCommand web_lutDisciplines_del(int disciplineID)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "web.lutDisciplines_del";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter p;

    p = new SqlParameter("@DisciplineID", SqlDbType.Int);
    p.Value = disciplineID;
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p);



    return cmd;
    }

    private static SqlCommand web_lutDisciplines_ins(Discipline obj)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "web.lutDisciplines_ins";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter[] p = new SqlParameter[2] ;

    p[0] = new SqlParameter("@Description", SqlDbType.VarChar);
    p[0].Value = obj.Description;
    p[0].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[0]);

    p[1] = new SqlParameter("@IsActive", SqlDbType.Bit);
    p[1].Value = obj.IsActive;
    p[1].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[1]);

    return cmd;
    }

    private static SqlCommand web_lutDisciplines_Sel()
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "web.lutDisciplines_Sel";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter p;

    p = new SqlParameter("@GetActiveOnly", SqlDbType.Bit);
    p.Value = false;
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p);

    return cmd;
    }

    private static SqlCommand web_lutDisciplines_Sel(int ID)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "web.lutDisciplinesByID_Sel";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter p;

    p = new SqlParameter("@DisciplineID", SqlDbType.Int);
    p.Value = false;
    p.Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p);

    return cmd;
    }

    private static SqlCommand web_lutDisciplines_upd(Discipline obj)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "web.lutDisciplines_upd";
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter[] p = new SqlParameter[4];

    p[0] = new SqlParameter("@DisciplineID", SqlDbType.Int);
    p[0].Value = obj.DisciplineID;
    p[0].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[0]);

    p[1] = new SqlParameter("@Description", SqlDbType.VarChar);
    p[1].Value = obj.Description;
    p[1].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[1]);

    p[2] = new SqlParameter("@IsActive", SqlDbType.Bit);
    p[2].Value = obj.IsActive;
    p[2].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[2]);

    p[3] = new SqlParameter("@RowVersion", SqlDbType.VarBinary);
    p[3].Value = obj.RowVersion;
    p[3].Direction = ParameterDirection.Input;
    cmd.Parameters.Add(p[3]);

    return cmd;
    }
    }
    Chuck P, Nov 15, 2006
    #1
    1. Advertising

  2. Hi Chuck,

    If the Visible property of a row field is set to false, the row is not
    displayed in the GridView control and the data for the row does not make a
    round trip to the client. If you want the data for a row that is not
    visible to make a round trip, add the field name to the DataKeyNames
    property:

    DataKeyNames="DisciplineID,RowVersion"


    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Nov 16, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kha Tran
    Replies:
    0
    Views:
    550
    Kha Tran
    Jun 3, 2005
  2. Danny W

    GridView update with ObjectDataSource

    Danny W, Sep 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,070
    Danny W
    Sep 8, 2005
  3. family

    gridview, objectdatasource

    family, Dec 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    531
    family
    Dec 22, 2005
  4. =?Utf-8?B?Sm9obl9I?=
    Replies:
    1
    Views:
    2,771
    =?Utf-8?B?Sm9obl9I?=
    Jan 19, 2006
  5. David Thielen

    ObjectDataSource method as another ObjectDataSource

    David Thielen, Mar 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    242
    Steven Cheng[MSFT]
    Mar 23, 2006
Loading...

Share This Page