Search code examples
c#asp.netwebformsdropdownlistitem

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


my logic is that if ddlCoffee.SelectedIndex = 0 it will not add any row in my database. I did tried sqlcmd.Parameters.AddWithValue("@Qty", ((object)ddlCoffee.SelectedIndex) ?? DBNull.Value); but in SQL it come out as Qty 0 value row in SQL database which I do not want. I do not want to add any row if Qty is 0. Kindly advise how should I do?

.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>

Solution

  • The only conditional you have set is

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

    Nothing is stopping this line from executing

     sqlcmd.ExecuteNonQuery();
    

    You need to wrap the whole event in a conditional so that the sqlcmd is never executed e.g.:

     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();
               }
          }
     }
    

    Note, a better way to handle this is to have some client side validation so that the form is never submitted in the first place if the quantity isn't chosen.