注册多次插入MS Access数据库

本文关键字:Access 数据库 MS 插入 注册 | 更新日期: 2023-09-27 18:26:14

我的注册以前运行得很好,但后来我对它进行了多次修改,所以我找不出是什么导致了这个问题。。。

当我输入详细信息并单击"注册"时,使用asp.net本地服务器处理该页面需要很长时间。然后它最后说我已经注册了,但已经在数据库中输入了10-30个条目(有些条目甚至不相同,缺少不同的字段等)

我可以清除数据库中所有创建的条目,然后重新打开它,它又添加了10-30个。。。它会一直这样做,直到我"不调试就启动"时杀死它创建的asp.net开发服务器。

这是页面的c#文件:

using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            //Display welcome message
            lblMessage.Text = "Welcome " + txtFirstName.Text + "!";
            //Save the registration data into database, 17/8/2014
            SaveRegistration();
            sendEmail();
        } 
    }
    private void SaveRegistration()
    {
        OleDbConnectionStringBuilder sb = new OleDbConnectionStringBuilder();
        sb.Provider = "Microsoft.ACE.OLEDB.12.0";
        //sb.DataSource = Server.MapPath("/carpec02/asp_assignment/App_Data/shoeDB.accdb");
        sb.DataSource = Server.MapPath("~/App_Data/shoeDB.accdb");
        OleDbConnection myConnection = new OleDbConnection(sb.ConnectionString);
        string queryString = "";
        OleDbCommand myCmd = new OleDbCommand(queryString, myConnection);
        //Open connection
        myConnection.Open();
        //Build the query string
        StringBuilder queryStringBuilder = new StringBuilder("Insert into customer([customerFirstName], [customerLastName], [customerPassword], [customerPhHome], [customerPhWork], [customerPhMobile], [customerEmail], [customerPrivilege], [customerUsername])");
        queryStringBuilder.Append("values ('");
        queryStringBuilder.Append(txtFirstName.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtLastName.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtPassword.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtPhHome.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtPhWork.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtPhMobile.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtEmail.Text);
        queryStringBuilder.Append("','");
        queryStringBuilder.Append("User"); 
        queryStringBuilder.Append("','");
        queryStringBuilder.Append(txtUsername.Text);
        queryStringBuilder.Append("')");
        queryString = queryStringBuilder.ToString();
        //Assign the QueryString to the command object
        myCmd.CommandText = queryString;
        //Execute the query
        myCmd.ExecuteNonQuery();
        //Create another command object to display the inserted record ID
        OleDbCommand anotherCmd = new OleDbCommand("SELECT @@IDENTITY", myConnection);
        int numId = Convert.ToInt32(anotherCmd.ExecuteScalar());
        lblDataId.Text = "<h3>Your registration number is <big>" + numId.ToString() + "</big></h3>";
        //Close the connection
        myConnection.Close();
    }
    //Modified from http://www.aspsnippets.com/Articles/How-to-create-Contact-Us-Page-in-ASPNet.aspx
    protected void sendEmail()
    {
        try
        {
            string strReceiver = txtEmail.Text;
            MailMessage mm = new MailMessage("fakeemail@gmail.com", strReceiver); //Sender / receiver
            mm.Subject = "Welcome to Awesome Shoes!";
            mm.Body = "Thank you, " + txtFirstName.Text + " " + txtLastName.Text + ", for registering at Awesome Shoes. Your username is: " + txtUsername + " and your password is " + txtPassword;
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "fakeemail@gmail.com";
            NetworkCred.Password = "fake123";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
            lblMessage2.Text = "Email Sent Sucessfully.";
        }
        catch
        {
        }
    }
}

这是该页面的asp.net代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/AwesomeShoes.master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TabTitle" Runat="Server">
Register - Awesome Shoes
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Head" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentTabRegister" Runat="Server">
class="currentTab"
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainTitle" Runat="Server">
Register
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="MainBody" Runat="Server">
<script type="text/javascript">
    function IsInteger(input) {
        var regEx = /^'+{0,1}'d+'d*$/;
        return regEx.test(input);
    }
    function btnRegister_OnClick() 
    {
        var txtFirstName = document.getElementById('MainBody_txtFirstName');
        if (txtFirstName.value.length == 0) 
        {
            alert("You must enter all the required information before submitting");
            return;
        }
        var txtEmail = document.getElementById('MainBody_txtEmail');
        if (txtEmail.value.indexOf('@') == -1) 
        {
            alert("You must enter an @ for your email address.");
            return;
        }
        var txtPassword = document.getElementById('MainBody_txtPassword');
        if (txtPassword.value.length < 6) {
            alert("Your password must be at least 6 characters long");
            return;
        }
        var txtRepeatPassword = document.getElementById('MainBody_txtRepeatPassword');
        if (txtRepeatPassword.value != txtPassword.value) {
            alert("Your password does not match");
            return;
        }
        var txtPhHome = document.getElementById('MainBody_txtPhHome');
        var txtPhWork = document.getElementById('MainBody_txtPhWork');
        var txtPhMobile = document.getElementById('MainBody_txtPhMobile');
        if ((!IsInteger(txtPhHome.value)) && (!IsInteger(txtPhWork.value)) && (!IsInteger(txtPhMobile.value))) 
        {
            alert('You must enter at least one phone number.');
            return;
        }
        if (IsInteger(txtPhHome.value)) 
        {
            if (txtPhHome.value < 1000000 || txtPhHome.value > 9999999) {
                alert('The home phone number you entered is invalid. It must be between 100-0000 and 999-9999');
                return;
            }
        }
        if (IsInteger(txtPhWork.value)) {
            if (txtPhWork.value < 1000000 || txtPhWork.value > 9999999) {
                alert('The work phone number you entered is invalid. It must be between 100-0000 and 999-9999');
                return;
            }
        }
        if (IsInteger(txtPhMobile.value)) {
            if (txtPhMobile.value < 0210000000 || txtPhMobile.value > 0299999999) {
                alert('The mobile phone number you entered is invalid. It must be between 021-000-0000 and 029-999-9999');
                return;
            }
        }
        document.getElementById('form1').submit();
    }
</script>
<div>
    <table>
    <tr><td>First Name:</td><td><asp:TextBox ID="txtFirstName" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>last Name:</td><td><asp:TextBox ID="txtLastName" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Username:</td><td><asp:TextBox ID="txtUsername" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Password:</td><td><asp:TextBox ID="txtPassword" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Repeat Password:</td><td><asp:TextBox ID="txtRepeatPassword" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Email:</td><td><asp:TextBox ID="txtEmail" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Home Phone:</td><td><asp:TextBox ID="txtPhHome" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Work Phone:</td><td><asp:TextBox ID="txtPhWork" Width="150" runat="server"></asp:TextBox></td></tr>
    <tr><td>Mobile Phone:</td><td><asp:TextBox ID="txtPhMobile" Width="150" runat="server"></asp:TextBox></td></tr>
    </table>
    <input type="button"  class="caption" value="Register" id="btnRegister" name="btnRegister" onclick="btnRegister_OnClick()"/>
    <br/>
    <asp:Label ID="lblMessage" runat="server" class="message"></asp:Label>
    <br/>
    <asp:Label ID="lblMessage2" runat="server" class="message"></asp:Label>
    <br/>
    <asp:Label ID="lblDataId" runat="server"></asp:Label>
</div>
</asp:Content>

感谢任何关于为什么会发生这种情况的帮助,因为任务将于今天晚些时候完成。

注册多次插入MS Access数据库

与其在页面加载中传递这些方法,不如将它们作为.写入按钮单击事件

protected void btnRegister_Click(object sender, EventArgs e)
{
    //Save the registration data into database, 17/8/2014
      SaveRegistration();
      sendEmail();
}

将Register按钮的html标记更改为,

<asp:Button type="button"  CssClass="caption" Text="Register" ID="btnRegister" onclick="btnRegister_Click" onClientClick="javascript:btnRegister_OnClick();"/>