How to use a DataSource and paramaters with VS 2008

Posted on 2008-11-10
Last Modified: 2013-12-17

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

Question by:bluebirds1984
    1 Comment
    LVL 4

    Accepted Solution

    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.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now