Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can any body Help me in below code in which when i update Dept_Name column of Dept
table in sql database it will also update the record of same column Dept_Name in Staffs
table.

Below is the Update Code for Dept Table:-

VB
Dim obj As New ConStr
            Dim sql As String
            sql = "update Dept set Dept_Name='" & TextBox2.Text & "' where Dept_No=" & TextBox1.Text
            obj.RunInsertDeleteUpdateQry(sql)
            'RefreshGrid()
            TextBox1.Clear()
            TextBox2.Clear()
            MsgBox("Data Updated Successfully")


Below is the Update Code for Staffs Table:-

VB
Dim obj As New ConStr
            Dim sql As String
            sql = "update Staffs set Dept_Name='" & TextBox2.Text & "' where Dept_No=" & TextBox1.Text
            obj.RunInsertDeleteUpdateQry(sql)
            'RefreshGrid()
            TextBox1.Clear()
            TextBox2.Clear()
            MsgBox("Data Updated Successfully")


But Staffs table was not updated.

Please help me.........
Posted
Updated 16-Aug-15 21:23pm
v2

Check whether staffs table contains data with this dept number and name.

As an aside, you should use command parameters to avoid SQL Injection[^].
 
Share this answer
 
Comments
hspl 17-Aug-15 3:21am    
Staffs table does not contain dept number.
Abhinav S 17-Aug-15 3:40am    
Then obviously your query is wrong.
sql = "update Staffs set Dept_Name='" & TextBox2.Text & "' where Dept_No=" & TextBox1.Text
hspl 17-Aug-15 3:35am    
can you please help me
I would suggest not to repeat the data on several tables. If the staff is linked to a department, define a foreign key relation from Staff to Department and have the department name only in the department table. When fetching the data, join both department and staff in order to get data rom both tables.

However, if you need to update both tables, a very traditional approach is to create a stored procedure which would contain the updates. The stored procedure would have the input values as parameters.

Have a look at CREATE PROCEDURE[^]
 
Share this answer
 
v2
Just to add to what the others have said, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
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