简体   繁体   中英

Data is not being updated on SQL Server from Web Page

I recently started on C#.Net. I am having some trouble with updating my sql server database. This page is just a basic "Create an account". The user submits username,password and email. The program takes it and puts it into a table with UserName, Password, and Email columns. The Problem is that its not writing to the database. I am VERY new to .net (transitioning from Java) and any input is greatly appreciated.

C# Code:

namespace WebApplication1.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];


        }

        protected void Button_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            if (btn.ID == "CreateUserButton")
            {

                String cmdText = "dbo.InsertUser";

                String connStr = ConfigurationManager.ConnectionStrings["PracticeConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();

                SqlCommand cmd = new SqlCommand(cmdText, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                FormView fv1 = new FormView();
                fv1.DataBind();

                try
                {
                    String userName = null, password = null, email = null;

                    TextBox Username = (TextBox)fv1.FindControl("UserName");
                    userName = Username.Text.ToString();
                    TextBox Email = (TextBox)fv1.FindControl("Email");
                    email = Email.Text.ToString();
                    TextBox Password = (TextBox)fv1.FindControl("Password");
                    password = Password.Text.ToString();

                    SqlDataSource src = new SqlDataSource();
                    src.DataBind();
                    src.InsertParameters.Clear();
                    src.InsertParameters.Add("UserName", TypeCode.String, userName);
                    src.InsertParameters.Add("Password", TypeCode.String, password);
                    src.InsertParameters.Add("Email", TypeCode.String, email);
                    src.Insert();

                    cmd.ExecuteNonQuery();

                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.StackTrace.ToString());
                }
                finally
                {
                    if (conn != null && conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn = null;
                        cmd = null;
                    }
                }


                Response.Redirect("~/HomePage.aspx");
            }

        }

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

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

        protected void OnInsert(object sender, EventArgs e)
        {

        }
        protected void onInserted(object sender, EventArgs e)
        {
            Response.BufferOutput = true;
            Response.Redirect("HomePage.aspx");
        }
    }
}

asp.net Code:

<%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Register.aspx.cs" Inherits="WebApplication1.Account.Register" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser">
        <LayoutTemplate>
            <asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder>
            <asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <WizardSteps>
            <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
                <ContentTemplate>
                    <h2>
                        Create a New Account
                    </h2>
                    <p>
                        Use the form below to create a new account.
                    </p>
                    <p>
                        Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length.
                    </p>
                    <span class="failureNotification">
                        <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
                    </span>
                    <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
                         ValidationGroup="RegisterUserValidationGroup"/>
                    <div class="accountInfo">
                        <fieldset class="register">
                            <legend>Account Information</legend>
                            <p>
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                                     CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                                <asp:TextBox ID="Email" runat="server" CssClass="textEntry"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" 
                                     CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                                     CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                                <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
                                     ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" 
                                     ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                     CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator>
                            </p>
                        </fieldset>
                        <p class="submitButton">
                            <asp:Button ID="CreateUserButton"  OnClick = "Button_Click" runat="server" CommandName="MoveNext" Text="Create User" 
                                        ValidationGroup="RegisterUserValidationGroup"/> 
                        </p>

                        <asp:SqlDataSource 
                            ID="src" 
                            runat="server"
                            ConnectionString="<%$ ConnectionStrings:PracticeConnectionString %>"  
                            InsertCommand= "InsertUser" 
                            InsertCommandType = "StoredProcedure" 
                            OnInserted = "Button_Click">
                            <InsertParameters>
                                <asp:Parameter Name="UserName" Type="String"/>
                                <asp:Parameter Name="Password" Type="String"/>
                                <asp:Parameter Name="Email" Type="String"/>
                            </InsertParameters>
                        </asp:SqlDataSource>

                        <asp:FormView ID = "fv1" runat="server" ></asp:FormView>

                    </div>
                </ContentTemplate>
                <CustomNavigationTemplate>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>

</asp:Content>

You will need to use SQL Profiler to view the SQL query being passed through to the sql server. If you can run that query in the Sql Server Management Studio (SMSS), or whatever tool, and the data updates then you will need to look for your error in the code.

If it does not update the table when run directly through SMSS then you will need to debug the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM