Foros del Web » Programación para mayores de 30 ;) » .NET »

Problemas con ObjectDataSource-Pameter y TemplateField

Estas en el tema de Problemas con ObjectDataSource-Pameter y TemplateField en el foro de .NET en Foros del Web. Hola amigos, Tengo un problema. Estoy intentando que el siguiente codigo actualize un registro en una tabla de MYSQL y todo funciona perfectamente a EXCEPCION ...
  #1 (permalink)  
Antiguo 26/12/2008, 00:51
 
Fecha de Ingreso: diciembre-2008
Mensajes: 3
Antigüedad: 15 años, 4 meses
Puntos: 0
Problemas con ObjectDataSource-Pameter y TemplateField

Hola amigos,

Tengo un problema. Estoy intentando que el siguiente codigo actualize un registro en una tabla de MYSQL y todo funciona perfectamente a EXCEPCION del campo "perfil" que siempre que el programa entra dentro del metodo de update (UpdateMethod="saveUser") tiene valor de cero.

Creo que no estoy ligando bien el update parameter de perfil con el DropDownList que corresponde tambien a perfil.

Tengo 2 tablas: 1 para "User" y otra para "perfil" (esta ultima no la actualizo, solo la uso para llenar el DropDownList.

Les agradezco mucho su ayuda.

Un beso.


Codigo aspx:
Código:
    <asp:GridView id="UserGridView"
        DataSourceID="ObjetDataUserGridView"
        AutoGenerateDeleteButton="True"
        AutoGenerateEditButton="True"
        AutoGenerateColumns="False" SelectedDataKey="perfil"
        DataKeyNames="Id" 
        runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None"
        BorderWidth="1px" CellPadding="3" GridLines="Vertical" AllowPaging="True" 
         onrowupdated="updated" onrowupdating="updating" >
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <PagerSettings FirstPageText="<<" LastPageText=">>" NextPageText=">" PreviousPageText="<" Mode="NextPreviousFirstLast" />
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="Nombre" HeaderText="Nombre" SortExpression="Nombre" />
                <asp:BoundField DataField="Login" HeaderText="Login" SortExpression="Login" />
                <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
                <asp:TemplateField HeaderText="Perfil" >
                    <EditItemTemplate>
                        <asp:DropDownList id="ddlPerfil" runat="server" Width="120px" DataSourceID="SqlDataSourcePerfil" DataTextField="perfil" DataValueField="perfilid" AutoPostBack="false" SelectedValue='<%# Bind("perfil") %>'>
                        </asp:DropDownList>
                        <asp:SqlDataSource id="SqlDataSourcePerfil" runat="server" ConnectionString="<%$ ConnectionStrings:stringConnection %>" ProviderName="MySql.Data.MySqlClient"
                            SelectCommand="select perfilid, perfil from perfil">
                         </asp:SqlDataSource>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label id="LabelPerfil" runat="server" Text='<%# Eval("Perfil") %>' Width="136px"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="FechaCreacion" HeaderText="FechaCreacion" SortExpression="FechaCreacion" ReadOnly="True" />
                <asp:BoundField DataField="FechaModificacion" HeaderText="FechaModificacion" SortExpression="FechaModificacion" ReadOnly="True" />
            </Columns>
    </asp:GridView>
   
    <asp:ObjectDataSource runat="server"
        id="ObjetDataUserGridView"
        SelectMethod="GetAllUsers"
        UpdateMethod="saveUser"
        DeleteMethod="deleteUser"
        DataObjectTypeName="com.sis.telecabletepa.catalogos.User"
        TypeName="com.sis.telecabletepa.catalogos.UserManager" 
         onupdated="modificarPerfil_updated" onupdating="modificarPerfil_updating">
        <UpdateParameters>
            <asp:Parameter Name="Id" Type="Int16" />
            <asp:Parameter Name="Nombre" Type="String" />
            <asp:Parameter Name="Login" Type="String" />
            <asp:Parameter Name="Password" Type="String" />
            <asp:ControlParameter ControlID="ddlPerfil" Name="Perfil" PropertyName="SelectedValue" Type="Int16" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="FechaCreacion" Type="DateTime" />
            <asp:Parameter Name="FechaModificacion" Type="DateTime" />
        </UpdateParameters>
    </asp:ObjectDataSource>

Clase User:
Código:
    public class User
    {
        public User()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //Variables
        private int id;
        private string nombre;
        private string login;
        private string password;
        private int perfil;
        private string email;
        private DateTime fechacreacion;
        private DateTime fechamodificacion;
        private int estado;

        public int Id
        {
            set { id = value; }
            get { return id; }
        }

        public string Nombre
        {
            set { nombre = value; }
            get { return nombre; }
        }

        public string Login
        {
            set { login = value; }
            get { return login; }
        }

        public string Password
        {
            set { password = value; }
            get { return password; }
        }

        public int Perfil
        {
            set { perfil = value; }
            get { return perfil; }
        }

        public string Email
        {
            set { email = value; }
            get { return email; }
        }

        public DateTime Fechacreacion
        {
            set { fechacreacion = value; }
            get { return fechacreacion; }
        }

        public DateTime Fechamodificacion
        {
            set { fechamodificacion = value; }
            get { return fechamodificacion; }
        }

        public int Estado
        {
            set { estado = value; }
            get { return estado; }
        }
    }
metodo de update:
Código:
public static void saveUser(User user)
        {
               
            DateTime MyDateTime = DateTime.Now;            
            string sqlCmd = null;

            sqlCmd = "update usuario ";
            sqlCmd += "set ";
            sqlCmd += "nombre = '" + user.Nombre + "', login = '" + user.Login +
                "', password = '" + user.Password + "', perfilid = " + user.Perfil +
                ", email = '" + user.Email + "', fechamodificacion = '" + MyDateTime.ToString("yyyy/MM/dd") + 
                "' where usuarioid = " + user.Id;
            DataBaseConnection.modifySQLCommand(sqlCmd);
        }
  #2 (permalink)  
Antiguo 20/01/2009, 10:16
 
Fecha de Ingreso: diciembre-2008
Mensajes: 3
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Problemas con ObjectDataSource-Pameter y TemplateField

Hola a todos

Despues de buscar, finalmente encontre la solucion. Es de la siguente manera:

Se necesitaba agregar a mi consulta el registro que contiene el ID y el nombre del perfil

string strSql = "select usuario.usuarioid as Id, usuario.nombre as Nombre, usuario.login as Login, usuario.password as Password, " + perfil.perfil as NombrePerfil, usuario.email as Email, usuario.fechacreacion as FechaCreacion, " + "usuario.fechamodificacion as FechaModificacion, perfil.perfilid as Perfil from usuario inner join perfil on usuario.perfilid=perfil.perfilid where usuario.estado = 1 order by usuario.usuarioid ";

y cambiar en el gridview, en el campo Perfil

Código:
                 <asp:TemplateField HeaderText="Perfil" SortExpression="Perfil">
                    <ItemTemplate>
                        <asp:Label id="lblPerfil" runat="server" Text='<%# Eval("NombrePerfil") %>' Width="150px"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:SqlDataSource id="SqlDataSourcePerfil" runat="server" ConnectionString="<%$ ConnectionStrings:stringConnection %>" ProviderName="MySql.Data.MySqlClient"
                            SelectCommand="select perfilid as PerfilID, perfil as NombrePerfil from perfil order by perfil">
                         </asp:SqlDataSource>
                        <asp:DropDownList id="ddlPerfil" runat="server" Width="120px" DataValueField="PerfilID" DataSourceID="SqlDataSourcePerfil" 
                            DataTextField="NombrePerfil" AutoPostBack="False" AppendDataBoundItems="true" SelectedValue='<%# Bind("Perfil") %>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
Actualmente esto me funciona muy bien
http://static.forosdelweb.com/images/smilies/aplausos.gif
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:09.