0

我有三个从 sql 数据库(一列)中检索信息的下拉菜单。When a user select WorkShopName from the first drop down, the second drop down shows available dates for it, and when the date is selected the user can choose the location from the third drop down. 我正在尝试将下拉列表中的选定信息插入到 sql 数据库中的另一列。当我单击按钮注册时,我有“从字符串转换日期和/或时间时转换失败”。我尝试插入 DateTime.Now 而不是 Date Drop Down 的 Selected Value 但错误是相同的。有人可以帮忙吗?谢谢

以下是下拉菜单:

<asp:Label runat="server" 
            Text="Please select work shop you would like to register for:"></asp:Label>
    </td>
    <td>
        <asp:DropDownList ID="ddlWorkShopName"  AutoPostBack="true"  runat="server" 
            DataSourceID="workshopName" DataTextField="WorkshopName" 
            DataValueField="WorkshopName">
        </asp:DropDownList>

        <asp:SqlDataSource ID="workshopName" runat="server" 
            ConnectionString="<%$ ConnectionStrings:WildLife_EducationConnectionString %>" 
            SelectCommand="SELECT DISTINCT [WorkshopName] FROM [tblWorkshop]">
        </asp:SqlDataSource>

    </td>
</tr>
<tr>
    <td>
        <asp:Label ID = "lblChoseWDate" runat="server" Text="Please chose workshop date:"></asp:Label>
    </td>
    <td>
        <asp:DropDownList ID="ddlChoseWDate" DataTextFormatString="{0:yyyy-MM-dd}" 
            AutoPostBack="True" runat="server" DataSourceID="WorkshopDate2" 
            DataTextField="WorkshopBeginingDate" DataValueField="WorkshopBeginingDate">
        </asp:DropDownList>

        <asp:SqlDataSource ID="WorkshopDate2" runat="server"
            ConnectionString="<%$ ConnectionStrings:WildLife_EducationConnectionString %>" 

            SelectCommand="SELECT DISTINCT ([WorkshopBeginingDate]) FROM [tblWorkshop] WHERE ([WorkshopName] = @WorkshopName)">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlWorkShopName" Name="WorkshopName" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

    </td>
</tr>
<tr>
    <td>
        <asp:Label ID="lblChoseWLoc" runat="server" Text="Please chose workshop location:"></asp:Label>
    </td>
    <td>
        <asp:DropDownList ID="ddlChoseWorkShopLoc" runat="server" 
            DataSourceID="WorkshopLocation2" DataTextField="WorkshopLocation" 
            DataValueField="WorkshopLocation">
        </asp:DropDownList>
        <asp:SqlDataSource ID="WorkshopLocation2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:WildLife_EducationConnectionString %>" 
            SelectCommand="SELECT [WorkshopLocation] FROM [tblWorkshop] WHERE (([WorkshopBeginingDate] = @WorkshopBeginingDate) AND ([WorkshopName] = @WorkshopName))">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlChoseWDate" Type="String" 
                    Name="WorkshopBeginingDate" PropertyName="SelectedValue" />
                <asp:ControlParameter ControlID="ddlWorkShopName" Name="WorkshopName" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </td>
</tr>

<td>
    <asp:Button ID="btnRegister" runat="server" onclick="btnRegister_Click" 
        Text="Register" />
</td>

按钮寄存器的代码后面:

SqlParameter workshopDate = new SqlParameter("@WorkshopDate", SqlDbType.DateTime);
    workshopDate.Value = ddlChoseWDate.SelectedValue;

using (SqlConnection conn2 = new SqlConnection(@"Data Source=CIS489_3\WILDLIFE;Initial Catalog=WildLife_Education;Integrated Security=True;"))
{
    SqlCommand CmdSql2 = new SqlCommand
    ("INSERT INTO [tblUserWorkshop] ([UserID],[WorkshopName],[WorkshopDate],[WorkshopLocation]) VALUES (@WorkshopName, @WorkshopDate, @WorkshopLocation, @UserID)", conn2);
    conn2.Open();

    CmdSql2.Parameters.AddWithValue("@UserID", userId.ToString());
    CmdSql2.Parameters.AddWithValue("@WorkshopName", ddlWorkShopName.SelectedValue);
    CmdSql2.Parameters.AddWithValue("@WorkshopDate",workshopDate.Value);
    CmdSql2.Parameters.AddWithValue("@WorkshopLocation", ddlChoseWorkShopLoc.SelectedValue);
CmdSql2.ExecuteNonQuery();
conn2.Close();
4

2 回答 2

3

我很确定这必须按照给定的确切顺序。

错误的:

("INSERT INTO [tblUserWorkshop] ([UserID],[WorkshopName],[WorkshopDate],[WorkshopLocation]) VALUES (@WorkshopName, @WorkshopDate, @WorkshopLocation, @UserID)", conn2);

正确的:

("INSERT INTO [tblUserWorkshop] ([UserID],[WorkshopName],[WorkshopDate],[WorkshopLocation]) VALUES (@UserID, @WorkshopName, @WorkshopDate, @WorkshopLocation )", conn2);

最后有用户 ID,但应该在前面。

几乎您应该将插入和值指定为相同的顺序。您当前正在尝试将 WorkShopLocation 转换为 DateTime。

于 2013-04-16T21:03:48.053 回答
0

尝试这个:

CmdSql2.Parameters.Add("@WorkshopDate", SqlDbType.VarChar).Value = workshopDate.Text;
于 2013-04-16T21:01:39.510 回答