2

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Follow this pages to see more information about typed data sources and their parameters.. http://www.tek-tips.com/faqs.cfm?fid=7074

http://msdn.microsoft.com/en-us/library/ms228051.aspx

http://msdn.microsoft.com/en-us/library/z72eefad.aspx

<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />

check insert statement in this code snippet.

<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
  SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"

  InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); 
                 SELECT @EmpID = SCOPE_IDENTITY()"
  UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName 
                   WHERE EmployeeID=@EmployeeID"
  DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"

  ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
  OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
  RunAt="server">

  <SelectParameters>
    <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
  </SelectParameters>

  <InsertParameters>
    <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
  </InsertParameters>

</asp:sqlDataSource>

1 Comment

I've gone through that tutorial and I believe that my code is exactly in resemblance with his. The problem is that I can't access my @PatID outside of the .inserted event. I put it in the msgbox() and it displays the correct ID, but I am unable to use that as a <asp:controlparameter> with a different SqlDataSource so i can use that as a relational key in another database table. Is there anyway to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.