Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • 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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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