2 Replies - 557 Views - Last Post: 02 November 2016 - 03:27 AM Rate Topic: -----

#1 musty4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-November 16

How do i make an Update button to these codes

Posted 01 November 2016 - 07:11 PM

Hello Friends! I am new here in DreamInCode Forum
I have a problem concerning my Update button, I don't know where to start to make it update my SQL database, I use SSMS Microsoft to create my table Subjects and I also connected it with my VS 2010 ULTIMATE where I drag my table to LINQtoSQL and DATASET, I have created a Save button which successfully saves to my database and refreshes my datagridview.


Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If txtSubjectName.Text = "" Then
        ErrorProvider1.SetError(txtSubjectName, "Subject Name Cannot be Empty!")
        Exit Sub
    ElseIf txtSubSName.Text = "" Then
        ErrorProvider2.SetError(txtSubSName, "Subject Short Name Cannot be Empty!")
        Exit Sub
    End If
    Dim db As New EMSDataContext
    Dim SaveSubject = From C In db.Subjects
            Where C.SubjectName = txtSubjectName.Text
            Select C

    If SaveSubject.Count <> 0 Then
        MsgBox("Subject already exits!!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alart!!")
        Exit Sub
    Else
        Dim SaveNSubject As New Subject With {.SubjectName = txtSubjectName.Text, .ShortName = txtSubSName.Text}
        db.Subjects.Insertonsubmit(SaveNSubject)
        db.SubmitChanges()
        MsgBox("Subject added successfully!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Information")
    End If
    Registration_Load(sender, e)
    txtSubjectName.Text = ""
    txtSubSName.Text = ""
End Sub


And I also have a Delete button which successfully deletes my data from database and refreshes my datagridview.

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim A As New EMSDataContext
    Dim B = From C In A.Subjects
            Where C.ID = Val(DtgvSubject.CurrentRow.Cells(0).Value)
            Select C
    Try
        A.Subjects.Deleteonsubmit(B.FirstOrDefault)
        If MsgBox("Are You Sure to Delete This Record?", MsgBoxStyle.Question + vbYesNo, "Question") = MsgBoxResult.Yes Then
            A.SubmitChanges()
            MsgBox("Record Deleted Successfully!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Information")
            txtSubjectName.Text = ""
            txtSubSName.Text = ""
            Registration_Load(sender, e)
        Else
            txtSubjectName.Text = ""
            txtSubSName.Text = ""
            Registration_Load(sender, e)
        End If
    Catch ex As Exception
        MsgBox("Select a Record First!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!")
    End Try

    End If

End Sub


I also created a double click Events of the datagridview cell where I get cells values to my Subject Name textbox and Subject Short Name textbox if I double clicked which is also successfull.

Private Sub DtgvSubject_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DtgvSubject.CellDoubleClick
    Try
        Dim i As Integer
        i = DtgvSubject.CurrentRow.Index
        Me.txtSubjectName.Text = DtgvSubject.Item(1, i).Value
        Me.txtSubSName.Text = DtgvSubject.Item(2, i).Value
    Catch ex As Exception
        MsgBox("No Values in the Cells!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alert!!!")
        Button17_Click(sender, e)
    End Try
End Sub


My Question is, how can I create an Update button to my Database after getting the Values to the Textboxes and update them, Submit Changes and refresh my datagridview?
Thank you in Advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How do i make an Update button to these codes

#2 maceysoftware  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 348
  • View blog
  • Posts: 1,493
  • Joined: 07-September 13

Re: How do i make an Update button to these codes

Posted 02 November 2016 - 01:37 AM

Depends on how EMSDataContext is implemented.

However looking at it at face value without knowing what these classes look like it would just be calling the db.submitchanges, this routine to me should then handle what objects are new and need inserting, what objects are dirty and need updating and what objects are marked for deletion and delete them.

This may or may not be the case however like i said we would need to know more information about the classes your using, for example are they built using a third party framework or your own built framework?

How does the submitchanges work? whats the logic behind this?
Was This Post Helpful? 0
  • +
  • -

#3 musty4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-November 16

Re: How do i make an Update button to these codes

Posted 02 November 2016 - 03:27 AM

View Postmaceysoftware, on 02 November 2016 - 01:37 AM, said:

Depends on how EMSDataContext is implemented.

However looking at it at face value without knowing what these classes look like it would just be calling the db.submitchanges, this routine to me should then handle what objects are new and need inserting, what objects are dirty and need updating and what objects are marked for deletion and delete them.

This may or may not be the case however like i said we would need to know more information about the classes your using, for example are they built using a third party framework or your own built framework?

How does the submitchanges work? whats the logic behind this?

It is just a windows form Class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1