C#'必须声明标量变量“@ID_ZAPISA”'错误

时间:2011-08-26 16:23:01

标签: c# asp.net

我遇到以下代码问题。删除功能不起作用(抛出标量变量错误)。更新功能不起作用......它不会抛出任何错误,但它根本不会对数据库进行更改。

如果我制作精确的DetailsView,一切正常。问题在哪里?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ServisnaKnjiga.aspx.cs" Inherits="CernaticJurij_Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        Registrska številka:&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Iskanje" />
    </p>
    <p>
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" 
            AutoGenerateRows="False" CellPadding="4" DataSourceID="SqlDataSource1" 
            ForeColor="#333333" GridLines="None" Height="50px" Width="915px">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
            <EditRowStyle BackColor="#999999" />
            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
            <Fields>
                <asp:BoundField DataField="REG_STEVILKA" HeaderText="REGISTRSKA ŠTEVILKA" 
                    SortExpression="REG_STEVILKA" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="VIN_STEVILKA" HeaderText="VIN ŠTEVILKA" 
                    SortExpression="VIN_STEVILKA" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="ZNAMKA" HeaderText="ZNAMKA" 
                    SortExpression="ZNAMKA" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="MODEL" HeaderText="MODEL" SortExpression="MODEL" 
                    InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="LETO_IZDELAVE" HeaderText="LETO IZDELAVE" 
                    SortExpression="LETO_IZDELAVE" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="ID_ZAPISA" HeaderText="ID_ZAPISA" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ID_ZAPISA" />
                <asp:BoundField DataField="DATUM" HeaderText="DATUM" 
                    SortExpression="DATUM" />
                <asp:BoundField DataField="KILOMETRINA" HeaderText="KILOMETRINA" 
                    SortExpression="KILOMETRINA" />
                <asp:BoundField DataField="OPIS" HeaderText="OPIS" SortExpression="OPIS" />
                <asp:BoundField DataField="ODG_OSEBA" HeaderText="ODGOVORNA OSEBA" 
                    SortExpression="ODG_OSEBA" />
                <asp:BoundField DataField="CENA" HeaderText="CENA" SortExpression="CENA" />
                <asp:BoundField DataField="REG_STEVILKA1" HeaderText="REG_STEVILKA1" 
                    SortExpression="REG_STEVILKA1" Visible="False" />
                <asp:BoundField DataField="ID_STRANKE" HeaderText="ID_STRANKE" 
                    SortExpression="ID_STRANKE" Visible="False" />
                <asp:BoundField DataField="PROSTORNINA_MOTORJA" HeaderText="PROSTORNINA_MOTORJA" 
                    SortExpression="PROSTORNINA_MOTORJA" Visible="False" />
                <asp:BoundField DataField="MOC_MOTORJA" 
                    HeaderText="MOC_MOTORJA" SortExpression="MOC_MOTORJA" 
                    Visible="False" />
                <asp:BoundField DataField="TIP_MOTORJA" HeaderText="TIP_MOTORJA" 
                    SortExpression="TIP_MOTORJA" Visible="False" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                    ShowInsertButton="True" />
            </Fields>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        </asp:DetailsView>
    </p>
    <p>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Stranke %>" 
            DeleteCommand="DELETE FROM [SERVISNI_ZAPIS] WHERE [ID_ZAPISA] = @ID_ZAPISA"

            InsertCommand="INSERT INTO [SERVISNI_ZAPIS] ([REG_STEVILKA], [DATUM], [KILOMETRINA], [OPIS], [ODG_OSEBA], [CENA]) VALUES (@REG_STEVILKA, @DATUM, @KILOMETRINA, @OPIS, @ODG_OSEBA, @CENA)" SelectCommand="SELECT *
FROM SERVISNI_ZAPIS
FULL JOIN VOZILO
ON (SERVISNI_ZAPIS.REG_STEVILKA=VOZILO.REG_STEVILKA)
WHERE (SERVISNI_ZAPIS.REG_STEVILKA = @REG_STEVILKA)" 

            UpdateCommand="UPDATE [SERVISNI_ZAPIS] SET [REG_STEVILKA] = @REG_STEVILKA, [DATUM] = @DATUM, [KILOMETRINA] = @KILOMETRINA, [OPIS] = @OPIS, [ODG_OSEBA] = @ODG_OSEBA, [CENA] = @CENA WHERE [ID_ZAPISA] = @ID_ZAPISA">
            <DeleteParameters>
                <asp:Parameter Name="ID_ZAPISA" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA" 
                    PropertyName="Text" />
                <asp:Parameter DbType="Date" Name="DATUM" />
                <asp:Parameter Name="KILOMETRINA" Type="String" />
                <asp:Parameter Name="OPIS" Type="String" />
                <asp:Parameter Name="ODG_OSEBA" Type="String" />
                <asp:Parameter Name="CENA" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="REG_STEVILKA" 
                    PropertyName="Text" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="REG_STEVILKA" Type="String" />
                <asp:Parameter DbType="Date" Name="DATUM" />
                <asp:Parameter Name="KILOMETRINA" Type="String" />
                <asp:Parameter Name="OPIS" Type="String" />
                <asp:Parameter Name="ODG_OSEBA" Type="String" />
                <asp:Parameter Name="CENA" Type="String" />
                <asp:Parameter Name="ID_ZAPISA" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
</asp:Content>

2 个答案:

答案 0 :(得分:0)

你的意思是它不适用于gridview但是可以使用DetailsView吗?如果是这样,那么您需要为gridview设置DataKeyNames。

您可以查看详细信息:http://fabdata.wordpress.com/2007/03/23/must-declare-the-scalar-variable-id/

答案 1 :(得分:0)

我通过添加:

解决了这个问题
<asp:DetailsView ID="DetailsView1" runat="server" DataKeyNames="ID"....