2011-12-03 1 views
0

DELETE 명령에 매개 변수를 전달할 때 몇 가지 문제가 발생하며 어떻게 작동하는지 잘 이해하지 못하는 것 같습니다.SQLDataSource 매개 변수를 가져 오려면 DeleteCommand

<asp:SqlDataSource ID="sdsPropertyList" 
    runat="server" 
    ProviderName="<%$ appSettings:ProviderName %>" 
    ConnectionString="<%$ appSettings:ConnectionString %>" 
    SelectCommand="selPropertyByAcntID" 
    SelectCommandType="StoredProcedure" 
    OnSelecting="sdsPropertyList_Selecting" 
    OnSelected="sdsPropertyList_Selected" 
    DeleteCommand="delPropertyByPropID" 
    DeleteCommandType="StoredProcedure" 
    OnDeleting="sdsPropertyList_Deleting" 
    OnDeleted="sdsPropertyList_Deleted"> 
    <SelectParameters> 
     <asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" /> 
    </SelectParameters> 
    <DeleteParameters> 
     <asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" /> 
     <asp:Parameter Name="in_propID" Type="Int32" DefaultValue="0" /> 
    </DeleteParameters> 
</asp:SqlDataSource> 

<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList" 
    AutoGenerateColumns="false" CssClass="gvPropList"> 
    <Columns> 
     <asp:BoundField HeaderText="ID" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" /> 
     <asp:BoundField HeaderText="Property" DataField="prop_title" 
      ItemStyle-CssClass="gvPropTitle" > 
     <ItemStyle CssClass="gvPropTitle" /> 
     </asp:BoundField> 
     <asp:BoundField HeaderText="Units" DataField="unitCount" 
      ItemStyle-CssClass="gvUnitCount" > 
     <ItemStyle CssClass="gvUnitCount" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified" 
      ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" > 
     <ItemStyle CssClass="gvDate" /> 
     </asp:BoundField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
       <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <HeaderStyle CssClass="headerPropList"/> 
    <RowStyle CssClass="gvPropRow" /> 
</asp:GridView> 

protected void sdsPropertyList_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 
     int userID = Convert.ToInt32(Page.User.Identity.Name); 
     if (userID != 0) 
      e.Command.Parameters["in_acntID"].Value = userID; 
    } 

protected void sdsPropertyList_Deleting(object sender, SqlDataSourceCommandEventArgs e) 
    { 
     int userID = Convert.ToInt32(Page.User.Identity.Name); 
     if (userID != 0) 
     { 
      e.Command.Parameters["in_acntID"].Value = userID; 
     } 
    } 

SELECT 문은 userID의 입력 매개 변수 하나를 필요로합니다. 그러나 DELETE 문에는 2 개의 매개 변수 입력이 필요합니다. in_acntID = userID in_propID = 바운드 필드 데이터 필드 prop_id

내가 뭘 잘못하고 있니? SqlDataSource 수준에서 정의 된 경우 CommandName 및 CommandArgument를 ItemTemplate 수준에서 전달해야합니까? 있는 gridview

UPDATE

을 한 후로부터 행을 제거

  • DB
  • 테이블 (들)에서

    1. 삭제 기록 :

      나는 삭제 버튼은 다음과 달성하고자하는 몇 가지 추가 연구, 그 발견했습니다, Boundfield에 대한 매개 변수 및 HeaderText에 대한 이름은 귀하의 gridview 내의 값이 SQL com에 의해 사용될 수 있도록 동일해야합니다 데이터 소스의 mands.

      초기 선택 명령을 제외하고는 모든 참조 코드를 제거했습니다.

      모두 현재 근면하게 작업 중입니다.

    답변

    0

    몇 가지 추가 조사를 한 후 데이터 필드의 SQL 명령에서 gridview의 값을 사용할 수 있도록 매개 변수의 이름과 Boundfield의 HeaderText가 동일해야합니다.

    초기 선택 명령을 제외하고는 모든 참조 코드를 제거했습니다.

    모두 현재 근면하게 작업 중입니다.

    <asp:SqlDataSource ID="sdsPropertyList" 
        runat="server" 
        ProviderName="<%$ appSettings:ProviderName %>" 
        ConnectionString="<%$ appSettings:ConnectionString %>" 
        SelectCommand="selPropertyByAcntID" 
        SelectCommandType="StoredProcedure" 
        OnSelecting="sdsPropertyList_Selecting" 
        DeleteCommand="delPropertyByPropID" 
        DeleteCommandType="StoredProcedure" 
        OnDeleted="sdsPropertyList_Deleted" > 
        <SelectParameters> 
         <asp:Parameter Name="acnt_id" Type="Int32" /> 
        </SelectParameters> 
        <DeleteParameters> 
         <asp:Parameter Name="acnt_id" Type="Int32" /> 
         <asp:Parameter Name="prop_id" Type="Int32" /> 
        </DeleteParameters> 
    </asp:SqlDataSource> 
    <asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList" 
        AutoGenerateColumns="false" CssClass="gvPropList" DataKeyNames="acnt_id, prop_id"> 
        <Columns> 
         <asp:BoundField HeaderText="acnt_id" InsertVisible="true" DataField="acnt_id" ReadOnly="true" Visible="False" /> 
         <asp:BoundField HeaderText="prop_id" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" /> 
         <asp:BoundField HeaderText="Property" DataField="prop_title"> 
         <ItemStyle CssClass="gvPropTitle" /> 
         </asp:BoundField> 
         <asp:BoundField HeaderText="Units" DataField="unitCount" > 
         <ItemStyle CssClass="gvUnitCount" /> 
         </asp:BoundField> 
         <asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified" 
          ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" > 
         <ItemStyle CssClass="gvDate" /> 
         </asp:BoundField> 
         <asp:BoundField HeaderText="Active" DataField="prop_active"> 
         <ItemStyle CssClass="gvPropActive" /> 
         </asp:BoundField> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit"></asp:LinkButton> 
           <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add"></asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
        <HeaderStyle CssClass="headerPropList"/> 
        <RowStyle CssClass="gvPropRow" /> 
    </asp:GridView> 
    
    0

    ID가 목록 상자 또는 드롭 다운에있는 경우

    <DeleteParameters> 
           <asp:ControlParameter ControlID="controlname" Name="id" PropertyName="SelectedValue" Type="Int32" /> 
            </DeleteParameters> 
    

    위 항목은 삭제에 성공적으로 사용되었습니다. 이것은 텍스트 상자 또는 레이블에 대해 작동 할 수 있습니다. 이벤트를 처리하는 경우 전체 삭제 프로세스를 짝수 핸들러로 가져가는 것이 가장 좋은 방법일까요? 연결, 명령 실행을 포함하여 전체 SQL 설정을 지정하십시오. 이 방법은 나에게도 효과적이다.

    2

    MSDN documentation에 따르면, 당신은 GRIDVIEW에 DataKeyNames를 지정해야합니다

    "데이터 소스의 기본 키를 나타내는 필드 또는 필드를 지정합니다 DataKeyNames 속성을 사용하여 당신은 DataKeyNames 속성을 설정해야합니다. GridView 컨트롤의 자동 업데이트 및 삭제 기능이 작동하려면 이러한 키 필드의 값이 업데이트 또는 삭제할 행을 지정하기 위해 데이터 소스 컨트롤에 전달됩니다. "

    +0

    DataKeyNames = "prop_id, acnt_id"를 추가하려고했지만 원하는 결과를 얻지 못했습니다. 값을 datakeynames로 전달할 때 sdsPropertyList_Deleting 명령에 매개 변수로 전달되는 방법은 무엇입니까? – Rick

    +0

    이 대답은 나를위한 해결책이었습니다. 당신의 해결책은 다른 문제로 인한 것이지만, 사람들이 매개 변수로 포함 시키길 원한다면 항상 모든 데이터 키를 포함하도록 상기시키고 싶습니다.이 답변을 주셔서 감사합니다! – SelAromDotNet