1

我对asp.net 很陌生。我做了这个更新sql语句:

Dim dbconn As SqlConnection
    Dim dbcomm As SqlCommand


    dbconn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("sqlString"))
    dbconn.Open()

    Dim updateCmd As String = "UPDATE tbl_Project SET ID = @ID," _
     & " Titel = @Titel, CatID = @CatID, Website = @Website," _
     & " Naslag = @Naslag; WHERE ID = @ID;"

    dbcomm = New SqlCommand(updateCmd, _
     dbconn)
    dbcomm.Parameters.Add(New SqlParameter("@ID", SqlDbType.Int))
    dbcomm.Parameters.Add(New SqlParameter("@Titel", _
       SqlDbType.VarChar))
    dbcomm.Parameters.Add(New SqlParameter("@CatID", _
       SqlDbType.Int))
    dbcomm.Parameters.Add(New SqlParameter("@Website", _
       SqlDbType.VarChar))
    dbcomm.Parameters.Add(New SqlParameter("@Naslag", _
       SqlDbType.VarChar))
    dbcomm = New SqlCommand(updateCmd, dbconn)
    dbcomm.ExecuteNonQuery()`

我在 Forview 编辑模式下进行了所有设置。这个想法是,当您搜索记录并单击details它时,您将进入可编辑的项目视图。在详细信息页面中,您可以对其进行编辑并更新项目。

问题

当我单击更新按钮时,它显示:声明我拥有的变量@ID。而且我不知道我做错了什么。

这是我的表单视图:

            <table class="style1">
                <tr>
                    <td class="style2">
                        <asp:Label ID="Label1" runat="server" CssClass="kopje" Text="Categorie"></asp:Label>
                    </td>
                    <td class="style3">
                        <asp:Label ID="Label2" runat="server" CssClass="kopje" Text="Klant">        </asp:Label>
                    </td>
                    <td class="style4">
                        <asp:Label ID="Label3" runat="server" CssClass="kopje        " Text="Website"></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label5" runat="server" CssClass="kopje" Text="Titel">        </asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:DropDownList ID="DropDownCat" runat="server" 
                            DataSourceID="SqlDataSourceDrop" DataTextField="Categorie" 
                            DataValueField="Cat_ID" value="0">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSourceDrop" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:Goed %>" 
                            SelectCommand="SELECT * FROM [tbl_Cat]">
                            <SelectParameters>
                                <asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Decimal" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                    </td>
                    <td class="style3">
                        <asp:DropDownList ID="DropDownList1" runat="server" 
                            DataSourceID="SqlDataSourceKlant" DataTextField="Bedrijf" 
                            DataValueField="Klant_ID">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSourceKlant" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:Goed %>" 
                            SelectCommand="SELECT * FROM [tbl_Klant]"></asp:SqlDataSource>
                    </td>
                    <td class="style4">
                        <asp:TextBox ID="TextBoxWebsite" runat="server" Text='<%# Eval("Website") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBoxTitel" runat="server" Text='<%# Eval("Titel") %>'></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="TextBoxOmschrijving" runat="server" CssClass="kopje" 
                            Text="Omschrijving"></asp:Label>
                    </td>
                    <td class="style3">
                        &nbsp;</td>
                    <td class="style4">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:TextBox ID="TextBoxslag" runat="server" Height="200px" 
                            Text='<%# Eval("Naslag") %>' TextMode="MultiLine" Width="350px"></asp:TextBox>
                    </td>
                    <td class="style3">
                        &nbsp;</td>
                    <td class="style4">
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:LinkButton ID="LinkButton1" runat="server" 

CausesValidation="True" 
  CssClass="buttons" Text="Update"
  OnClientClick="Verzend"></asp:LinkButton>
                        </td>
                        <td class="style3">
                            &nbsp;</td>
                        <td class="style4">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                </table>
            </EditItemTemplate>
</asp:FormView>
4

4 回答 4

1

相信你需要AddWithValue。您需要指定的值@ID

dbcomm.Parameters.AddWithValue("@ID", "TheValue");
dbcomm.Parameters.AddWithValue("@Titel", "TitelValue");
于 2013-04-22T08:28:07.467 回答
1

你有一个(;)不应该在后面的分号

"@Naslag" in your SQL-command (" Naslag = @Naslag**;** WHERE ID = @ID;"). 可能足以导致您出现该错误。

于 2013-04-22T08:41:57.473 回答
0

我在你的代码中发现了一些错误,正确的代码应该是

Dim updateCmd As String = "UPDATE tbl_Project SET ID = @ID, Titel = @Titel, CatID = @CatID, Website = @Website, Naslag = @Naslag WHERE ID = @ID"
dbcomm = New SqlCommand(updateCmd, dbconn)
dbcomm.Parameters.AddWithValue("@ID", value)
dbcomm.Parameters.AddWithValue("@Titel", value)
dbcomm.Parameters.AddWithValue("@CatID", value)
.........
.........
// remove this-> dbcomm = New SqlCommand(updateCmd, dbconn)
dbcomm.ExecuteNonQuery()
于 2013-04-22T09:01:09.267 回答
0

请在您的代码中进行以下更改

         SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandText = "UPDATE tbl_Project SET ID = @ID," _
         & " Titel = @Titel, CatID = @CatID, Website = @Website," _
         & " Naslag = @Naslag WHERE ID = @ID";
        _cmd.Parameters.Add(new SqlParameter("@ID", '1'));
        _cmd.Parameters.Add(new SqlParameter("@Titel", 'title'));
        _cmd.Parameters.Add(new SqlParameter("@CatID", '1'));
        _cmd.Parameters.Add(new SqlParameter("@Website", 'www.google.com'));
        _cmd.Parameters.Add(new SqlParameter("@Naslag", 'Naslag'));
        if (_cmd.ExecuteNonQuery() > 0)
            return "Record Successfully Updated";
        else
            return "Record not Affected to DataBase";
于 2013-04-22T08:49:57.913 回答