Save, Edit, Delete Data using VB.Net and SQL Server 2000

Please support our VB.NET advertiser: Intel Parallel Studio Home
Jx_Man Jx_Man is offline Offline Mar 5th, 2008, 10:21 am |
0
This code to save, Edit and delete data in VB.Net using SQLServer as backend. this code is continuance from my previous post "Show Data in DataGrid with VB.Net 2003 and SQLServer 2000". so i didn't write code how to connect SQLServer 2000 with VB.Net 2003 cause this already in there.
Please see and read a comment on code carefully so this code can implement goods.
Quick reply to this message  
VB.NET Syntax
  1. ' Programmed By Jery M
  2. 'this Following code shows how to Save, Edit, Delete Data using VB.Net and SQL Server 2000 as database.
  3. 'this code needed some control :
  4. 'a database with 4 column (Id[primary key],FirstName,LastName,Age)
  5. '3 button (cmdSave,cmdEdit,cmdDelete)
  6. '4 text box (txtId,txtFirstName,txtLastName,txtAge).
  7. '1 datagrid (named dgStudent)
  8.  
  9. ' This Proceduere to refresh form and refresh data in datagrid (always show the newest data)
  10. Private Sub Refresh_Form()
  11. Dim conn As SqlConnection
  12. Dim cmdStudent As New SqlCommand
  13. Dim daStudent As New SqlDataAdapter
  14. Dim dsStudent As New DataSet
  15. Dim dtStudent As New DataTable
  16.  
  17. 'clear all textbox
  18. txtId.Text = ""
  19. txtFirstName.Text = ""
  20. txtLastName.Text = ""
  21. txtAge.Text = ""
  22.  
  23. 'this part to call data from database and show in datagrid
  24. conn = GetConnect()
  25. Try
  26. cmdStudent = conn.CreateCommand
  27. cmdStudent.CommandText = "SELECT * FROM Student"
  28. daStudent.SelectCommand = cmdStudent
  29. daStudent.Fill(dsStudent, "Student")
  30. dgStudent.DataSource = dsStudent
  31. dgStudent.DataMember = "Student"
  32. dgStudent.ReadOnly = True
  33. Catch ex As Exception
  34. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
  35. End Try
  36. End Sub
  37.  
  38. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  39. Dim check As Integer
  40. Dim conn As SqlConnection
  41. Dim cmdStudent As New SqlCommand
  42. Dim cmdStudent1 As New SqlCommand
  43. Dim daStudent As New SqlDataAdapter
  44. Dim dsStudent As New DataSet
  45. Dim dtStudent As New DataTable
  46.  
  47. If txtId.text = "" Or txtFirstName.Text = "" txtLastName.Text = "" Or txtAge.Text = "" Then
  48. MsgBox("Student Data is not completed", MsgBoxStyle.OKOnly)
  49. Else
  50. If MsgBox("Are you sure to save Student data with Id : " & txtId.text & " ?", MsgBoxStyle.OKCancel, "Input confirm") = MsgBoxResult.Cancel Then
  51. ' do nothing
  52. Else
  53. Try
  54. conn = GetConnect()
  55. conn.Open()
  56. cmdStudent = conn.CreateCommand
  57. cmdStudent.CommandText = "SELECT * FROM Student WHERE Id='" & Trim(txtId.text) & " ' "
  58. daStudent.SelectCommand = cmdStudent
  59. daStudent.Fill(dsStudent, "Student")
  60. dtStudent = dsStudent.Tables("Student")
  61.  
  62. If (dtStudent.Rows.Count > 0) Then
  63. MsgBox("Student dengan Id " & Trim(cmbId.Text) & " already in database", MsgBoxStyle.OKOnly, "Message :")
  64. Else
  65.  
  66. cmdStudent1 = conn.CreateCommand
  67. cmdStudent1.CommandText = "INSERT INTO Student(Id, FirstName, LastName,Age) VALUES('" & Trim(txtId.text) & "','" & Trim(txtFirstName.Text) & "','" & Trim(txtLastName.Text) & "','" & Trim(txtAge.Text) & "')"
  68. check = cmdStudent1.ExecuteReader.RecordsAffected()
  69. If check > 0 Then
  70. MsgBox("Student With Id " & Trim(cmbId.Text) & " succesfully to added", MsgBoxStyle.OKOnly, "Message :")
  71. Else
  72. MsgBox("Student With Id " & Trim(cmbId.Text) & " Failure to added", MsgBoxStyle.OKOnly, "Message :")
  73. End If
  74. Refresh_Form()
  75. conn.Close()
  76.  
  77. End If
  78.  
  79. Catch ex As Exception
  80. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
  81. End Try
  82. End If
  83. End If
  84. End Sub
  85.  
  86. Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
  87. Dim check As Integer
  88. Dim cmdStudent As New SqlCommand
  89. Dim daStudent As New SqlDataAdapter
  90. Dim dsStudent As New DataSet
  91.  
  92. If txtId.text = "" Then
  93. MessageBox.Show("Please fill all data!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  94. Else
  95. If txtId.text = "" Or txtFirstName.Text = "" txtLastName.Text = "" Or txtAge.Text = "" Then
  96. MsgBox("Student Data is not completed", MsgBoxStyle.OKOnly)
  97. Else
  98. If MsgBox("Are you sure to edit Student data with Id : " & txtId.text & " ?", MsgBoxStyle.OKCancel, "Edit confirm") = MsgBoxResult.Cancel Then
  99. ' do nothing
  100. Else
  101. Try
  102. conn = GetConnect()
  103. conn.Open()
  104. cmdStudent = conn.CreateCommand
  105. cmdStudent.CommandText = "UPDATE Student SET FirstName ='" & Trim(txtFirstName.Text) & "', LastName= '" & Trim(txtLastName.Text) & "' , Age='" & Trim(txtAge.Text) & "' WHERE Id ='" & Trim(txtId.text) & "'"
  106. check = cmdStudent.ExecuteReader.RecordsAffected
  107. If check > 0 Then
  108. MsgBox("Student With Id " & Trim(txtId.text) & " Succesfully To Edit", MsgBoxStyle.OKOnly, "Info Update Data Student ")
  109. Else
  110. MsgBox("Student With Id " & Trim(txtId.text) & " Failure To Edit", MsgBoxStyle.OKOnly, "Info Update Data Student ")
  111. End If
  112. Refresh_Form()
  113. conn.Close()
  114.  
  115. Catch ex As Exception
  116. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
  117. End Try
  118. End If
  119. End If
  120. End If
  121. End Sub
  122.  
  123. Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
  124. Dim check As Integer
  125. Dim conn As SqlConnection
  126. Dim cmdStudent As New SqlCommand
  127. Dim daStudent As New SqlDataAdapter
  128. Dim dsStudent As New DataSet
  129.  
  130. If txtId.text <> "" Then
  131. If MsgBox("Are you sure to delete data with Id : " & txtId.text & " ?", MsgBoxStyle.OKCancel, "Delete confirm") = MsgBoxResult.Cancel Then
  132. ' do nothing
  133. Else
  134. conn = GetConnect()
  135. Try
  136. conn.Open()
  137. cmdStudent = conn.CreateCommand
  138. cmdStudent.CommandText = "DELETE FROM Student WHERE Id ='" & Trim(txtId.text) & "'"
  139. check = cmdStudent.ExecuteReader.RecordsAffected
  140. If check > 0 Then
  141. MsgBox("Student With Id " & Trim(txtId.text) & " Succesfully To Delete", MsgBoxStyle.OKOnly, "Info Delete Student")
  142. Else
  143. MsgBox("Student With Id " & Trim(txtId.text) & " Failure To Delete", MsgBoxStyle.OKOnly, "Info Delete Student")
  144. End If
  145. Refresh_Form()
  146. conn.Close()
  147.  
  148. Catch ex As Exception
  149. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
  150. End Try
  151. End If
  152. Else
  153. MsgBox("fill Id Student on Id textbox which student to delete!!", MsgBoxStyle.OKOnly, "Info Data")
  154. End If
  155. End Sub
0
kimhanu kimhanu is offline Offline | Mar 26th, 2008
what is conn = GetConnect() ?
please give the code of GetConnect() .
 
0
Jx_Man Jx_Man is offline Offline | Apr 11th, 2008
as i tell before post this code is continuance from my previous post "Show Data in DataGrid with VB.Net 2003 and SQLServer 2000". so i didn't write code how to connect SQLServer 2000 with VB.Net 2003 cause this already in there.
Click this lick to go to my previous snippet :
Show Data in DataGrid with VB.Net 2003 and SQLServer 2000
 
0
dlayante dlayante is offline Offline | Jun 20th, 2008
This code is exactly what i need but can you give a code of this using C# windows application
THANX...
 
0
muhammad.atif muhammad.atif is offline Offline | Feb 18th, 2009
how i can achiveve the same goal if i first save all input into dataset then all the data of dataset into database.
 
0
Piya27 Piya27 is offline Offline | Jul 11th, 2009
thanks.. it helped a lot...
 
0
dapsin999 dapsin999 is offline Offline | Sep 25th, 2009
Pls i am new to vb.net. Can this code help me as a beginner to save data in a datagrid in sql server unsing vs2008 and vb.net language. Thanks
dapsin
 
0
Jx_Man Jx_Man is offline Offline | Sep 28th, 2009
Yes..you can
Last edited by Jx_Man; Sep 28th, 2009 at 11:30 pm.
 
0
smdhas smdhas is offline Offline | 10 Days Ago
very useful code...however would appreciate if you cud post snapshots which wud b more helpful.
thanks...
 
0
niek_e niek_e is offline Offline | 10 Days Ago
@ smdhas: At the time that this snippet was created, (> a year ago) it wasn't possible yet to add screenshots to a snippet.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC