ruinas 0 Denunciar post Postado Agosto 14, 2007 Olá Amigos, Seguinte, estou começando com o ASP.NET agora.... Estava fazendo uma pagina simples com um GridView (Delete, Edit) e um DetailsView(Insert) para manipulação dos dados. Insert - TudoOK edit - TudoOK DELETE - Nada OK. Criei um metodo para fazer o delete do registro no access só que a função não está recebendo o parametro que no caso eh o idCategoria. Posto o código abaixo, vejam se podem me dar uma força.. já estou a alguns dias com este problema. Categoria.vb Imports Microsoft.VisualBasicImports System.ConfigurationImports System.DataImports System.Data.SqlClientImports System.Collections.GenericImports System.Data.OleDbPublic Class DAL Public Class Categoria Private _idCategoria As Int32 Private _idProfile As Int32 Private _categoria As String Private _comentario As String Public Sub New() End Sub Public Sub New(ByVal idCategoria As Int32, ByVal idProfile As Int32, ByVal categoria As String, _ ByVal comentario As String) Me._idCategoria = idCategoria Me._idProfile = idProfile Me._categoria = categoria Me._comentario = comentario End Sub Public Property idCategoria() As Int32 Get Return _idCategoria End Get Set(ByVal value As Int32) If value > 0 Then _idCategoria = value Else Throw New ArgumentException("O valor idCategoria não pode ser menor que 1.") End If End Set End Property Public Property idProfile() As Int32 Get Return _idProfile End Get Set(ByVal value As Int32) If value > 0 Then _idProfile = value Else Throw New ArgumentException("O valor idProfile não pode ser menor que 1.") End If End Set End Property Public Property categoria() As String Get Return _categoria End Get Set(ByVal value As String) _categoria = Left(value, 30) End Set End Property Public Property comentario() As String Get Return _comentario End Get Set(ByVal value As String) _comentario = Left(value, 30) End Set End Property End Class Public Class CategoriaDAL Public Shared Function getCategorias() As List(Of Categoria) Dim strSql As String strSql = "SELECT idCategoria, categoria, comentario FROM categoria" ' where idProfile = 1" & p_idProfile Dim myConnection As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("bancoConnectionString").ConnectionString) Dim myCommand As New OleDbCommand(strSql, myConnection) myConnection.Open() Dim reader As OleDbDataReader = myCommand.ExecuteReader() Dim results As New List(Of Categoria)() While reader.Read() Dim icategoria As New Categoria() icategoria.idCategoria = Convert.ToInt32(reader("idCategoria")) icategoria.categoria = reader("categoria").ToString() icategoria.comentario = reader("comentario").ToString() results.Add(icategoria) End While reader.Close() myConnection.Close() Return results End Function Public Shared Sub UpdateCategoria(ByVal idCategoria As Int32, ByVal categoria As String, _ ByVal comentario As String) Dim strSql As String strSql = "UPDATE categoria set categoria = @categoria, comentario = @comentario where idCategoria = @idCategoria" Using myConnection As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("bancoConnectionString").ConnectionString) Dim myCommand As New OleDbCommand(strSql, myConnection) myCommand.Parameters.Add(New OleDbParameter("@categoria", categoria)) myCommand.Parameters.Add(New OleDbParameter("@comentario", comentario)) myCommand.Parameters.Add(New OleDbParameter("@idCategoria", idCategoria)) myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() End Using End Sub Public Sub InsertCategoria(ByVal idProfile As Int32, ByVal categoria As String, _ ByVal comentario As String) Dim strSql As String Dim param As OleDbParameter strSql = "INSERT into categoria (idProfile, categoria, comentario) values (@idProfile, @categoria, @comentario)" Using myConnection As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("bancoConnectionString").ConnectionString) Dim myCommand As New OleDbCommand(strSql, myConnection) param = New OleDbParameter("@categoria", OleDbType.VarChar) param.Value = categoria myCommand.Parameters.Add(param) param = New OleDbParameter("@comentario", OleDbType.VarChar) param.Value = comentario myCommand.Parameters.Add(param) param = New OleDbParameter("@idProfile", OleDbType.Integer) param.Value = idProfile myCommand.Parameters.Add(param) myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() End Using End Sub Public Shared Sub DeleteCategoria(ByVal idCategoria As Integer) Dim strSql As String strSql = "DELETE [categoria] where idCategoria = @idCategoria" Using myConnection As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("bancoConnectionString").ConnectionString) Dim myCommand As New OleDbCommand(strSql, myConnection) myCommand.Parameters.Add(New OleDbParameter("@idCategoria", idCategoria)) myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() End Using End Sub End ClassEnd Class ---------------------------------------------------------------------------------------------------------------------------------------------- Categorias.aspx <%@ Page Language="VB" AutoEventWireup="false" CodeFile="categorias.aspx.vb" Inherits="categorias" %><%@ Register Src="rodape.ascx" TagName="rodape" TagPrefix="uc3" %><%@ Register Src="menu.ascx" TagName="menu" TagPrefix="uc2" %><%@ Register Src="topo.ascx" TagName="topo" TagPrefix="uc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0" style="width: 780px"> <tr> <td colspan="2" style="height: 62px"> <uc1:topo ID="Topo1" runat="server" /> </td> </tr> <tr> <td style="width: 3px; height: 99px"> <uc2:menu ID="Menu1" runat="server" valign="top"/> </td> <td style="width: 808px; height: 99px; text-align: center;"> <table border="0" cellpadding="0" cellspacing="0" style="width: 500px"> <tr> <td style="width: 100px; height: 34px;"> </td> </tr> <tr> <td style="width: 100px"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="objCategoria" ForeColor="#333333" GridLines="None" AllowPaging="True" HorizontalAlign="Center" Width="460px"> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <Columns> <asp:CommandField ButtonType="Button" CancelText="Cancelar" DeleteText="Deletar" EditText="Editar" InsertText="Inserir" NewText="Novo" SelectText="Selecionar" ShowEditButton="True" UpdateText="Atualizar" ShowDeleteButton="True"> <FooterStyle BackColor="#C0FFC0" /> </asp:CommandField> <asp:BoundField DataField="idCategoria" HeaderText="idCategoria" SortExpression="idCategoria" /> <asp:BoundField DataField="categoria" HeaderText="Categoria" SortExpression="categoria" /> <asp:BoundField DataField="comentario" HeaderText="Comentário" SortExpression="comentario" /> </Columns> <SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <RowStyle BackColor="#E3EAEB" /> <EditRowStyle BackColor="#7C6F57" /> </asp:GridView> <asp:ObjectDataSource ID="objCategoria" runat="server" SelectMethod="getCategorias" TypeName="DAL+CategoriaDAL" UpdateMethod="UpdateCategoria" InsertMethod="InsertCategoria" DeleteMethod="DeleteCategoria"> <UpdateParameters> <asp:Parameter Name="idCategoria" Type="Int32" /> <asp:Parameter Name="categoria" Type="String" /> <asp:Parameter Name="comentario" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="idProfile" Type="Int32" /> <asp:Parameter Name="categoria" Type="String" /> <asp:Parameter Name="comentario" Type="String" /> </InsertParameters> <DeleteParameters> <asp:Parameter Name="idCategoria" Type="Int32" /> </DeleteParameters> </asp:ObjectDataSource> <br /> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="idCategoria" DataSourceID="objCategoria" DefaultMode="Insert" Height="50px" Width="497px" CaptionAlign="Right" CellPadding="4" ForeColor="#333333" GridLines="None"> <Fields> <asp:BoundField DataField="idProfile" HeaderText="idProfile" SortExpression="idProfile" /> <asp:BoundField DataField="categoria" HeaderText="categoria" SortExpression="categoria" /> <asp:BoundField DataField="comentario" HeaderText="comentario" SortExpression="comentario" /> <asp:CommandField ShowInsertButton="True" /> </Fields> <InsertRowStyle BorderStyle="Solid" /> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <CommandRowStyle BackColor="#C5BBAF" Font-Bold="True" /> <RowStyle BackColor="#E3EAEB" /> <FieldHeaderStyle BackColor="#D0D0D0" Font-Bold="True" /> </asp:DetailsView> </td> </tr> <tr> <td style="width: 100px"> </td> </tr> </table> <br /> </td> </tr> <tr> <td colspan="2"> <uc3:rodape ID="Rodape1" runat="server" /> </td> </tr> </table> </div> </form></body></html> Compartilhar este post Link para o post Compartilhar em outros sites
Aguinelo Pedroso 0 Denunciar post Postado Agosto 17, 2007 Amigo, seguinte.... o grid tem uma propriedade chamada onde deve ser setado o campo que voce esta isando como parametro, se o parametro for id então use assim DataKeyNames="id" senão o grid fica sem referencia pra passar o parametro. Compartilhar este post Link para o post Compartilhar em outros sites