How to use a DataSource and paramaters with VS 2008


I want to add data to a SQL database using a Stored Procedure. I know there is a way of running it via the DataSource object from the toolbox in VS 2008. I'm currently hard-coding it myself via the C# page (see attached code snippet).

I'm adding the User.Identity.Name to the DB, and this works fine. Can some one tell me how I can add the User.Identity.Name (or a String variable that represents User.Identity.Name) as a paramater via the SQL DataSource object from the toolbox. (See attached Screenshot).

I can get the original code to run by putting it in the Page_Load() method. If I use the datasource object in the ASPX page will the stored proc run automatically or will I need to call it somehow in the C# page?

many thanks
public void Test()
            SqlDataSource testDataSource = new SqlDataSource();
            testDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString();
            testDataSource.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
            testDataSource.InsertCommand = "mpUserInsert";
            testDataSource.InsertParameters.Add("NetID", User.Identity.Name.ToString());

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Define the parameter as asp:Parameter <asp:Parameter Name="UsrName" Type="String" />
You can use a OnInserting method to your SQLDataSource, and assign
Call the method in datasource as OnInserting="InsertUsers_OnInserting()"
write the code behind in.
protected void InsertUsers_OnInserting(object sender, SqlDataSourceCommandEventArgs e)
        e.Command.Parameters["@UsrName"].Value = Membership.GetUser().UserName;

or you can use a control parameter, and a hidden field. Set the current user name in the hidden field. Point the control parameter to the hiddne feild.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.