2011-07-18 6 views
0

私はasp.netメンバーシップ・プロバイダが提供する情報とは別に、mysqlデータベースに追加のユーザー情報を格納するためにcreateuserwizardを使用しています。この情報を 'userprofile'という別のテーブルに保存したいと思います。 'userprofile'のユーザーIDはint型で、 'my_aspnet_users'の 'id'を参照する外部キーです。mysqlデータベース用のcreateuserwizardからユーザーIDを取得する方法は?

これは私の問題です: 新しく作成されたユーザーのユーザーIDを私の 'userprofile'テーブルに保存することはできません。これらの行は動作しません:

 MembershipUser newUser = Membership.GetUser(RegisterUser.UserName); 
     Int32 newUserId = (Int32)newUser.ProviderUserKey;    

これらの行は、ユーザ名はとても奇妙ですProviderUserKeyで返されるGUID値に設定することが原因となっています!一方、my_aspnet_usersのidカラムは、ユーザの作成時に1ずつ増加します。また、 'userprofile'テーブルに何も追加されません。

 protected void RegisterUser_CreatedUser(object sender, EventArgs e) 
    { 
     FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */); 

     string continueUrl = RegisterUser.ContinueDestinationPageUrl; 
     if (String.IsNullOrEmpty(continueUrl)) 
     { 
      continueUrl = "~/"; 
     } 

     // Retrieve all the values from the registration form to insert into database 
     TextBox FirstNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("FirstName"); 
     TextBox LastNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("LastName"); 
     TextBox UsernameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("UserName"); 
     TextBox EmailTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email"); 

     // Get the UserId of the just-added user 
     MembershipUser newUser = Membership.GetUser(RegisterUser.UserName); 
     Int32 newUserId = (Int32)newUser.ProviderUserKey;    

     MySqlConnection DBConn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString); 
     MySqlCommand DBCmd = new MySqlCommand(); 

     try 
     { 
      // Add Insert statement to insert into database 
      DBCmd = new MySqlCommand(
       "INSERT INTO userprofile(UID, Email, Fname, Lname)" + 
       "VALUES (@UID, @Email, @Fname, @Lname)", DBConn); 

      // Add database parameters 
      DBCmd.Parameters.Add("@UID", MySqlDbType.Int32).Value = newUserId; 
      DBCmd.Parameters.Add("@Email", MySqlDbType.VarChar).Value = EmailTextBox.Text; 
      DBCmd.Parameters.Add("@Fname", MySqlDbType.VarChar).Value = FirstNameTextBox.Text; 
      DBCmd.Parameters.Add("@Lname", MySqlDbType.VarChar).Value = LastNameTextBox.Text; 
      DBCmd.ExecuteNonQuery(); 
     } 

     catch(Exception exp){ 
      Response.Write(exp); 
     } 

     // Close database connection and dispose database objects 
     DBCmd.Dispose(); 
     DBConn.Close(); 
     DBConn = null; 

     Response.Redirect(continueUrl); 

    } 

誰でもMySQLと追加情報についてcreateuserwizardと協力してきました:

これは私が上記のコードを挿入どこにあるのでしょうか?

ご協力いただければ幸いです。ありがとう!

答えて

0

上記のコードに間違いはありません。私はここで問題を引き起こしていた別の関数でユーザID値を使いこなしていました!

1

あなたのSQL文は、新しく作成されたID値を返していません。これは、insert文を実行すると自動的には返されません。

+0

お返事ありがとうございます。次にID値を取得するにはどうすればよいですか? – coder

関連する問題