locked
Problem adding texbox values together RRS feed

  • Question

  • User1702728255 posted

    I have a form that is opened from click on a link from a Gridview. This form has a formview on it bound to a datasource. Shows Item detail. Formview1 is set to DefaultMode=Edit.

    I then added and unbound texbox and a button. What I wanted to do was to let someone type a number in the textbox and then be able to click on the button and have the bound field and the unbound field add together and be returned as the new bound field textbox value to update to the database.

    I tried the following, but keeping getting and error assigning the Text Value. The QInS.Text always says Text is not a member of Integer which I understand but I cannot seem to convert it to a usable state. I am fairly new at ASP.NET 2 and VB.NET so I am sure I am missing something simple.

    Imports System.Web.UI.Page

    Imports System.Web.UI.WebControls

    Imports System.Web.UI.WebControls.FormViewRow

    Partial Class edithardware

    Protected Sub AddBtn_Click(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs)

    Dim OrgQty As Integer

    Dim AddQty As Integer

    Dim totalStock As Integer

    Dim QInS As Integer

    OrgQty = CType(FormView1.Row.Cells(0).FindControl("QInS"), TextBox).Text

    AddQty = CType(FormView1.Row.Cells(1).FindControl("AddQty"), TextBox).Text

    'warehousearea = DirectCast(FormView1.Row.Cells(2).FindControl("Warehouse_AreaTextBox"), TextBox).Text

    totalStock = Integer.Parse(OrgQty) + Integer.Parse(AddQty)

    QInS = totalStock.ToString()

    QInS.Text = totalStock.ToString

    End Sub

    End Class

    Tuesday, June 3, 2008 2:48 PM

Answers

  • User1702728255 posted

    I was never able to get the value to display back on the form so I had to re-think and have gotten it working using SQL commands instead of trying to use VB.

     

    Thanks for all the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2008 2:17 PM

All replies

  • User1564875471 posted

    I tried the following, but keeping getting and error assigning the Text Value. The QInS.Text always says Text is not a member of Integer

    yes , its an integer variable and not a textbox ! you need to choose which textbox to assign the value to it ...

     

    Tuesday, June 3, 2008 3:08 PM
  • User1702728255 posted

    The QIns is the name of the databound Textbox but how can I convert properly so that the QIns.Text equals the new value

    Tuesday, June 3, 2008 3:23 PM
  • User1564875471 posted

    try this

    totalStock = Integer.Parse(OrgQty) + Integer.Parse(AddQty)

    QInS = totalStock.ToString()

    CType(FormView1.Row.Cells(1).FindControl("QInS"), TextBox).Text=QInS.ToString()

     

    if you still have problems , post the formview declaration code ( aspx)

     

     

    Tuesday, June 3, 2008 3:28 PM
  • User1702728255 posted

    Thought that code worked because there are no longer errors showing, but the buton is not working. The value is not being updated. I am including the aspx code for the form and formview control. ANy help would be greatly appreciated.

    <form id="form1" runat="server">

    <div>

     

    <table align="center" cellpadding="2" cellspacing="0" class="style1">

    <tr>

    <td align="center" valign="top" width="620">

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="RecID"

    DataSourceID="SqlDataSource1" DefaultMode="Edit">

    <EditItemTemplate>

    <table align="center" cellpadding="2" cellspacing="0" class="style2">

    <tr>

    <td align="left" width="150">

    RecID:</td>

    <td align="left" width="400">

    <asp:Label ID="RecIDLabel1" runat="server" Text='<%# Eval("RecID") %>' />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    ItemName:</td>

    <td align="left" width="400">

    <asp:TextBox ID="ItemNameTextBox" runat="server"

    Text='<%# Bind("ItemName") %>' />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    ItemDesc:</td>

    <td align="left" width="400">

    <asp:TextBox ID="ItemDescTextBox" runat="server"

    Text='<%# Bind("ItemDesc") %>' />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    QtyInStock:</td>

    <td align="left" width="400">

    <asp:TextBox ID="QInS" runat="server" Text='<%# Bind("QtyInStock") %>' />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    Add</td>

    <td align="left" width="400">

    <asp:TextBox ID="AddQty" runat="server" AutoPostBack="True">0</asp:TextBox>

    &nbsp;

    <asp:Button ID="AddBtn" runat="server" Height="20px" Text="Add To Stock"

    Width="100px" />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    &nbsp;</td>

    <td align="left" width="400">

    <asp:TextBox ID="SuybQty" runat="server"></asp:TextBox>

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    ReOrderLevel:</td>

    <td align="left" width="400">

    <asp:TextBox ID="ReOrderLevelTextBox" runat="server"

    Text='<%# Bind("ReOrderLevel") %>' />

    </td>

    </tr>

    <tr>

    <td align="left" width="150">

    &nbsp;</td>

    <td align="left" width="400">

    &nbsp;</td>

    </tr>

    </table>

    <br />

    <br />

    &nbsp;<br />

    &nbsp;<br />

    &nbsp;<br />

    &nbsp;<br />

    &nbsp;<br />

    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"

    CommandName="Update" Text="Update" />

    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"

    CausesValidation="False" CommandName="Cancel" Text="Cancel" />

    </EditItemTemplate>

     

    </asp:FormView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HdwInvConnStr %>"

     

    SelectCommand="SELECT [RecID], [ItemName], [ItemDesc], [QtyInStock], [ReOrderLevel] FROM [PCPeripherals]"

    UpdateCommand="UPDATE [PCPeripherals] SET [ItemName] = @ItemName, [ItemDesc] = @ItemDesc, [QtyInStock] = @QtyInStock, [ReOrderLevel] = @ReOrderLevel WHERE [RecID] = @original_RecID AND [ItemName] = @original_ItemName AND [ItemDesc] = @original_ItemDesc AND [QtyInStock] = @original_QtyInStock AND [ReOrderLevel] = @original_ReOrderLevel"

    ConflictDetection="CompareAllValues"

    OldValuesParameterFormatString="original_{0}">

    <UpdateParameters>

    <asp:Parameter Name="ItemName" Type="String" />

    <asp:Parameter Name="ItemDesc" Type="String" />

    <asp:Parameter Name="QtyInStock" Type="Int32" />

    <asp:Parameter Name="ReOrderLevel" Type="Int32" />

    <asp:Parameter Name="original_RecID" Type="Int32" />

    <asp:Parameter Name="original_ItemName" Type="String" />

    <asp:Parameter Name="original_ItemDesc" Type="String" />

    <asp:Parameter Name="original_QtyInStock" Type="Int32" />

    <asp:Parameter Name="original_ReOrderLevel" Type="Int32" />

    </UpdateParameters>

    </asp:SqlDataSource>

    </td>

    </tr>

    </table>

     

    </div>

    </form>

    Wednesday, June 4, 2008 9:08 AM
  • User1564875471 posted

    Hi

    Change the Method to this :

        Protected Sub AddBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    
            ' quantity to add 
            Dim AddQty As Integer
            ' primary key for the record 
            Dim RecID As Integer = CInt(FormView1.DataKey.Value)
            AddQty = CType(FormView1.Row.FindControl("AddQty"), TextBox).Text
    
            '' call the update method , if the update happened , we bind the formview to reflect the changes 
            If UpdateQuantity(RecID, AddQty) Then
                FormView1.DataBind()
            End If
    
        End Sub

     And add this function to the code behind ,

        Private Function UpdateQuantity(ByVal RecID As Integer, ByVal AddQuantity As Integer) As Boolean
            Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("HdwInvConnStr").ConnectionString)
                Dim com As New SqlCommand("update PCPeripherals set QtyInStock =QtyInStock + @AddedQuantity where RecID = @RecID ")
                '' declare the paramters 
                com.Parameters.Add("@AddedQuantity", Data.SqlDbType.Int)
                com.Parameters.Add("@RecID", Data.SqlDbType.Int)
    
                ' set parameters values 
                com.Parameters("@AddedQuantity").Value = AddQuantity
                com.Parameters("@RecID").Value = RecID
                'assign the connection to the command
                com.Connection = con
    
                Dim updated As Boolean = False
                Try
                    con.Open()
                    '' ExecuteNonQuery will returns number of rows affected , so if its >0 then the update succeded 
                    updated = com.ExecuteNonQuery() > 0
                Catch ex As Exception
                    Throw
                End Try
    
                Return updated
            End Using
        End Function

     

    And Remove the "AutoPostPack" from the "AddQty" TextBox like this:

    <asp:TextBox ID="AddQty" runat="server"> 0</asp:TextBox>

     

    Saturday, June 7, 2008 3:36 PM
  • User1702728255 posted

    I was never able to get the value to display back on the form so I had to re-think and have gotten it working using SQL commands instead of trying to use VB.

     

    Thanks for all the help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2008 2:17 PM