没有给出一个或多个必需参数的值。 VB

时间:2016-07-28 13:18:01

标签: asp.net vb.net visual-studio webforms

好吧,我已经根据之前提出的问题,改变了我的代码三四次,仍然无法获得我正在努力将数据插入到我的表中的表单。寻找有用的建议。

表格代码:

<div>
    <table align="center" width="200px" cellpadding="5px">
        <tr><td width="50px">Season:</td><td width="150px">
            <asp:DropDownList name="Season" ID="ddlSeason" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Season" DataValueField="Season">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SeasonConnectionString %>" ProviderName="<%$ ConnectionStrings:SeasonConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_Season] ORDER BY [Season]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Game: </td><td>
            <asp:DropDownList name="Game" ID="ddlGameNumber" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="Game" DataValueField="Game">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GameNumberConnectionString %>" ProviderName="<%$ ConnectionStrings:GameNumberConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_GameNumbers] ORDER BY [Game]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Team: </td><td>
            <asp:DropDownList name="Team" ID="ddlTeam" runat="server" Height="16px" Width="122px" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="Team" DataValueField="Team">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:TeamConnectionString %>" ProviderName="<%$ ConnectionStrings:TeamConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT * FROM [T_Teams] ORDER BY [Team]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Player: </td><td>
            <asp:DropDownList name="Player" ID="ddlPlayer" runat="server" Height="16px" Width="121px" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="Player" DataValueField="Player">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:PlayerConnectionString %>" ProviderName="<%$ ConnectionStrings:PlayerConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT [Team], [Player] FROM [T_Passing] WHERE (([Season] = ?) AND ([Team] = ?)) ORDER BY [Player]">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlSeason" Name="Season" PropertyName="SelectedValue" Type="String" />
                    <asp:ControlParameter ControlID="ddlTeam" Name="Team" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
            </td></tr>
        <tr><td>Atts: </td><td>
            <asp:DropDownList name="Atts" ID="ddlAtts" runat="server" DataSourceID="SqlDataSource5" DataTextField="Game" DataValueField="Game">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:AttsConnectionString %>" ProviderName="<%$ ConnectionStrings:AttsConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_Atts] ORDER BY [Game]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Comps: </td><td>
            <asp:DropDownList name="Comps" ID="ddlComps" runat="server" DataSourceID="SqlDataSource6" DataTextField="Comps" DataValueField="Comps">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:CompletionsConnectionString %>" ProviderName="<%$ ConnectionStrings:CompletionsConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_Comps] ORDER BY [Comps]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Yards: </td><td>
            <asp:TextBox name="Yards" ID="txtYards" runat="server" Width="68px" Text="0"></asp:TextBox>
            </td></tr>
        <tr><td>TDs: </td><td>
            <asp:DropDownList name="TDs" ID="ddlTDs" runat="server" DataSourceID="SqlDataSource7" DataTextField="TDs" DataValueField="TDs">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:TDConnectionString %>" ProviderName="<%$ ConnectionStrings:TDConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_TDs] ORDER BY [TDs]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Ints: </td><td>
            <asp:DropDownList name="Ints" ID="ddlInts" runat="server" DataSourceID="SqlDataSource8" DataTextField="Ints" DataValueField="Ints">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource8" runat="server" ConnectionString="<%$ ConnectionStrings:IntsConnectionString %>" ProviderName="<%$ ConnectionStrings:IntsConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_Ints] ORDER BY [Ints]"></asp:SqlDataSource>
            </td></tr>
        <tr><td>Long: </td><td>
            <asp:TextBox name="Longest" ID="txtLongest" runat="server" Width="67px" Text="0"></asp:TextBox>
            </td></tr>
        <tr><td colspan="2" align="center">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" />&nbsp;&nbsp;
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
            </td></tr>
    </table>
</div>

代码背后:

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim season As String = Request.Form("Season")
    Dim game As String = Request.Form("Game")
    Dim team As String = Request.Form("Team")
    Dim player As String = Request.Form("Player")
    Dim atts As String = Request.Form("Atts")
    Dim comps As String = Request.Form("Comps")
    Dim yards As String = Request.Form("Yards")
    Dim tds As String = Request.Form("TDs")
    Dim ints As String = Request.Form("Ints")
    Dim longest As String = Request.Form("Longest")

    Dim objConnection As OleDbConnection = Nothing
    Dim objcmd As OleDbCommand = Nothing

    Dim sqlString As String
    Dim dbconn As OleDbConnection = Nothing

    dbconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath("~/App_Data/TCFLStatistics.accdb"))
    dbconn.Open()

    sqlString = "Insert into T_Passing (Season, Game, Team, Player, Atts, Comps, Yards, TDs, INTs, Longest) Values (?,?,?,?,?,?,?,?,?,?)"
    objcmd = New OleDbCommand(sqlString, dbconn)
    objcmd.Parameters.AddWithValue("@Season", ddlSeason.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Game", ddlGameNumber.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Team", ddlTeam.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Player", ddlPlayer.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Atts", ddlAtts.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Comps", ddlComps.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Yards", txtYards.Text)
    objcmd.Parameters.AddWithValue("@TDs", ddlTDs.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@INTs", ddlInts.SelectedValue.ToString)
    objcmd.Parameters.AddWithValue("@Longest", txtLongest.Text)
    objcmd.ExecuteNonQuery()

    dbconn.Close()
    MsgBox("Record submitted successfully!")

End Sub

2 个答案:

答案 0 :(得分:1)

我找到了自己问题的答案!

当我继续滚动浏览许多类似的问题时,有人建议使用Response.Write命令来显示sql字符串以确保它看起来正确。我插入了一个Response.Write命令来显示连接字符串和sql字符串以验证准确性。我仍然有同样的错误。然后我明确地想出了提交按钮代码。我再次运行代码并得到相同的错误消息。这告诉我错误不在我的提交代码中。

我移动到代码的表单部分,开始注释掉文本框,并从结尾开始一次下拉列表。我在代码的这一部分中创建了sqldatasource链接之后发现我已经更改了表格中的列标题:

<asp:DropDownList name="Atts" ID="ddlAtts" runat="server" DataSourceID="SqlDataSource5" DataTextField="Game" DataValueField="Game">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:AttsConnectionString %>" ProviderName="<%$ ConnectionStrings:AttsConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [T_Atts] ORDER BY [Game]"></asp:SqlDataSource>

一旦我在所有适用的代码部分中将Game改为Atts,它就像一个魅力!

感谢所有花时间阅读我的问题,提交答案或回答其中许多类似问题的人,这些问题引导我解决问题!

答案 1 :(得分:0)

我会尝试在查询文本中使用您的参数名称:

sqlString = "Insert into T_Passing (Season, Game, Team, Player, Atts, Comps, Yards, TDs, INTs, Longest) Values (@Season,@Game,@Team,@Player,@Atts,@Comps,@Yards,@TDs,@INTs,@Longest)"