Using Output Parameter in SqlDataSource

10,160

In this InsertPatientInfo_Inserted handler, you can assign the output parameter's value as the default value for your other SqlDataSource named InsInqInfo:

Protected Sub InsertPatientInfo_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles InsertPatientInfo.Inserted
    Dim PatID As String = e.Command.Parameters("@PatID").Value
    MsgBox(PatID, MsgBoxStyle.Critical)
    ' Assign the output as the default for 
    InsInqInfo.InsertCommand("@PatID").DefaultValue = PatID
End Sub

I personally would move all this code to a the code behind.

Share:
10,160
Nick G
Author by

Nick G

Updated on June 04, 2022

Comments

  • Nick G
    Nick G almost 2 years

    I am retrieving the scope_identity of a db entry, and I want to use that variable in a different SqlDataSource, primarily as an but I am unable to access the variable. I have the variable being displayed in a msgbox, and it is displaying properly, I am just unsure how to access it in the SqlDataSource. Here is my code;

    This is the datasource that inserts the first information and received the scope_identity, as well as the _inserted event; Code:

    <asp:SqlDataSource ID="InsertPatientInfo" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
    providername="<%$ ConnectionStrings:DataConnectionString.ProviderName %>"
        InsertCommandType="StoredProcedure"
        InsertCommand = "InsertPatInfo"
        OnInserted="InsertPatientInfo_Inserted">
        <InsertParameters>
            <asp:ControlParameter ControlID = "PatInfoName" Name="PatName" PropertyName="text"/>
            <asp:ControlParameter ControlID = "PatInfoAge" Name="PatAge" PropertyName="text" />
            <asp:ControlParameter ControlID = "PatInfoState" Name="PatState" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "PatInfoCountry" Name="PatCountry" PropertyName="selectedvalue" />
            <asp:ControlParameter ControlID = "PatInfoPhone" Name="PatPhone" PropertyName = "text" />
            <asp:ControlParameter ControlID = "PatInfoCell" Name="PatCell" PropertyName="Text" />
            <asp:Parameter DbType="Int32" Direction="Output" Name="PatID" />
        </InsertParameters> 
    Protected Sub InsertPatientInfo_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles InsertPatientInfo.Inserted
        Dim PatID As String = e.Command.Parameters("@PatID").Value
        MsgBox(PatID, MsgBoxStyle.Critical)
    End Sub
    

    Here is the call to the next SqlDataSource, with the InsertCommand included, I'm planning on switching to a stored procedure once I get it working; I continue to get the error, that the scalar varaible @PatID is not set, am I supposed to declare it as a and if so, what type? Code:

          <asp:SqlDataSource ID="InsInqInfo" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
            providerName="<%$ ConnectionStrings:DataConnectionString.ProviderName %>"
            InsertCommand = "Insert into tblInquirer(InqPatID, InqName, InqState, InqCountry, InqPhone, InqRelation, InqVia, InqCareLevel, InqProgram) VALUES 
            (@PatID, @InqName, @InqState, @InqCountry, @InqPhone, @InqRelation, @InqVia, @InqCareLevel, @InqProgram)"
    
    
    <InsertParameters>
    
                <asp:ControlParameter ControlID = "InqName" Name="InqName" PropertyName="text"/>
                <asp:ControlParameter ControlID = "InqStateList" Name="InqState" PropertyName="selectedvalue" />
                <asp:ControlParameter ControlID = "InqCountry" Name="InqCountry" PropertyName="selectedvalue" />
                <asp:ControlParameter ControlID = "InqPhone" Name="InqPhone" PropertyName="Text" />
                <asp:ControlParameter ControlID = "radInqRel" Name="InqRelation" PropertyName="selectedvalue" />
                <asp:ControlParameter ControlID = "InitInqVia" Name="InqVia" PropertyName = "selectedvalue" />
                <asp:ControlParameter ControlID = "CareLevel" Name="InqCareLevel" PropertyName="selectedvalue" />
                <asp:ControlParameter ControlID = "ProgSelect" Name="InqProgram" PropertyName="selectedvalue" />     
            </InsertParameters>
    

    Thank you in advance, Nick