c# - Uupdate values in two tables via stored procedure -


i have 2 tables , need update values in them via stored procedure. tried update times update first table only, others second or fail due cannot allow duplicates. when updates whole data in table becomes same new updated ones. i've reached error after these lines of codes

cannot insert value null column 'emp_id',table 'datepics'; column not allow nulls. update fails.the statement has been terminated

here sql code :

alter procedure [dbo].[updateemp]     @empname nvarchar(100),     @nationality nvarchar(30),         @passport nvarchar(20),     @contractdate date,     @healthdate date begin      set nocount on;  declare @ids table (id int ) update employee set  empname=@empname, nationality=@nationality, visa=@visa, passport=@passport, receiveddate=@receiveddate,idissue=@idissue, idexpiry=@idexpiry, sponsor=@sponsor output inserted.id @ids (id) id = @id  update datepics set fingerdate=@fingerdate, contractdate=@contractdate, healthdate=@healthdate emp_id in (select id @ids); end 

after writing stored procedure code, wrote c# code this:

private void updatebtn_click(object sender, eventargs e) {         sqlcommand cmd = new sqlcommand();         cmd.connection = db.con;         cmd.commandtype = commandtype.storedprocedure;         cmd.commandtext = "updateemp";          cmd.parameters.addwithvalue("@empname", nameseartxt.text);         cmd.parameters.addwithvalue("@nationality", natseartxt.text);                     cmd.parameters.addwithvalue("@passport", passseartxt.text);                     cmd.parameters.addwithvalue("@contractdate", contractseartxt.text);         cmd.parameters.addwithvalue("@healthdate", healthseartxt.text);          db.con.open();         int = cmd.executenonquery();          if (up > 0)         {             messagebox.show("update done ", "done !");             searnametxt.text = "";         }          else         {             messagebox.show("failed update", "fail !");             searnametxt.text = "";         }         db.con.close();     } 

any clue?

i can see 3 problems query. 1 declare id, don't assign before using it, null first query, never update rows:

declare @id int   update frsttable set  empname=@empname, nationality=@nationality, passport=@passport id = @id 

secondly, using scope_identity attempt id of record has been updated. can't that, scope_identity return last inserted id, not affected updates. need use output updated id:

declare @ids table (id int);  update  firsttable output inserted.id @ids (id) set     empname = @empname,          nationality = @nationality,          passport = @passport; 

thirdly, second update statement has no clause, update entire table:

update  scndtable  set     emp_id=@id, contractdate=@contractdate, healthdate=@healthdate   empid in (select id @ids); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -