943,899 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 15266
  • VB.NET RSS
Jun 9th, 2008
0

Insert,delete,Update codings in VB.NET?

Expand Post »
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..
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Jun 9th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
anansiva is offline Offline
4 posts
since Jan 2008
Jun 10th, 2008
0

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

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.
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 25
Solved Threads: 5
Newbie Poster
dadelsen is offline Offline
22 posts
since Jun 2007
Jun 14th, 2008
0

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

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Jun 14th, 2008
0

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

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:
VB.NET Syntax (Toggle Plain Text)
  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:
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 5
Newbie Poster
dadelsen is offline Offline
22 posts
since Jun 2007
Jun 23rd, 2008
0

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

Hi, In the TRY .....CATCH block,
u have written some thing like...
[ litMsg.text ] what is this ? is it textbox or anything??
Reputation Points: 10
Solved Threads: 0
Light Poster
preethi_ga is offline Offline
38 posts
since Jun 2008
Jun 23rd, 2008
0

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

i think its textbox
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,140 posts
since Nov 2007
Jun 23rd, 2008
0

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

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.
Reputation Points: 25
Solved Threads: 5
Newbie Poster
dadelsen is offline Offline
22 posts
since Jun 2007
Jul 17th, 2010
0
Re: Insert,delete,Update codings in VB.NET?
am new to vb.net. i use sql 3.5 compact as my database. how can i save(update) to my database? Please help me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
olobo is offline Offline
1 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Data Analysis with zedgraph and imported data question
Next Thread in VB.NET Forum Timeline: No Description in Task Manager





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC