Returning PK of gridview when updating or inserting (ASP.NET, C#) -
i require changes such inserts, updates , deletes table auditted, , i'm using simple sqldatasource , gridview automagically generated insert/update/delete statements. problem is, when row updated want id (primary key) , put auditting table - because parameters update query actual data , not primary key, don't know how access information. here update query:
insertcommand="insert [nominalcode] ([vaxcode], [reference], [costcentre], [department], [reportingcategory]) values (@vaxcode, @reference, @costcentre, @department, @reportingcategory)"
and here codebehind auditting:
protected void sqldatasource1_updating(object sender, sqldatasourcecommandeventargs e) { string fields = e.command.parameters[0].value.tostring() + "," + e.command.parameters[1].value.tostring() + "," + e.command.parameters[2].value.tostring() + "," + e.command.parameters[3].value.tostring() + "," + e.command.parameters[4].value.tostring(); system.security.principal. windowsprincipal p = system.threading.thread.currentprincipal system.security.principal.windowsprincipal; string[] namearray = p.identity.name.split('\\'); string name = namearray[1]; string querystring = "insert audit (source, action, item, userid, timestamp) values (@source, @action, @item, @userid, @timestamp)"; using (sqlconnection connection = new sqlconnection("con string removed privacy")) { sqlcommand command = new sqlcommand(querystring, connection); command.parameters.addwithvalue("@source", "nominal"); command.parameters.addwithvalue("@action", "update"); command.parameters.addwithvalue("@item", fields); command.parameters.addwithvalue("@userid", name); command.parameters.addwithvalue("@timestamp", datetime.now); connection.open(); try { command.executenonquery(); } catch (exception x) { response.write(x); } { connection.close(); } } }
okay, feel stupid. id located in clause of sql query of course! , pasted in insert command reason.
Comments
Post a Comment