1,105,380 Community Members

Help creating an Update Query in vb

Member Avatar
JustineAubrey
Newbie Poster
7 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I'm creating a form application using vb. I'm still new to this and I'm teaching myself via the internet so please don't be too mean. I wrote my code using queries that I created in visual studio. I don't quite remember how to create an update query and write the code to update different datagrids. I tried searching the forums, and I can't find anything that fits into the way I wrote my code. I have an update button that I want to use to save all the edited data in the form. Here's my code so far:

Public Class Form1

    Private Sub Blsys_systemsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.Blsys_systemsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.GsaisDataSet)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Removes Empty Row Displayed at bottom of DataGirdView
        Blsys_buildingcomponentsDataGridView.AllowUserToAddRows = False
        Blsys_itcomponentsDataGridView.AllowUserToAddRows = False
        Blsys_attachmentsDataGridView.AllowUserToAddRows = False
    End Sub

    Private Sub SystemComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SystemComboBox.SelectedIndexChanged
        'Clears location combobox when a new system is selected
        LocationComboBox.Items.Clear()
        LocationComboBox.Text = ""

        Dim System As String = SystemComboBox.Text
        Dim Row As gsaisDataSet.blsys_systemsRow
        Dim Table As gsaisDataSet.blsys_systemsDataTable

        'Selection of system queries system locations
        If System <> "" Then

            Table = Blsys_systemsTableAdapter.GetDataBySysName(System)
            For Each Row In Table
                LocationComboBox.Items.Add(Row.LocationName)
            Next
        End If
    End Sub

    Private Sub DeviceSearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeviceSearchButton.Click
        showformdata()
    End Sub
    Private Sub showformdata()
        Dim System As String = SystemComboBox.Text
        Dim BuildLoc As String = LocationComboBox.Text

        'Display error message if both comboboxes are empty
        If System = "" And BuildLoc = "" Then
            MessageBox.Show("Please Enter Search Criteria.")

            'Display error message if Location combobox is empty
        ElseIf System <> "" And BuildLoc = "" Then
            MessageBox.Show("Please Enter a System Location.")

            'Display error message if System combobox is empty
        ElseIf System = "" And BuildLoc <> "" Then
            MessageBox.Show("Please Enter a System.")

            'Fills IT Components and Building Components datagrids
        Else
            Dim ITRow As gsaisDataSet.blsys_itcomponentsRow
            Dim ITTable As gsaisDataSet.blsys_itcomponentsDataTable

            Blsys_itcomponentsTableAdapter.FillByITC(GsaisDataSet.blsys_itcomponents, System, BuildLoc)
            ITTable = Blsys_itcomponentsTableAdapter.GetDataByITC(System, BuildLoc)

            For Each ITRow In ITTable
                Dim dgvRow As New DataGridViewRow
                Dim dgvCell As DataGridViewCell

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ITRow.AssetID
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ITRow.SystemID
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ITRow.Comments
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ITRow.itcomponentid
                dgvRow.Cells.Add(dgvCell)
            Next

            Dim BSRow As gsaisDataSet.blsys_buildingcomponentsRow
            Dim BSTable As gsaisDataSet.blsys_buildingcomponentsDataTable

            Blsys_buildingcomponentsTableAdapter.FillByBuildC(GsaisDataSet.blsys_buildingcomponents, System)
            BSTable = Blsys_buildingcomponentsTableAdapter.GetDataByBuildC(System)

            For Each BSRow In BSTable
                Dim dgvRow As New DataGridViewRow
                Dim dgvCell As DataGridViewCell

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.SystemID
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.IPAddress
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.DevName
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.Description
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.LocationAddress
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.CategoryID
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.componentid
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = BSRow.Comments
                dgvRow.Cells.Add(dgvCell)
            Next

            Dim ARow As gsaisDataSet.blsys_attachmentsRow
            Dim ATable As gsaisDataSet.blsys_attachmentsDataTable

            Blsys_attachmentsTableAdapter.FillByAttach(GsaisDataSet.blsys_attachments, System)
            ATable = Blsys_attachmentsTableAdapter.GetDataByAttach(System)

            For Each ARow In ATable
                Dim dgvRow As New DataGridViewRow
                Dim dgvCell As DataGridViewCell

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ARow.Attachmentname
                dgvRow.Cells.Add(dgvCell)

                dgvCell = New DataGridViewTextBoxCell()
                dgvCell.Value = ARow.AttachmentDescrip
                dgvRow.Cells.Add(dgvCell)
            Next

        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Closes the form
        Me.Close()
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        'Clears the form
        SystemComboBox.Text = ""
        LocationComboBox.Text = ""
        LocationComboBox.Items.Clear()
        GsaisDataSet.Clear()
    End Sub

    Private Sub Blsys_attachmentsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Blsys_attachmentsDataGridView.CellContentClick
        If (Blsys_attachmentsDataGridView.Columns(e.ColumnIndex).Name = "AttachmentButton") Then
            MessageBox.Show(Me.Urlprefix.Text & Blsys_attachmentsDataGridView.Item(3, Blsys_attachmentsDataGridView.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub LocationComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LocationComboBox.SelectedIndexChanged
        showformdata()
    End Sub

    Private Sub UpdateInfo()

    End Sub

    Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        'I AM NOT SURE WHAT TO WRITE HERE :(
    End Sub

End Class

Tell me if you need more information. Thanks!

Member Avatar
M.Waqas Aslam
Practically a Posting Shark
837 posts since Aug 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

hello!
it takes long time to read your all code , but as far as concern about update query ,you can do like this

dim mycon as new sqlconnection("connection string")
dim cmd as new sqlcommand
mycon.open()
cmd.connection= mycon
cmd.commandtext="update tablename set dbcolumnname1=@Column1,dbcolumnname1=@Column2,dbcolumnname1=@Column3 where recID=@RecID"
cmd.parameters.addwithvalue("=@Column1",txt1.text)
cmd.parameters.addwithvalue("=@Column2",txt2.text)
cmd.parameters.addwithvalue("=@Column3",txt3.text)
cmd.parameters.addwithvalue("=@recID",txtRecID.text)

cmd.executenonquery()
mycon.close()

Hope this will helps you
Regards

Member Avatar
GAiXz
Newbie Poster
23 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i don't get it...

here's my example of ADD/EDITorUPDATE/DELETE...
http://incpinoyprogrammer.blogspot.com/

you can make a simple code for that...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: