首页 > 解决方案 > 制作数据库和注册页面不添加信息将其放入数据库

问题描述

我一直在为一个项目制作数据库,当我提交信息时,它不会添加信息。我不知道问题出在cs页面还是aspx页面本身,我真的很绝望。提前谢谢。

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
     <script>
        function CheckAll()
{
  var flag = true;
  var length = document.getElementById('user').value.length;
  var value = document.getElementById('user').value;
  for (var i = 0; i < length; i++) {
    if (!((value.charAt(i) >= '0' && value.charAt(i) <= '9') || (value.charAt(i) >= 'a' && value.charAt(i) <= 'z') || (value.charAt(i) >= 'A' && value.charAt(i) <= 'Z'))) {
      alert("username must be made of characters and numbers only");
      return false;
    }
  }
  var lengthPass = document.getElementById('password').value.length;
  var valuePass = document.getElementById('password').value;
  for (var i = 0; i < lengthPass; i++) {
    if (!((valuePass.charAt(i) >= '0' && valuePass.charAt(i) <= '9') || (valuePass.charAt(i) >= 'a' && valuePass.charAt(i) <= 'z') || (valuePass.charAt(i) >= 'A' && valuePass.charAt(i) <= 'Z'))) {
      alert("Password must be made of characters and numbers only");
      return false;
    }

  }
        if (!(length >= 6 || length <= 20))
        {
    alert("Username Must be 6-20 Characters long");
    return false;
  }
        if (!(lengthPass >= 6 || lengthPass <= 20))
        {
            alert("Password must be 6-20 characters long")
            return false;
        }


        var x = document.forms["form"]["mail"].value;
        var atpos = x.indexOf("@");
        var dotpos = x.lastIndexOf(".");
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
            alert("Not a valid e-mail address");
            return false;
        }
            var valuePhone = document.getElementById('phone').value;
            var lengthPhone = document.getElementById('phone').value.length;
            for (var i = 0; i < length; i++)
            {
                if (!((valuePhone.charAt(i) >= '0' || valuePhone.charAt(i) <= '9')))
                {
                    alert("phone number must be made of numbers only");
                    return false;
                }
            }
            if (lengthPhone != 10)
            {
                alert("phone number must be 10 numbers long");
            }



  return true;
}




    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <form id="form" method="post">
        Name+Last Name: <input type="text" name="name" />
        <br />
        <br /> Username: <input type="text" name="username" id="user" />
        <br />
        <p style="font-size:70%">must contain 6-20 character</p>
        <br /> Password: <input type="text" name="password" id="password" />
        <br />
        <p style="font-size:70%">must contain 6-20</p>
        <br /> E-mail: <input type="text" name="mail" id="mail" />
        <br /> Phone Number: <input type="text" name="phone" id="phone" />
        <br /> Enter Your Birth Date:
        <br /> 
        <input type="date" name="date" id="date"/>
        <br />
        <br /> Enter Your Favourite Esports Game:
        <select name="choose" id="choose">
            <option>League Of Legends</option>
            <option>Dota</option>
            <option>StarCraft</option>
            <option>Street Fighter</option>
            <option>Smite</option>
            <option>Mortal Kombat</option>
            <option>Heroes Of The Storm</option>
            <option>Super Smash Bros</option>
            <option>Call of Duty</option>
            <option>Counter Strike: Global Offensive</option>
            <option>I Dont Watch Esports</option>
        </select>

        <table border="1">
            <tr>
                <th>
                    <a href="HomePage.html" style="color:red">To Get Back To The Home Page</a>
                </th>
            </tr>
        </table>
        <br />
        <br />
        <input type="checkbox" name="agree" id="agree" value="check validity" onclick="AgreeCheck()" />
        <input type="submit" name="sub" id="sub" value="submit" disabled />
    </form>
    <script>
        function AgreeCheck() {
            document.getElementById("agree").checked = CheckAll();
            document.getElementById("sub").disabled = !CheckAll();
        }
    </script>
</asp:Content>

cs 页面是我们的老师给我们的,他告诉我们只需根据我们的变量更改内容,它会起作用,我不知道它是否可以,因为它不会将信息发送到数据库和 cs页:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = String.Empty;
        string fileName = "Database.mdf";
        // string fileName = "db.mdf";

        if (Request.Form["submit"] != null)
        {
            {
                string name = Request.Form["name"];
                string user = Request.Form["user"];
                string password = Request.Form["passwod"];
                string mail = Request.Form["mail"];
                string phone = Request.Form["phone"];
                string date = Request.Form["date"];
                string choose = Request.Form["choose"];
                string agree = Request.Form["agree"];

                    sql = "INSERT INTO personalTable(name, user, password,mail,phone,date,choose,agree)";
                    sql += "VALUES('" + name + "','" + user + "','" + password +"','"+mail+"','"+phone+"','"+date+"','"+choose+"','"+agree+ "')";
                    Response.Write(sql);
                    Response.Write("<br/>");

                    MyAdoHelper.DoQuery(fileName, sql);






            }
        }

    }
}

标签: c#asp.netdatabase

解决方案


推荐阅读