static void

SqlDataSource and Guids

You can't use SCOPE_IDENTITY() and you have to add the uniqueidentifier output parameter manually.
See version with integer identity.

Markup

<asp:SqlDataSource ID="SqlDataSourceProducts" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

        SelectCommand="SELECT [Id], [Name] FROM [Product]"></asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSourceDetail" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

        InsertCommand="

SET @Id = NEWID(); INSERT INTO [Order] ([Id], [ModifyDate], [ModifyUser], [ProductID], [Qty]) VALUES (@Id, @ModifyDate, @ModifyUser, @ProductID, @Qty)"

        OnInserting="SqlDataSourceDetail_Inserting" OnInserted="SqlDataSourceDetail_Inserted">

        <InsertParameters>

            <asp:Parameter Name="ModifyDate" Type="DateTime" />

            <asp:Parameter Name="ModifyUser" Type="String" />

            <asp:Parameter Name="ProductID" Type="Int32" />

            <asp:Parameter Name="Qty" Type="Int32" />

            <%--

            <asp:Parameter Name="Id" Type="Object" Direction="Output" />

            !This doesn't work. Manually add the parameter in the Inserting method!

            --%>

        </InsertParameters>

    </asp:SqlDataSource>

Code behind

protected void SqlDataSourceDetail_Inserting(object sender, SqlDataSourceCommandEventArgs e)

{

    //binding manually

    e.Command.Parameters["@ModifyUser"].Value = User.Identity.Name;

    e.Command.Parameters["@ModifyDate"].Value = DateTime.Now;

 

    SqlParameter insertedKey = new SqlParameter("@Id", SqlDbType.UniqueIdentifier);

    insertedKey.Direction = ParameterDirection.Output;

    e.Command.Parameters.Add(insertedKey);

}

 

protected void SqlDataSourceDetail_Inserted(object sender, SqlDataSourceStatusEventArgs e)

{

    if (e.Exception == null)

    {

        DbCommand command = e.Command;

        string id = ((Guid)command.Parameters["@Id"].Value).ToString();

        labMessage.Text = id + " added";

    }

}