[英]Data is not being updated on SQL Server from Web Page
我最近开始使用C#.Net。 我在更新SQL Server数据库时遇到了一些麻烦。 此页面只是一个基本的“创建帐户”。 用户提交用户名,密码和电子邮件。 该程序将其放入带有“用户名”,“密码”和“电子邮件”列的表中。 问题是它没有写入数据库。 我对.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事件探查器来查看传递给sql服务器的SQL查询。 如果可以在Sql Server Management Studio(SMSS)或任何工具中运行该查询,然后进行数据更新,则需要在代码中查找错误。
如果直接通过SMSS运行时它不更新表,则需要调试查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.