Posted: 6/24/2011
First, I am complete novice trying to complete my first web application. I am using visual web developer 2010. I have gotten a bunch of great help here. I am hoping someone can help me with this one.
I customized the create user control by adding a previous step. This was to collect billing information: First name, Last name, Address1, Address2, City, State, Zip Code. All but the State are Text boxes. State is a drop down list. I added a table to the ASPNETDB. I called it User_Signup. These are the fields: UserId uniqueidentifier, FirstName varchar(50), LastName varchar(50), Address1 varchar(50), Address2 varchar(50), City varchar(50), ZipCode varchar(25), State varchar(25). The only one that allows nulls is Address2. I made a primary key/foreign key relationship between UserId on my User_Signup table and the UserId in the aspnet_User table.
This is the code for my Sqldatasource control:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBconnectionstring %>" InsertCommand="INSERT INTO [User_Signup] ([UserId], [FirstName], [LastName], [Address1], [Address2], [City], [ZipCode], [State]) VALUES (@UserId, @FirstName, @LastName, @Address1, @Address2, @City, @ZipCode, @State)" ProviderName="<%$ ConnectionStrings:ASPNETDBconnectionstring.ProviderName %>"> <InsertParameters> <asp:ControlParameter Name="FirstName" Type="String" ControlID="FnTxtBox" PropertyName="Text" /> <asp:ControlParameter Name="LastName" Type="String" ControlID="LnTxtBox" PropertyName="Text" /> <asp:ControlParameter Name="Address1" Type="String" ControlID="Add1TxtBox" PropertyName="Text" /> <asp:ControlParameter Name="Address2" Type="String" ControlID="Add2TxtBox" PropertyName="Text" /> <asp:ControlParameter Name="City" Type="String" ControlID="CityTxtBox" PropertyName="Text" /> <asp:ControlParameter Name="ZipCode" Type="String" ControlID="ZcTxtBox" PropertyName="Text" /> <asp:ControlParameter Name="State" Type="String" ControlID="StateDropDown" PropertyName="Text" /> </InsertParameters> </asp:SqlDataSource>
This is the C# code for my event handler:
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { TextBox UserNameTextBox = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"); SqlDataSource DataSource = (SqlDataSource)CreateUserWizardStep1.ContentTemplateContainer.FindControl("InsertExtraInfo"); MembershipUser User = Membership.GetUser(UserNameTextBox.Text); object UserGUID = User.ProviderUserKey; DataSource.InsertParameters.Add("UserId", UserGUID.ToString()); DataSource.Insert(); }
I do not get any errors, but the data collected does not get written back to the database. I thought maybe it all would except the "State" because it is in a drop down list. Could the drop down list be causing none of it to be written back. All the fields in the table show "null' after I create a new user. The aspnet_Users get updated fine. Do I need to change the Type or PropertyName because of the Drop down list? What do I need to do to get this to update the Db?
Thank you in advance.
Posted: 6/25/2011
Hi Mike,
Try to check what result you get from the Insert() method?
Add this:
int result = InsertExtraInfo.Insert(); if (result != -1) { Response.Write("Successfully inserted!"); }
And tell me what is the number in 'result' integer variable?
Regards,Hajan