Results 1 to 21 of 21

Thread: [RESOLVED] How to add mathematic operation to update database values!?

  1. #1

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Resolved [RESOLVED] How to add mathematic operation to update database values!?

    Hi…


    I need help to update the item QTY after performing search criteria by adding and subtracting new values which they will be saved to the database after the updating!

    Code:
    Imports System.Data
    Imports System.Data.OleDb
    
    
    Public Class Form2
        Dim frm As New Form1
        Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
        Dim Conn As New OleDbConnection(ConStr)
        Dim DataSet1 As New DataSet
        Dim SQLstr As String
        Dim m As String
    
    
        Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    
            DataSet1 = New DataSet
    
            Static m As Integer = 0
            If tbSearch.Text = Trim("") Then Exit Sub
            ExactSearch()
            Conn.Open()
            Dim DataAdapter1 As New OleDbDataAdapter(SQLstr, Conn)
            DataAdapter1.Fill(DataSet1, "store")
    
            Conn.Close()
            tbSearchResulItemPartNumber.DataBindings.Clear()
            tbSearchResultItemDeascriptions.DataBindings.Clear()
            tbSearchResultItemLocation.DataBindings.Clear()
            tbItemQuantitySearchResult.DataBindings.Clear()
    
            tbSearchResulItemPartNumber.DataBindings.Add("Text", DataSet1, "store.Item Part Number")
            tbSearchResultItemDeascriptions.DataBindings.Add("Text", DataSet1, "store.Item Descriptions")
            tbSearchResultItemLocation.DataBindings.Add("Text", DataSet1, "store.Item Location")
            tbItemQuantitySearchResult.DataBindings.Add("Text", DataSet1, "store.Item Quantity")
    
            DataGridView1.DataSource = DataSet1
            DataGridView1.DataMember = "store"
    
            If Me.BindingContext(DataSet1, "store").Count = 0 Then
                MsgBox("No Result??!! ")
                Form1.Show()
                Me.Close()
            End If
    
            Exit Sub
    
        End Sub
    
        Public Sub ExactSearch()
            SQLstr = "SELECT * FROM store WHERE [Item Part Number ]= '" & tbSearch.Text & "'"
        End Sub
    
        
        Private Sub btnBACK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBACK.Click
            Me.Close()
            Form1.Show()
        End Sub
    
       
    
        Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Conn.Open()
            Try
                Dim cmd As OleDbCommand = New OleDbCommand("Select [Item Part Number] from store", Me.Conn)
                Dim reader As OleDbDataReader = cmd.ExecuteReader()
                Dim list As New AutoCompleteStringCollection
                While reader.Read
                    list.Add(reader.Item("Item Part Number"))
                End While
                tbSEARCH.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                tbSEARCH.AutoCompleteSource = AutoCompleteSource.CustomSource
                tbSEARCH.AutoCompleteCustomSource = list
    
            Catch ex As Exception
                Conn.Close()
                Throw ex
    
            End Try
            Conn.Close()
        End Sub
    
        Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbItemQuantityCalculation.KeyPress
            If (Char.IsControl(e.KeyChar) = False) Then
                If (Char.IsDigit(e.KeyChar)) Then
    
                Else
                    e.Handled = True
                    MsgBox("Sorry Only Digits Allowed!!", MsgBoxStyle.Information, "Store Mangement")
                    tbItemQuantityCalculation.Focus()
    
                End If
            End If
        End Sub
    
        
        Private Sub btnSUBTRACT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUBTRACT.Click
    
        End Sub
    
        Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
    
        End Sub
    End Class

    Any idea about the codes for adding and subtracting

    Regards…
    Last edited by HOTFIX; Jan 9th, 2009 at 08:13 AM.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    I would just write an Update query and perform

    vb.net Code:
    1. Dim x As Integer
    2.  
    3. If Not Integer.TryParse(Me.TextBox1.Text,x)
    4.     MessageBox("Only Integer values Here")
    5.     TextBox1.SelectAll
    6.     TextBox1.Focus
    7.     Exit Sub
    8. End If
    9. Dim strUPD as String
    10. strUPD = "Update TableName Set "
    11. strUPD &= "FieldName = FieldName + " & x.ToString()
    12. strUPD &= "Where ItemId = " & TheItemID
    13. Open the connection
    14. Create a command Object
    15. Execute the SQL on the command Object
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    I would just write an Update query and perform

    vb.net Code:
    1. Dim x As Integer
    2.  
    3. If Not Integer.TryParse(Me.TextBox1.Text,x)
    4.     MessageBox("Only Integer values Here")
    5.     TextBox1.SelectAll
    6.     TextBox1.Focus
    7.     Exit Sub
    8. End If
    9. Dim strUPD as String
    10. strUPD = "Update TableName Set "
    11. strUPD &= "FieldName = FieldName + " & x.ToString()
    12. strUPD &= "Where ItemId = " & TheItemID
    13. Open the connection
    14. Create a command Object
    15. Execute the SQL on the command Object

    Thanks GaryMazzone for your support

    In which event I put your suggested codes

    Regards...

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    If you have buttons to add/subtract put it there
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    I put the codes in the events but still getting many errors

    Code:
    Private Sub btnSUBTRACT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUBTRACT.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] - " & x.ToString()
            strUPD &= "Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
            Open the connection        
            Create a command Object
            Execute the SQL on the command Object
        End Sub
    
        Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= "Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
            Open the connection
            Create a command Object
            Execute the SQL on the command Object
        End Sub
    any idea
    Last edited by HOTFIX; Dec 15th, 2008 at 01:17 PM.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    That is example code only use your connection string. Create a command object then execute on it.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    That is example code only use your connection string. Create a command object then execute on it.
    is this what you mean:
    Code:
    Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= "Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
            Dim Conn As New OleDbConnection(ConStr)
            Dim DataSet1 As New DataSet
        End Sub

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    There is no need to create a dataset. You are not going to return anything from the database just do an update. So the first 2 lines are correcrt. Next create a OleDbCommand object. Set the command objects connection property to your connection then use the command objects ExecuteNonQuery method to execute the SQL string.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    i try this code

    Code:
    rivate Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= "Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
            Dim Conn As New OleDbConnection(ConStr)
            Dim SavInto As New OleDb.OleDbCommand
            SavInto.Connection = Conn
            SavInto.CommandType = CommandType.Text
            SavInto.CommandText = "UPDATE store SET [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"        Conn.Open()
            SavInto.ExecuteNonQuery()
            Conn.Close()
        End Sub
    but its return this error:
    "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    What are you trying to do.... You are trying to set every Item Part Number to the same thing. If that is the PK for the table of course it will fail
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    What are you trying to do.... You are trying to set every Item Part Number to the same thing. If that is the PK for the table of course it will fail


    sorry I dont know how to manage this code

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    SavInto.CommandText = strUPD
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    SavInto.CommandText = strUPD

    Thanks GaryMazzone for your Continuous support...

    Code:
    Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= "Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
            Dim Conn As New OleDbConnection(ConStr)
            Dim SavInto As New OleDb.OleDbCommand
            SavInto.Connection = Conn
            SavInto.CommandType = CommandType.Text
            SavInto.CommandText = strUPD
            Conn.Open()
            SavInto.ExecuteNonQuery()
            Conn.Close()
        End Sub
    still getting error this time on query expression :"Syntax error (missing operator) in query expression '[Item Quantity] + 3Where [Item Part Number] = 'AF090-111-T22''

    Any idea please

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    space betwee 3 and Where
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Finally some success

    The codes now are working but when clicking on add button the updated value doesn’t appear in the item quantity textbox until a I do another search neither in the grid view

    Any idea how to fix this

    Regards...

  16. #16
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    Either call a refresh of the the gridview or update the column when you get successful update of the database.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  17. #17

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    Either call a refresh of the the gridview or update the column when you get successful update of the database.
    I try this codes but they are not working

    Code:
     Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= " Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
            Dim Conn As New OleDbConnection(ConStr)
            Dim SavInto As New OleDb.OleDbCommand
            SavInto.Connection = Conn
            SavInto.CommandType = CommandType.Text
            SavInto.CommandText = strUPD
            Conn.Open()
            SavInto.ExecuteNonQuery()
            Conn.Close()
    
            tbItemQuantitySearchResult.Refresh()
            DataGridView1.DataSource = DataSet1.GetChanges
            DataGridView1.DataMember = "store"
        End Sub

  18. #18
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    vb.net Code:
    1. Dim intNumEfft As Integer = 0
    2. intNumEff = SavInto.ExecuteNonQuery()
    3. Conn.Close()
    4.  
    5. If intNumEff > 0 Then
    6.   tbItemQuantitySearchResult += tbItemQuantityCalculation
    7. End If
    then call the part that fills the grid again
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  19. #19

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Quote Originally Posted by GaryMazzone
    vb.net Code:
    1. Dim intNumEfft As Integer = 0
    2. intNumEff = SavInto.ExecuteNonQuery()
    3. Conn.Close()
    4.  
    5. If intNumEff > 0 Then
    6.   tbItemQuantitySearchResult += tbItemQuantityCalculation
    7. End If
    then call the part that fills the grid again
    I've try it, it doesn't work

    Code:
    Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
            Dim x As Integer
            If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
                MessageBox.Show("Only Integer values Here")
                tbItemQuantityCalculation.SelectAll()
                tbItemQuantityCalculation.Focus()
                Exit Sub
            End If
            Dim strUPD As String
            strUPD = "Update store set"
            strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
            strUPD &= " Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    
            Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
            Dim Conn As New OleDbConnection(ConStr)
            Dim SavInto As New OleDb.OleDbCommand
            SavInto.Connection = Conn
            SavInto.CommandType = CommandType.Text
            SavInto.CommandText = strUPD
            Conn.Open()
            SavInto.ExecuteNonQuery()
            Conn.Close()
            Dim intNumEfft As Integer = 0
            intNumEfft = SavInto.ExecuteNonQuery()
            Conn.Close()
            If intNumEfft > 0 Then
                tbItemQuantitySearchResult.Text += tbItemQuantityCalculation.Text
            End If
            DataGridView1.DataSource = DataSet1
            DataGridView1.DataMember = "store"
        End Sub

  20. #20
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to add mathematic operation to update database values!?

    vb.net Code:
    1. Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
    2.         Dim x As Integer
    3.         If Not Integer.TryParse(Me.tbItemQuantityCalculation.Text, x) Then
    4.             MessageBox.Show("Only Integer values Here")
    5.             tbItemQuantityCalculation.SelectAll()
    6.             tbItemQuantityCalculation.Focus()
    7.             Exit Sub
    8.         End If
    9.         Dim strUPD As String
    10.         strUPD = "Update store set"
    11.         strUPD &= "[Item Quantity] = [Item Quantity] + " & x.ToString()
    12.         strUPD &= " Where [Item Part Number] = '" & tbSearchResulItemPartNumber.Text & "'"
    13.  
    14.         Dim ConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source =" & Application.StartupPath & "\data.mdb"
    15.         Dim Conn As New OleDbConnection(ConStr)
    16.         Dim SavInto As New OleDb.OleDbCommand
    17.         SavInto.Connection = Conn
    18.         SavInto.CommandType = CommandType.Text
    19.         SavInto.CommandText = strUPD
    20.         Conn.Open()
    21.         Dim intNumEfft As Integer = 0
    22.         intNumEfft = SavInto.ExecuteNonQuery()
    23.         Conn.Close()
    24.         If intNumEfft > 0 Then
    25.             tbItemQuantitySearchResult.Text += tbItemQuantityCalculation.Text
    26.         End If
    27.         DataGridView1.DataSource = DataSet1
    28.         DataGridView1.DataMember = "store"
    29.     End Sub
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  21. #21

    Thread Starter
    Lively Member HOTFIX's Avatar
    Join Date
    Sep 2008
    Posts
    91

    Re: How to add mathematic operation to update database values!?

    Many thanks GaryMazzone for your assistance and your time for helping

    Unfortunately, the last code for displaying updated values in the textbox result and grid view didn’t work as I expected…


    Anyway I will mark this thread as resolved

    Again I appreciate your support and I hope you keep up helping sprit with me and other members…

    Best Regards…

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width