Insert,delete,Update codings in VB.NET?

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

Insert,delete,Update codings in VB.NET?

 
0
  #1
Jun 9th, 2008
Hello,
In .NET im using the backend as SQL Server. but when i write codings for all insert,update and delete in .NET and run the form, the error it shows like
"Oledb exception was unhandled by the user" and error in SQL query.
the codings are....follows..
  1. imports system
  2. imports system.data
  3. imports system.data.sqlclient
  4. imports dataset1.
  5.  
  6. And in Page_Load..
  7. dim con as sqlconnection
  8. dim cmd as sqlcommand
  9. dim adp as sqladapter
  10. dim data as dataset1
  11. con.connectionstring="Provider=SQLOLEDB;initialcatalog=preethi;database=ACER;integrated security=true"
  12. cmd.connection=con
  13. cmd=new sqlcommand("select * from emp"),con
  14. adp=new sqldataadapter(cmd)
  15. adp.Fill(data)
  16.  
  17. 'since i have only 3 fields
  18. textbox1.text=data.tables(1).rows(0).item(0)
  19. textbox2.text=data.tables(1).rows(0).item(1)
  20. textbox3.text=data.tables(1).rows(0).item(2)
  21.  
  22. In Insert button-click event.....
  23. ' its bcoz the second textbox has numeric value and 1st and 3rd have only string values.
  24.  
  25. cmd = new sqlcommand("insert into emp values( ' " & textbox1.text & " ', " & textbox2.text & ", ' " & textbox3.text & " ')" , con
  26. cmd.executeNonQuery()
  27. Response.write(" One record inserted").
So these are the codings im using. for delete also the same typeim using. but im getting error, can anyone help me to solve this?
Last edited by Ancient Dragon; Jan 13th, 2009 at 10:59 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: anansiva is an unknown quantity at this point 
Solved Threads: 1
anansiva anansiva is offline Offline
Newbie Poster

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

 
0
  #2
Jun 9th, 2008
Hi,
I think that your command will be the problem for you..please replace "cmd=new sqlcommand("select * from emp",con)" in your code and test it whether it is working or not..
I dont get any other bug from your code.....Thanks
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

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

 
0
  #4
Jun 14th, 2008
Hi,
First of all thanks. the Insert command is working. but if i want to insert at run time, how can i give the coding?

But the delete command is not working.

[ Dim sSql as String = " Delete From tablename where name = ' " & Textbox1.Text & " ' " ]
[ Com = New SqlCommand(sSql,Con) ]
[ Com.ExecuteNonQuery() ]
[ Response.Write(" One Record Deleted ") ]

Can you tell me any other coding?
Reply With Quote Quick reply to this message  
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
  #5
Jun 14th, 2008
In most cases you would use a dataset object to keep a copy of database data inside your program, then insert, change and delete the rows in the dataset and, when done, you would use dataadapters to write the changes from the dataset to the database. But this is too much to be solved in a forum thread. There are books available, for example David Sceppa, Programming ADO.NET.

For directly inserting and deleting data into a database (using a literal on the form to show feedback):

Insert:
  1. Private Sub btnInsertDirect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  2. Handles btnInsertDirect.Click
  3. Dim iRet As Integer
  4. Dim iNextId As Integer
  5. Dim sConnectionString As String = "server=(local);database=contacts;user=ASPNET"
  6. Dim conn As New SqlConnection(sConnectionString)
  7. Dim sSQL As String
  8.  
  9. sSQL = "INSERT INTO contacts (contactid, firstname, lastname)" & _
  10. " VALUES (@contactid, @firstname, @lastname)"
  11.  
  12. Dim cmd As New SqlCommand(sSQL, conn)
  13. iNextId = 123
  14. cmd.Parameters.Add(New SqlParameter("@contactid", 123))
  15. cmd.Parameters.Add(New SqlParameter("@firstname", txtFirstName.Text))
  16. cmd.Parameters.Add(New SqlParameter("@lastname", txtLastName.Text))
  17.  
  18. conn.Open()
  19. Try
  20. iRet = cmd.ExecuteNonQuery()
  21. litMsg.Text = String.Format("Inserted {0} records", iRet)
  22. Catch ex As System.Exception
  23. litMsg.Text = String.Format("Error: {0}", ex.ToString)
  24. Finally
  25. conn.Close()
  26. End Try
  27.  
  28. End Sub
Delete:
  1. Private Sub btnDeleteDirect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteDirect.Click
  2. Dim iRet As Integer
  3. Dim sConnectionString As String = "server=(local);database=contacts;user=ASPNET"
  4. Dim conn As New SqlConnection(sConnectionString)
  5. Dim sSQL As String
  6.  
  7. sSQL = "DELETE FROM contacts WHERE lastname = '" & txtLastName.Text & "'"
  8.  
  9. Dim cmd As New SqlCommand(sSQL, conn)
  10. conn.Open()
  11. Try
  12. iRet = cmd.ExecuteNonQuery()
  13. litMsg.Text = String.Format("Deleted {0} records", iRet)
  14. Catch ex As System.Exception
  15. litMsg.Text = String.Format("Error: {0}", ex.ToString)
  16. Finally
  17. conn.Close()
  18. End Try
  19. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 38
Reputation: preethi_ga is an unknown quantity at this point 
Solved Threads: 0
preethi_ga preethi_ga is offline Offline
Light Poster

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

 
0
  #6
Jun 23rd, 2008
Hi, In the TRY .....CATCH block,
u have written some thing like...
[ litMsg.text ] what is this ? is it textbox or anything??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

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

 
0
  #7
Jun 23rd, 2008
i think its textbox
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
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
  #8
Jun 23rd, 2008
litMsg is a "literal", a Server Control from the toolbox/Web Forms Tab.

When you first posted the thread you wrote

"And in Page_Load.."
and "Response.Write"

so I thought your application must be an ASP. NET application, since in a Windows Forms Application the event would be "Form... Load", and there normally is no "Resonse.Write" in a Windows Forms application.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC