User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 373,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,808 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser:
Mar 5th, 2008
Views: 6,431
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.
Last edited : Mar 5th, 2008.
vbnet Syntax | 5 stars
  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
Comments (Newest First)
dlayante | Newbie Poster | 34 Days Ago
This code is exactly what i need but can you give a code of this using C# windows application
THANX...
Jx_Man | Nearly a Posting Maven | 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
kimhanu | Newbie Poster | Mar 26th, 2008
what is conn = GetConnect() ?
please give the code of GetConnect() .
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 7:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC