如果 Dropdownlist SelectedIndex = 0 则不在 SQL 数据库中添加任何行

if Dropdownlist SelectedIndex = 0 then not add any row in SQL Database

我的逻辑是,如果 ddlCoffee.SelectedIndex = 0,它不会在我的数据库中添加任何行。我确实尝试过 sqlcmd.Parameters.AddWithValue("@Qty", ((object)ddlCoffee.SelectedIndex) ?? DBNull.Value); 但在 SQL它作为 Qty 0 值行出现在 SQL 数据库中,我不想要。如果 Qty 为 0,我不想添加任何行。请告知我应该怎么做?

.aspx.cs

      protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        string connString = "Data Source";
        string insertCommand = "INSERT INTO tbDrinks ( DrinkName, DateOfOrder, Qty, UserName, UserCompany) " +
            "values(@DrinkName, @DateOfOrder, @Qty, @UserName, @UserCompany)";

        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand sqlcmd = new SqlCommand(insertCommand, conn))
            {

                sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
                sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);

                if (ddlCoffee.SelectedIndex != 0)
                {
                    sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
                }
                else
                {
                    //sqlcmd.Parameters.AddWithValue("@Qty", ((object)ddlCoffee.SelectedIndex) ?? DBNull.Value);
                    //Will Not Add in SQL Database
                }

                sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
                sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
                sqlcmd.ExecuteNonQuery();
              }
             }
           }

.aspx

<table id="tbHotDrinks" class="auto-style3" border="1" bordercolor="#1FC3F3" runat="server">

    <tr>
        <td>
            <asp:Label ID="lblCoffee" runat="server" Text="Coffee"></asp:Label>
        </td>
        <td class="auto-style2">
            <asp:Image ID="coffee" runat="server" Height="76px" ImageUrl="~/images/coffee.gif" Width="99px" ImageAlign="TextTop" />
        </td>

        <td class="auto-style11">
            <asp:DropDownList ID="ddlCoffee" runat="server">
               <asp:ListItem Value="0">--Select Qty--</asp:ListItem>
               <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
                <asp:ListItem>5</asp:ListItem>
                <asp:ListItem>6</asp:ListItem>
                <asp:ListItem>7</asp:ListItem>
                <asp:ListItem>8</asp:ListItem>
                <asp:ListItem>9</asp:ListItem>                        
            </asp:DropDownList>

        </td>

    </tr>
</table>

您设置的唯一条件是

  if (ddlCoffee.SelectedIndex != 0)
  {
       sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
  }

没有什么能阻止这条线的执行

 sqlcmd.ExecuteNonQuery();

您需要将整个事件包装在条件中,以便永远不会执行 sqlcmd,例如:

 using (SqlConnection conn = new SqlConnection(connString))
 {
      if (ddlCoffee.SelectedIndex != 0)
      {
           conn.Open();
           using (SqlCommand sqlcmd = new SqlCommand(insertCommand, conn))
           {
                sqlcmd.Parameters.AddWithValue("@DrinkName", lblCoffee.Text);
                sqlcmd.Parameters.AddWithValue("@DateOfOrder", DateTime.Today);

                sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);

                sqlcmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
                sqlcmd.Parameters.AddWithValue("@UserCompany", txtCompanyName.Text);
                sqlcmd.ExecuteNonQuery();
           }
      }
 }

请注意,处理此问题的更好方法是进行一些客户端验证,以便在未选择数量的情况下永远不会首先提交表单。

我认为您不需要考虑 SelectedIndex,而是 SelectedValue。试试下面的代码希望它能解决你的问题

if (ddlCoffee.SelectedValue != 0)
{
     sqlcmd.Parameters.AddWithValue("@Qty", ddlCoffee.SelectedValue);
}