• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

How to use a DataSource and paramaters with VS 2008

Hi,

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

screenshot.jpg
0
bluebirds1984
Asked:
bluebirds1984
1 Solution
 
lotusnotesnewbieCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now