Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my INSERT and DELETE command is working but UPDATE command is not. whats wrong.. :(
VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

       'INSERT record
       Connect()

       sql = "SELECT (sname) FROM tblsample WHERE sname = @sname"
       cmd = New OleDbCommand(sql, conn)

       cmd.Parameters.Add(New OleDbParameter("@sname", OleDbType.VarChar, 255)).Value = TextBox1.Text

       Dim rec = cmd.ExecuteScalar

       If rec = TextBox1.Text Then
           MessageBox.Show("Name already exist.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
           Exit Sub
       End If

       conn.Close()
       conn.Dispose()
       cmd.Dispose()

       Try

           Connect()

           Trans = conn.BeginTransaction

           sql = "INSERT INTO tblsample(sname,contact) VALUES(@sname,@contact)"

           cmd = New OleDbCommand(sql, conn, Trans)

           cmd.Parameters.Add(New OleDbParameter("@sname", OleDbType.VarChar, 255)).Value = TextBox1.Text

           cmd.Parameters.Add(New OleDbParameter("@contact", OleDbType.VarChar, 255)).Value = TextBox2.Text

           cmd.ExecuteNonQuery()

           Trans.Commit()

           conn.Close()
           conn.Dispose()
           cmd.Dispose()

           MessageBox.Show("Saved.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
           Call loadrec()
       Catch ex As Exception
           Trans.Rollback()
           MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
       End Try

   End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click

       'DELETE record
       Try
           Connect()

           Trans = conn.BeginTransaction

           sql = "DELETE FROM tblsample WHERE ID = @ID"

           cmd = New OleDbCommand(sql, conn, Trans)

           cmd.Parameters.Add(New OleDbParameter("@ID", OleDbType.Integer)).Value = DataGridView1.SelectedCells(0).Value

           cmd.ExecuteNonQuery()

           Trans.Commit()

           conn.Close()
           conn.Dispose()
           cmd.Dispose()

           Call loadrec()

           MessageBox.Show("DELETED!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)

       Catch ex As OleDb.OleDbException
           Trans.Rollback()
           MessageBox.Show(ex.Message, "DATABASE ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
       Catch ex As Exception
           Trans.Rollback()
           MessageBox.Show(ex.Message)
       End Try

   End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

       'UPDATE record
       Try
           Connect()

           Trans = conn.BeginTransaction

           sql = "UPDATE tblsample SET sname = @sname,contact = @contact WHERE contact = @ID"

           cmd = New OleDbCommand(sql, conn, Trans)

           cmd.Parameters.Add(New OleDbParameter("@sname", OleDbType.VarChar, 255)).Value = TextBox1.Text
           cmd.Parameters.Add(New OleDbParameter("@contact", OleDbType.VarChar, 255)).Value = TextBox2.Text
           cmd.Parameters.Add(New OleDbParameter("@ID", OleDbType.Integer)).Value = DataGridView1.SelectedCells(0).Value

           cmd.ExecuteNonQuery()

           Trans.Commit()

           conn.Close()
           conn.Dispose()
           cmd.Dispose()
           Call loadrec()
           MessageBox.Show("UPDATED!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)

       Catch ex As Exception
           Trans.Rollback()
           MessageBox.Show(ex.Message)
       End Try

   End Sub
Posted
Updated 12-Aug-12 10:23am
v3
Comments
Sandeep Mewara 12-Aug-12 15:12pm    
1. Do you get any error? If so, please share.
2. What do you see when you DEBUG?
TinTin19 12-Aug-12 15:29pm    
there is no error. the code will continue and saying that the record is updated.
TinTin19 12-Aug-12 16:26pm    
sorry sir, i put wrong question, i mean is the UPDATE command, not the DELETE command. i updated my question now. sorry
Philip Stuyck 12-Aug-12 17:18pm    
is this a sql server database ? Because not all databases support named parameters. Most expect ? iso @paramname.
TinTin19 12-Aug-12 20:05pm    
sir, it's ms access database.

If nothing is deleted it must be because your Id is wrong. Are you sure the parameter is filled in correctly and that this value actually exists.
ExecuteQuery has a return value with the number of updates that have been done. Very usefull information during debugging!

Also check your sql syntax, but normally if it is wrong exceptions should be thrown. Since this is ms access your named parameters don't work, this only works in sql server. Better read this link too :
http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access[^]

Please mark this as a solution if this solves your problem.
 
Share this answer
 
v2
Comments
TinTin19 12-Aug-12 16:26pm    
sorry sir, i put wrong question, i mean is the UPDATE command, not the DELETE command. i updated my question now. sorry
Philip Stuyck 12-Aug-12 17:25pm    
well, my comment still applies.
Please check the following line while debbuging, are you getting any value in DataGridView1.SelectedCells(0).Value.

C#
cmd.Parameters.Add(New OleDbParameter("@ID", OleDbType.Integer)).Value = DataGridView1.SelectedCells(0).Value


Thanks
Ashish
 
Share this answer
 
Comments
singhritu 13-Aug-12 12:13pm    
my 5!
Rahul Rajat Singh 14-Aug-12 0:44am    
good answer. +5.
thank you very much to you all.. problem solved with the help of your suggestions and advice. i change the cmd.parameters.add ...etc. in correct order based in the order of my fields, then it works. thanks!
 
Share this answer
 
There will be problem only in this line
sql = "DELETE FROM tblsample WHERE ID = @ID"

check the @ID value whether you input it rightly or exist correctly in your database..
 
Share this answer
 
Comments
TinTin19 12-Aug-12 16:26pm    
sorry sir, i put wrong question, i mean is the UPDATE command, not the DELETE command. i updated my question now. sorry
ridoy 13-Aug-12 3:21am    
There will be only problem in here "WHERE contact = @ID" of your update statement.Check your @ID value of your database.Update can fail here if you use such type of value that doesn't exist in your database.So carefully look out this point.
Check your update query carefully:
VB
sql = "UPDATE tblsample SET sname = @sname,contact = @contact WHERE contact = @ID"

Quote:
contact = @contact
WHERE
contact = @ID

You are updating the same ID field which you are providing as condition. Check your table, otherwise use this:
VB
sql = "UPDATE tblsample SET sname = @sname,contact = @contact WHERE ID = @ID"



--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900