View Single Post
Join Date: Jun 2007
Posts: 22
Reputation: dadelsen is an unknown quantity at this point 
Solved Threads: 5
dadelsen dadelsen is offline Offline
Newbie Poster

Re: Insert,delete,Update codings in VB.NET?

 
0
  #3
Jun 10th, 2008
Think it is an ASP.NET application. I made some changes, marked with XXX. On my system it works. Maybe the changes help you to get your program to work. Databaseprogramming is a wide area.
  1. Imports System
  2. Imports System.data
  3. Imports System.data.sqlclient
  4. 'XXX used a standard dataset since I do not have yours Imports dataset1
  5. Public Class WebForm1
  6. Inherits System.Web.UI.Page
  7.  
  8. 'XXX con needs to be in scope when the Insert-Button Click event is handled
  9. Dim con As SqlConnection
  10.  
  11. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12. 'XXX Dim con As SqlConnection
  13. Dim cmd As SqlCommand
  14. Dim adp As SqlDataAdapter 'XXX use SQLDataAdapter Dim adp As sqladapter
  15. Dim data As DataSet
  16.  
  17. 'XXX I do not have your database, please change connect string as required.
  18. 'XXX If you use the SQLClient dataprovider, you do not need the provider keyword.
  19. 'XXX instantiated the con object
  20. 'con.ConnectionString = "Provider=SQLOLEDB;initialcatalog=preethi;database=ACER;integrated security=true"
  21. con = New SqlConnection
  22. con.ConnectionString = "database=.;initial catalog=contacts;user=ASPNET;"
  23.  
  24. cmd = New SqlCommand 'XXX instantiated the command object
  25. 'XXX connection is assigned below
  26. 'cmd.Connection = con
  27. 'XXX cmd=new sqlcommand("select * from emp"),con
  28. cmd = New SqlCommand("select * from contacts", con) 'XXX
  29.  
  30. adp = New SqlDataAdapter(cmd)
  31. data = New DataSet 'instatiated dataset
  32. adp.Fill(data)
  33.  
  34. End Sub
  35.  
  36. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
  37.  
  38. Dim sSQL As String = "INSERT INTO contacts( contactid, firstname, lastname) VALUES (20, 'User20', 'User20Firstname')"
  39.  
  40. Dim cmd As SqlCommand = New SqlCommand(sSQL, con)
  41. con.Open() 'XXX connection must be opened before command is executed
  42. cmd.ExecuteNonQuery()
  43. con.Close() 'XXX
  44. Response.Write(" One record inserted")
  45.  
  46. End Sub
  47. End Class
Last edited by dadelsen; Jun 10th, 2008 at 6:28 am.
Reply With Quote