473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot do insert and delete

in my project there are
5 textbox : tbOrderid, tbSeqNum, tbFoodCode, tbFoodDesc and tbPrice
9 button: btInsert, btdelete, btUpdate, btClear, btBind, btFirst,
btPrevious, btNext and btLast

i cannot do the insert and delete function. Can anybody help me? If
you need the program I can email to anyone who want to help me,
thanks!! ^^Alan

and the code is as follow:

Imports System.Data.Ole Db

Public Class Form4
Inherits System.Windows. Forms.Form
Dim dap1 As OleDbDataAdapte r
Dim das1 As DataSet
Dim myData As OleDbDataReader

Sub populate()

Dim conStr As String
Dim cnn1 As OleDb.OleDbConn ection

'connect to access DB
conStr = "Provider=Micro soft.Jet.OLEDB. 4.0;Data
Source=C:\Docum ents and Settings\alan\M y Documents\Visua l Studio
Projects\Functi onTest\test.mdb ;" & _
"Persist Security Info=False"
cnn1 = New OleDbConnection (conStr)

'instantiate a data adapter based on the SQL String
dap1 = New OleDbDataAdapte r("Select order_id, food_code,
food_desc, price,seq_num from order_detail", cnn1)

'************** *************** *************** *************** *************** *************** *************** *************** ***
'------------------------
'Update Command
'------------------------
dap1.UpdateComm and = New OleDbCommand("U pdate order_detail set
food_desc=@food Desc where order_id=@order id and seq_num=@seqnum ",
cnn1)
dap1.UpdateComm and.Parameters. Add("@fooddesc" ,
OleDbType.VarCh ar, 50, "food_desc" )

Dim prm1 As OleDbParameter =
dap1.UpdateComm and.Parameters. Add("@orderid", OleDbType.Numer ic)
prm1.SourceColu mn = "order_id"

Dim prm2 As OleDbParameter =
dap1.UpdateComm and.Parameters. Add("@seqnum", OleDbType.Numer ic)
prm2.SourceColu mn = "seq_num"

prm1.SourceVers ion = DataRowVersion. Original
prm2.SourceVers ion = DataRowVersion. Original
'************** *************** *************** *************** *************** *************** *************** *************** ****
'------------------
'Insert Command'
'-----------------
dap1.InsertComm and = New OleDbCommand("I nsert into
order_detail (order_id, seq_num, food_code, food_desc,price ) values" &
_
"(@orderid,@seq num,@foodcde,@f ooddesc,@price) ", cnn1)
dap1.InsertComm and.Parameters. Add("@fooddesc" ,
OleDbType.VarCh ar, 50, "food_desc" )

Dim prm3 As OleDbParameter =
dap1.InsertComm and.Parameters. Add("@orderid", OleDbType.Numer ic)
prm3.SourceColu mn = "order_id"

Dim prm4 As OleDbParameter =
dap1.InsertComm and.Parameters. Add("@seqnum", OleDbType.Numer ic)
prm4.SourceColu mn = "seq_num"

Dim prm5 As OleDbParameter =
dap1.InsertComm and.Parameters. Add("@foodcode" , OleDbType.Numer ic)
prm5.SourceColu mn = "food_code"

Dim prm6 As OleDbParameter =
dap1.InsertComm and.Parameters. Add("@fooddesc" , OleDbType.VarCh ar)
prm6.SourceColu mn = "food_desc"

Dim prm7 As OleDbParameter =
dap1.InsertComm and.Parameters. Add("@price", OleDbType.Curre ncy)
prm7.SourceColu mn = "price"

prm3.SourceVers ion = DataRowVersion. Original
prm4.SourceVers ion = DataRowVersion. Original
'************** *************** *************** *************** *************** *************** *************** *************** ******
'------------------
'Delete Command
'-----------------
dap1.DeleteComm and = New OleDbCommand("D elete from
prder_detail where order_id=@order id and seq_num=@seqnum ", cnn1)
Dim prm8 As OleDbParameter =
dap1.DeleteComm and.Parameters. Add("@orderid", OleDbType.Numer ic)
prm8.SourceColu mn = "order_id"

Dim prm9 As OleDbParameter =
dap1.DeleteComm and.Parameters. Add("@seqnum", OleDbType.Numer ic)
prm9.SourceColu mn = "seq_num"

prm8.SourceVers ion = DataRowVersion. Original
prm9.SourceVers ion = DataRowVersion. Original
'************** *************** *************** *************** *************** *************** *************** *************** ******

cnn1.Open()
das1 = New DataSet
dap1.Fill(das1, "order_deta il")

cnn1.Close()
End Sub

Private Sub Form4_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

populate()

tbBinding()

displayLV()

End Sub

Private Sub btFirst_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles btFirst.Click

Me.BindingConte xt(das1, "order_detail") .Position =
Me.BindingConte xt(das1, "order_detail") .Position.MinVa lue

End Sub

Private Sub btPrevious_Clic k(ByVal sender As System.Object, ByVal
e As System.EventArg s) Handles btPrevious.Clic k

Me.BindingConte xt(das1, "order_detail") .Position -= 1

End Sub

Private Sub btNext_Click(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles btNext.Click

Me.BindingConte xt(das1, "order_detail") .Position += 1

End Sub

Private Sub btLast_Click(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles btLast.Click

Me.BindingConte xt(das1, "order_detail") .Position =
Me.BindingConte xt(das1, "order_detail") .Position.MaxVa lue

End Sub

Private Sub btUpdate_Click( ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btUpdate.Click

If Me.BindingConte xt(das1, "order_detail") .Position = 0 Then
Me.BindingConte xt(das1, "order_detail") .Position -= 1
Else
Me.BindingConte xt(das1, "order_detail") .Position -= 1
Me.BindingConte xt(das1, "order_detail") .Position += 1
End If

dap1.Update(das 1, "order_deta il")

End Sub
Sub displayLV()

Dim lvCol As ColumnHeader
Dim itm As ListViewItem
Dim sht1 As Short
Dim total As Integer = 0
Dim i As Integer = 0

' myData = dap1.SelectComm and.ExecuteRead er

lvCol = New ColumnHeader
lvCol.Text = "Food Code"
lv1.Columns.Add ("Food Code", 150, HorizontalAlign ment.Left)

lvCol = New ColumnHeader
lvCol.Text = "Price $"
lv1.Columns.Add ("Price $", 100, HorizontalAlign ment.Left)

lvCol = Nothing

For i = 0 To Me.BindingConte xt(das1, "order_detail") .Count - 1
Me.BindingConte xt(das1, "order_detail") .Position = i
itm = New ListViewItem
itm.Text = tbFood.Text

itm.SubItems.Ad d(tbPrice.Text)

total += CInt(tbPrice.Te xt)

lv1.Items.Add(i tm)
lbTotal.Text = total

Next i

Me.BindingConte xt(das1, "order_detail") .Position = 0
End Sub

Private Sub btInsert_Click( ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btInsert.Click

Dim newRow As DataRow = das1.Tables("or der_detail").Ne wRow
newRow("order_i d") = tbOrderId.Text
newRow("seq_num ") = tbSeqNum.Text
newRow("food_co de") = tbFoodCode.Text
newRow("food_de sc") = tbFood.Text
newRow("price") = tbPrice.Text

das1.Tables("or der_detail").Ro ws.Add(newRow)
dap1.Update(das 1, "order_deta il")

tbFood.DataBind ings.Add(New Binding("text", das1,
"order_detail.f ood_desc"))
tbPrice.DataBin dings.Add(New Binding("text", das1,
"order_detail.p rice"))
tbOrderId.DataB indings.Add(New Binding("text", das1,
"order_detail.o rder_id"))
tbSeqNum.DataBi ndings.Add(New Binding("text", das1,
"order_detail.s eq_num"))
tbFoodCode.Data Bindings.Add(Ne w Binding("text", das1,
"order_detail.f ood_code"))

Me.BindingConte xt(das1, "order_detail") .Position =
Me.BindingConte xt(das1, "order_detail") .Position.MaxVa lue

End Sub

Private Sub btDelete_Click( ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btDelete.Click

Dim dav1 As DataView = New
DataView(das1.T ables("order_de tail"))
dav1.Sort = ("seq_num")
Dim rowIndex As Integer = dav1.Find(tbSeq Num.Text)
das1.Tables("or der_detail").Ro ws(rowIndex).De lete()
dap1.Update(das 1, "order_deta il")

End Sub

Private Sub Clear_Click(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles Clear.Click

Me.BindingConte xt(das1, "order_detail") .Position =
Me.BindingConte xt(das1, "order_detail") .Position.MaxVa lue

tbFood.DataBind ings.Clear()
tbPrice.DataBin dings.Clear()
tbOrderId.DataB indings.Clear()
tbSeqNum.DataBi ndings.Clear()
tbFoodCode.Data Bindings.Clear( )

tbFood.Text = ""
tbPrice.Text = ""
tbFoodCode.Text = ""
tbSeqNum.Text = CInt(tbSeqNum.T ext) + 1
End Sub
Sub tbBinding()
tbFood.DataBind ings.Add(New Binding("text", das1,
"order_detail.f ood_desc"))
tbPrice.DataBin dings.Add(New Binding("text", das1,
"order_detail.p rice"))
tbOrderId.DataB indings.Add(New Binding("text", das1,
"order_detail.o rder_id"))
tbSeqNum.DataBi ndings.Add(New Binding("text", das1,
"order_detail.s eq_num"))
tbFoodCode.Data Bindings.Add(Ne w Binding("text", das1,
"order_detail.f ood_code"))

tbOrderId.ReadO nly = True
tbSeqNum.ReadOn ly = True
'tbFoodCode.Rea dOnly = True
End Sub

Private Sub btBind_Click(By Val sender As System.Object, ByVal e As
System.EventArg s) Handles btBind.Click
tbBinding()

End Sub
End Class
Nov 20 '05 #1
1 1512
Cor
Hi Alan,

I asume this question is answered by Jody in the message above this (with my
small addition)?

Cor
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2580
by: Simon | last post by:
Hi I am trying to produce an update trigger. I understand the concept of delete and insert triggers without a problem. Unfortuantely, the update triggers do not have particularly simple documentation in BoL. So, can someone please explain to me, quite simply how I would produce a trigger on the following: I have table 1 which we'll call simon. In here are various columns and
8
9024
by: Kragen Sitaker | last post by:
ERROR: Cannot insert a duplicate key into unique index pg_class_relname_nsp_index We've been getting this error in our application every once in a while --- typically once an hour to once a day, although it varies over time. The daemon that gets the error exits and restarts a few seconds later. Usually it's fine then, but sometimes the error happens three or more times in a row. Occasionally, instead, we get "ERROR: Cannot insert a...
16
3876
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the table, or perhaps bytes, being updated where the engine just decides, screw it, i'll just make a new one. surfed this group and google, but couldn't find anything. the context: we have some java folk who like to parametize/
0
7385
by: Mohammad Ali via DotNetMonster.com | last post by:
hi guys, I have a problem with my datagrid. My form is set up so I have two textboxes an add button and an editable datagrid. The datagrid is bound to a datatable which is in my cache.When I click the add button the contents of the textboxes are insertedinto the datatabel in teh cache and the datagrid is bound to it. However the problem comes when I delete a row from the database and then
0
1926
by: Luqman | last post by:
I have copied the following code from Internet, and copied it to a file named : test.aspx, and copied that file to c:\Inetpub\wwwroot Directory. When I type on my Internet explorer: http:\\localhost\test.aspx, following error occured: The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
4
4862
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load event I Fill the datagridview source table with a sql DataAdapter (da) da.SelectCommand.CommandText = "Select * from Servertbl1" da.Fill(ds, "tbl1") so far, so good. If I add a row to the datagridview I use the following sqlDataAdapter code to update the server table - which works OK when...
0
1799
by: yukijocelyn | last post by:
I have experienced a problem here while doing a form for accessing datadbase using MS Access. I'm using the AccessDataSource control, and using Gridview to pull the data from the database. I am able to see the contents in the database, but whenever I click "Delete" or "Update", the data never gets deleted or get updated repectively. It feels as if it was a normal page load, nothing is done to the data in the database. I also have another problem...
2
1252
by: ahmedlasheen | last post by:
hello every body when iam trying to delete the last record in DGV this erro appears "Input string was not in a correct format." this error appears only when i am trying to delete the last record. . the code i using is the following public void Delete(int id) { ////////////Delete the record from the dataset./////////// DataRow DR = dttel.Select("id=" + id + "");
1
3027
by: skanemupp | last post by:
in this program when using the "c"-button it deletes the last token entered. i want to delete the token after the mousecursor. lets say the string is: 12*(81**.5+12) and i put the cursor between the * and * because i want times .5 not root. now if i press "c" it deletes ")" which is not what i want. ive tried coming up with a function that does what i want but neither of the ways as shown below works. i want to do something like...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.