Storing additional user information i MSSQL using the CreateUserWizard

Posted on 2006-06-15
Last Modified: 2008-01-09
Hi there!

I am trying to store some user information using the CreateUserWizard in addition to the information which is otherwise stored in the membershipdb ASPNETDB.mdf (First name, last name, address etc.). This information will be stored in another MSSQL2005-database.

The strategy is to gather the information from a number of textboxes in a CreateUserWizard and add the information to a table.

The code below shows what I've got. Unfortunately I get the following error messages

The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)

The name 'ConnectionString' does not exist in the current context.

Can anyone explain this (and give a sollution)?

protected void CreateUserWizard1_CreatingUser(object sender, EventArgs e)
        SqlConnection myConnection = new SqlConnection(ConnectionString);

        string Q_ADD_USER = @"INSERT INTO Users(UserName, FirstName, LastName) VALUES (@UserName, @FirstName, @LastName)";

        SqlCommand myCommand = new SqlCommand(Q_ADD_USER, myConnection);

        string userName = CreateUserWizard1.UserName;

        string firstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtFirstName")).Text;
        string lastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtLastName")).Text;

        myCommand.Parameters.AddWithValue("@UserName", userName);
        myCommand.Parameters.AddWithValue("@FirstName", firstName);
        myCommand.Parameters.AddWithValue("@LastName", lastName);
Question by:RunePerstrup
  • 5
  • 4

Author Comment

ID: 16912882

The second error message goes like this:

The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?)
LVL 52

Expert Comment

by:Carl Tawn
ID: 16913008
Sound like you are missing the "using" directive for the System.Data.SqlClient namespace. Add the following at the top of your code file:

     using System.Data.SqlClient;

Or, fully-qualify your object types in code:

      System.Data.SqlConnection myConnection = new System.Data.SqlConnection(ConnectionString);

Author Comment

ID: 16918888
Great start. Seams like I still have a lot to learn...

For some reason I can't get the compiler to accept the "using" anywhere in the code, because it is an "invalid token"... I can still use the fully-qualify your object types though.

Anyway, I still have a problem with the connection string. It says "The name 'ConnectionString' does not exist in the current context.". How can I access the correct context?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 52

Expert Comment

by:Carl Tawn
ID: 16919059
Presumably you have ConnectionString defineds as a variable somewhere. Where is it in relation to the code you posted ?


    using System.Data.SqlClient;

needs to go at the very top of the code file, outside of any classes/methods, etc.

Author Comment

ID: 16919141
The connectionstring is defined in the web.config file - and I am trying to access it from my code which is executed when a CreateUserWizard1_CreatingUser occurs in a web page.
LVL 52

Expert Comment

by:Carl Tawn
ID: 16919157
.Net 1.1 or 2.0 ?

Author Comment

ID: 16919164
LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 16919181
In that case you need to use:

        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ConnectionString);

Author Comment

ID: 16919283
That is just so wonderful!

Thank you!

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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