locked
Inserting values into database when the values are calculated using if statements RRS feed

  • Question

  • User1277740678 posted

    Hi all,

    I declare a variable with an if statement like so

    if(bulkDDL.SelectedIndex != 0)

    { int bulkID = Convert.ToInt32(bulkDDL.SelectedItem.Value); }
    else

    { string bulkID = string.Empty; }

    But when I try to insert my values into an sql database I get the following error

    The name bulkID does not exist in the current context. 

    Are there any easy work arounds that don't use a case by case basis like

    if ()

    then bulk id = value

    ProductTestingTableAdapter productTestAdapter = new ProductTestingTableAdapter();

    productTestAdapter.Insert(productCategoryID, skuID, bulkID, state, FaultCategoryID, FaultDescriptionID, comments, startTime, endTime, user);

    else if

    {}

    else if{}

    ......

    Because then I'd have about ten else if statements, as several of my values are calculated with if statements.

    Preferably something simple that I can easily understand. Only started c# this Tuesday and a lot of the solutions I see her are too complicated for me to grasp. 

    Thanks in advance

    Sunday, July 2, 2017 6:19 PM

Answers

  • User-1716253493 posted

    Maybe you can pass nullable int to the adapter that allow null value

    https://msdn.microsoft.com/en-us/library/ms233762.aspx?f=255&MSPPError=-2147217396

    https://www.dotnetperls.com/nullable-int

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 2:55 AM
  • User-1838255255 posted

    Hi Kvetch,

    According to your description, as far as I know, you declare the bulkID inside if statement, so it is a local variable, so you can not use it out of if statement. So you could declare the bulkID as the global variable, then use it in insert command.

    Sample Code:

    <asp:DropDownList ID="bulkDDL" AutoPostBack="true" runat="server" OnSelectedIndexChanged="bulkDDL_SelectedIndexChanged">
                    <asp:ListItem>111</asp:ListItem>
                    <asp:ListItem>222</asp:ListItem>
                    <asp:ListItem>333</asp:ListItem>
    </asp:DropDownList>
    protected void bulkDDL_SelectedIndexChanged(object sender, EventArgs e) { int bulkID; if (bulkDDL.SelectedIndex != 0) { bulkID = Convert.ToInt32(bulkDDL.SelectedItem.Value); } else { bulkID = Convert.ToInt32(string.Empty); } Response.Write(bulkID);
    // write your insert command }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 3:06 AM

All replies

  • User-1716253493 posted

    Maybe you can pass nullable int to the adapter that allow null value

    https://msdn.microsoft.com/en-us/library/ms233762.aspx?f=255&MSPPError=-2147217396

    https://www.dotnetperls.com/nullable-int

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 2:55 AM
  • User-1838255255 posted

    Hi Kvetch,

    According to your description, as far as I know, you declare the bulkID inside if statement, so it is a local variable, so you can not use it out of if statement. So you could declare the bulkID as the global variable, then use it in insert command.

    Sample Code:

    <asp:DropDownList ID="bulkDDL" AutoPostBack="true" runat="server" OnSelectedIndexChanged="bulkDDL_SelectedIndexChanged">
                    <asp:ListItem>111</asp:ListItem>
                    <asp:ListItem>222</asp:ListItem>
                    <asp:ListItem>333</asp:ListItem>
    </asp:DropDownList>
    protected void bulkDDL_SelectedIndexChanged(object sender, EventArgs e) { int bulkID; if (bulkDDL.SelectedIndex != 0) { bulkID = Convert.ToInt32(bulkDDL.SelectedItem.Value); } else { bulkID = Convert.ToInt32(string.Empty); } Response.Write(bulkID);
    // write your insert command }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 3:06 AM
  • User1277740678 posted

    Haven't tried these out because I ended up having to do it on a case by case basis anyway due to personalised error messages for each case. 

    But I think a combination of both would work, because I actually wanted null, not string.empty. 

    Thought they were the same thing but they aren't. 

    Thanks to the both of you. 

    Monday, July 3, 2017 8:51 AM