SQL Server上的数据没有从Web页面更新

本文关键字:Web 更新 Server 数据 SQL | 更新日期: 2023-09-27 18:18:24

我最近开始学习c# . net。我有一些麻烦更新我的sql服务器数据库。这个页面只是一个基本的"创建帐户"。用户提交用户名、密码和电子邮件。程序获取它并将其放入一个包含UserName、Password和Email列的表中。问题是它没有写入数据库。我是非常新的。net(从Java过渡)和任何输入是非常感谢。

c#代码:

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代码:
<%@ 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>

SQL Server上的数据没有从Web页面更新

您需要使用SQL Profiler来查看传递给SQL server的SQL查询。如果您可以在Sql Server管理工作室(SMSS)或任何工具中运行该查询,并且数据更新,那么您将需要在代码中查找错误。

如果直接通过SMSS运行时没有更新表,则需要调试查询。