sql - Access SCOPE_IDENTITY() in CodeBehind C# TSQL -


i'm trying retrieve scope_identity() , place in parameter can access in codebehind. .aspx:

<asp:sqldatasource     id="sqldatasource1"     runat="server"     connectionstring="<%$ connectionstrings:mystring %>"     insertcommand="insert [countyhelp](county,city)     values (@county,@city) ;select @id = scope_identity()">     <insertparameters>                 <asp:formparameter name="county"  formfield="county" />           <asp:formparameter name="city"  formfield="city" />           <asp:parameter direction="output"  name="id" type="int32" />                  </insertparameters>      

then access in code behind:

protected void btn_send_click(object sender, sqldatasourcestatuseventargs e) {     string sid = e.command.parameters["@id"].value.tostring();     sqldatasource1.insert();     response.redirect("documentsnew.aspx?id=" + sid); } 

maybe button code not sending parameter value:

     <asp:button        id="button1"        runat="server"        text="add help"        onclick="btn_send_click"         />   

the error getting because using wrong eventargs button click event. use regular eventargs click event , see happens.

you grab scope_identity handling sqldatasources inserted event so

<asp:sqldatasource id="sqldatasource1" oninserted ="on_inserted" runat="server" connectionstring="<%$ connectionstrings:mystring %>" insertcommand="insert [countyhelp](county,city) values (@county,@city) ;select @id = scope_identity()"> <insertparameters>             <asp:formparameter name="county"  formfield="county" />       <asp:formparameter name="city"  formfield="city" />       <asp:sessionparameter direction="output"  name="id" type="int32" />              </insertparameters> </asp:sqldatasource> 

then

protected void btn_send_click(object sender, eventargs e) {       sqldatasource1.insert();  }  protected void on_inserted(object sender, sqldatasourcestatuseventargs e)  {     string sid = e.command.parameters["@id"].value.tostring();       response.redirect("documentsnew.aspx?id=" + sid); } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -