2016-11-01 23 views
0

私の更新ボタンに関する問題があります。SQLデータベースの更新を開始する場所がわからず、SSMS Microsoftを使用してテーブルの科目を作成し、VS 2010のULTIMATEテーブルをLINQtoSQLとDATASETに変換するために、私は正常に私のデータベースに保存し、私の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] 

また、データベースからデータを正常に削除し、データグリッドビューを更新する[削除]ボタンもあります。

[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 
    If B.Count = 0 Then 
     MsgBox("Please select list to delete!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alart!!") 

    End If 

End Sub] 

私も、私も成功しているこれをダブルクリック場合、私は私のサブジェクト名]テキストボックスと件名短縮名テキストボックスにセルの値を取得DataGridViewのセルのダブルクリックイベントを作成しました。

[ 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] 

私の質問は、私はテキストボックスに値を取得した後、私のデータベースへの更新ボタンを作成し、それらを更新することができますどのように、変更の送信、私のDataGridViewを更新していますか?

+0

を私に答えてください、私は私のポストが理解できることを望みます。 –

答えて

0

入力がデータセットにバインドされていないと仮定すると、データベースから既存のレコードを取得し、レコードを更新してもう一度保存するだけで済みます。このような何か作業をする必要があります:

public Function updaterecord (field1 as integer, field2 as text) as Object 
Dim db As New EMSDataContext 
Dim SaveSubject = From C In db.Subjects 
     Where C.SubjectName = txtSubjectName.Text 
     Select C 
if SaveSubject is not Nothing then 
    C.id = field1 
    C.name = field2 'etc etc 
End if 
db.SaveChanges() 
return 
From C In db.Subjects 
     Where C.SubjectName = txtSubjectName.Text 
     Select C 
    End Function 
+0

ありがとうございます。ハム私はそれを試してみます –

+0

ハームは動作しません。その他の助けてください? –

+0

どのようなエラーが表示されますか? –

0

は私がLinqtoSQL

[ Private Sub bnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnUpdate.Click 
    If txtClassTeacher.Text = "" Or txtClassName.Text = "" Or txtClassSName.Text = "" Or cboClassSection.Text = "" Then 
     MsgBox("Select the data you want Update Please!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!") 
     Exit Sub 
    End If 
    Dim db As New EMSDataContext 
    Dim SaveClasses = From c In db.Classtbls 
         Where c.ID = Val(DtgvClasstbl.CurrentRow.Cells(0).Value) 
         Select c 

    For Each c As Classtbl In SaveClasses 
     c.Class_Teacher = txtClassTeacher.Text 
     c.Class_Name = txtClassName.Text 
     c.Short_Name = txtClassSName.Text 
     c.Section = cboClassSection.Text 
     'Inserting additional changes 
    Next 
    'Submitting changes to the Database 
    Try 
     If MsgBox("Are You Sure You Want Update This Record?", MsgBoxStyle.Question + vbYesNo, "Question") = MsgBoxResult.Yes Then 
      db.SubmitChanges() 
      MsgBox("Data Successfully Updated!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Success!") 
      Registration_Load(sender, e) 
      txtClassTeacher.Text = "" 
      txtClassName.Text = "" 
      txtClassSName.Text = "" 
      cboClassSection.Text = "" 
     Else 
      Registration_Load(sender, e) 
      txtClassTeacher.Text = "" 
      txtClassName.Text = "" 
      txtClassSName.Text = "" 
      cboClassSection.Text = "" 
     End If 
    Catch ex As Exception 
     MsgBox("Update not Successful!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!") 
     'Making(Adudjstiment) 
     db.SubmitChanges() 
    End Try 
End Sub] 

を使用して問題を解決してきたあなたハイムありがとう指導のためにあなたのStackOverflowとMSDNをありがとう

関連する問題