I finally figured this out, thanks to the help of a co-worker. . .. and figured I would post it here, since this is the first thread I found relating to the subject.... (sqldsProjects is my sqldatasource)
Im working with my sqldatasource in the code behind. On my projects.aspx page, I have a gridview with the list of products. It has a add project area, which it only adds a project title, then it redirects to the projectdetails.aspx page to complete the rest of the information and then it can be updated.
//grab the output parameter value and assign it to a variable in my page class.[int idProject;]
protected void sqldsProjects_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
idProject = (int)((IDbDataParameter)e.Command.Parameters["idProject"]).Value;
}
// You insert the output parameter in the Inserting event
protected void sqldsProjects_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter();
param.DbType = DbType.Int32;
param.Direction = ParameterDirection.Output;
param.ParameterName = "idProject";
e.Command.Parameters.Add(param);
}
A button click event triggers a save - -
protected void btnAddProject_OnClick(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtProject.Text.Trim()))
{
sqldsProjects.InsertCommand = "InsertProject";
sqldsProjects.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
sqldsProjects.InsertParameters.Add("fldActive", TypeCode.Boolean, "False");
sqldsProjects.InsertParameters.Add("fldFeatured", TypeCode.Boolean, "False");
sqldsProjects.InsertParameters.Add("fldName", TypeCode.String, txtProject.Text.Trim());
sqldsProjects.InsertParameters.Add("fldLocation", TypeCode.String, string.Empty);
sqldsProjects.InsertParameters.Add("fldOwner", TypeCode.String, string.Empty);
sqldsProjects.InsertParameters.Add("fldCmpDate", TypeCode.DateTime, DBNull.Value.ToString());
sqldsProjects.InsertParameters.Add("fldDesc", TypeCode.String, string.Empty);
sqldsProjects.InsertParameters.Add("fldStatus", TypeCode.String, "0");
sqldsProjects.Insert();
Response.Redirect("projectdetails.aspx?id=" + idProject.ToString() );
}
}
--Stored procedure
ALTER PROCEDURE dbo.InsertProject
(
@fldActive bit,
@fldFeatured bit,
@fldName varchar(50),
@fldLocation varchar(50),
@fldOwner varchar(30),
@fldCmpDate DateTime,
@fldDesc text,
@fldStatus varchar(20),
@idProject int output
)
AS
SET NOCOUNT ON --stops the server from indicating the number of rows affected
INSERT INTO tblProjects
(fldActive, fldFeatured, fldName, fldLocation,
fldOwner, fldCmpDate, fldDesc, fldStatus)
VALUES
(@fldActive, @fldFeatured, @fldName, @fldLocation,
@fldOwner, @fldCmpDate, @fldDesc, @fldStatus)
SET @idProject = Scope_Identity()
Hope this helps anyone else looking for this info. I spent too much time on it... lol