Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 28th, 2007, 09:28 AM
Authorized User
 
Join Date: Sep 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parameter Collection

Hi,
I have a problem, I am trying to execute an insert into my database and I am doing that using the protected void CreateUserWizard1_CreatingUser()function of my CREATEUSERWIZARD control. I am using a stored procedure which name is "usp_EmployeeUserNameInsert". This stored procedure requires a parameter which is @Username and I have already assigned it a value. When I try to run my application it returns me this error:"An SqlParameter with ParameterName '@Username' is not contained by this SqlParameterCollection." Do I have to create a parameter collection? Where do I create that and how? Thanks
This is the code I am using:
 protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["itworks"].ConnectionString);
        SqlParameter userParam = new SqlParameter("@Username", SqlDbType.VarChar);
        SqlCommand cmd = new SqlCommand("usp_EmployeeUserNameInsert", conn);
        cmd.CommandText = "usp_EmployeeUserNameInsert";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters["@Username"].Value = CreateUserWizard1.UserName;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }:)
 
Old September 28th, 2007, 10:52 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
SqlParameter userParam = new SqlParameter("@Username", SqlDbType.VarChar);
userParam.Value = CreateUserWizard1.UserName;
SqlCommand cmd = new SqlCommand("usp_EmployeeUserNameInsert", conn);
cmd.CommandText = "usp_EmployeeUserNameInsert";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(userParam);
You'll be better off abstracting the data access code to its own library, maintaining code in event handlers becomes very hard work.
We use the Microsoft Patterns and Practices Enterprise Library for Data Access. It makes this sort of thing much cleaner and easier.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter object malfunction - out parameter dash dev C# 2005 6 December 4th, 2007 12:58 PM
Can anybody help? Collection in C# naveenj .NET Framework 2.0 4 July 15th, 2007 09:34 PM
Collection in C# ? dedex C# 1 January 14th, 2005 04:51 PM
COM Collection ben0027 Visual C++ 0 May 1st, 2004 05:19 AM
Help collection gone MikeB VS.NET 2002/2003 2 June 12th, 2003 02:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.