none
关于 注册模块 无法成功注册 RRS feed

  • 问题

  • 请问:我按教程在做一个注册的倒子,然后调试的时候老是提示是 注册错误,我贴上代码,请帮看下一吧。谢谢
    ASPX:


    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <link href ="css.css" rel ="Stylesheet" type ="text/css" />
    <style type ="text/css" >

    .style1
    {
     width :100%
    }

    .style2
    {
    }

    .style3
    {
     width :175px;
    }

    .style4
    {
     color :#ff3300;
    }

    .style5
    {
     color :#ff0000;
    }

    .style6
    {
     width :102px;
    }

    </style>
        <title>注册</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div class ="top "></div>
        <div class ="register ">
        <table class ="style1 " >
        <tr>
            <td>用户名:</td><td class ="style3">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </td><td>&nbsp;<span class  ="style4">*</span>&nbsp;
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                ControlToValidate="TextBox1" ErrorMessage="必须输入用户名!"></asp:RequiredFieldValidator>
                        </td>
        </tr>
        <tr>
            <td>密码:</td><td class ="style3">
                        <asp:TextBox ID="TextBox2" runat="server" TextMode  ="Password" ></asp:TextBox>
                        </td><td>&nbsp;<span class ="style4">*</span>&nbsp;
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                ControlToValidate="TextBox2" ErrorMessage="必须输入密码"></asp:RequiredFieldValidator>
                        </td>
        </tr>
        <tr>
            <td>性别:</td><td class ="style3">
                        <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem>帅哥</asp:ListItem>
                            <asp:ListItem>美女</asp:ListItem>
                            <asp:ListItem>保密</asp:ListItem>
                        </asp:DropDownList>
                        </td><td> &nbsp;<span class ="style4">*</span>&nbsp;
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                                ControlToValidate="DropDownList1" ErrorMessage="必须选择性别!"></asp:RequiredFieldValidator>
                        </td>
        </tr>
        <tr>
            <td>头像:</td><td class ="style3">
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        </td>
        </tr>
        <tr>
            <td>IM:</td><td class ="style3">
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                        </td>
        </tr>
        <tr>
            <td>个性签名:</td><td class ="style3">
                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                        </td>
        </tr>
        <tr>
            <td>备注:</td><td class ="style3">
                        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                        </td>
        </tr>
        <tr>
            <td colspan ="3">
                <asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" />
                <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        </table>
        </div>
        <div class ="end ">版权信息 本网站不会将用户信息泄露给任何人或机构!</div>
        </form>
    </body>
    </html>

     


    ASPX.CS:

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool Check(string text)
        {
            if (text.Contains("<") || text.Contains(">") || text.Contains("'") || text.Contains("//") || text.Contains("\\"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Check(TextBox1.Text ) || Check(TextBox2.Text ) || Check(TextBox3.Text ) || Check(TextBox4.Text ) || Check(TextBox5.Text )
                || Check(TextBox6.Text ))
            {
                Label8.Text = "注册错误,含有非法字符,请重新输入!";
            }
            else
            {

                try
                {
                    SqlConnection con = new SqlConnection("server='(local)';database='Register';uid='sa';pwd='sa'");
                    con.Open();
                    string strsql = "insert into Register(username,password,sex,picture,im,information,others,ifisuser) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + DropDownList1.Text  + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "',0)";
                    SqlCommand cmd = new SqlCommand(strsql, con);
                    cmd.ExecuteNonQuery();
                    Label8.Text = "注册成功,请牢记你的注册信息!";
                }
                catch
                {
                    Label8.Text = "注册失败,请重新注册!";
                }
            }
        }
    }

     

    sql:

    USE [Register]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Register](
     [id][int] IDENTITY(1,1) NOT NULL,
     [username][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL ,
     [password][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
     [sex][int] NULL,
     [picture][nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
     [IM][nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
     [information][nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
     [others][nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
     [ifisuser][int] NULL,
     CONSTRAINT [PK_Register] PRIMARY KEY CLUSTERED
    (
     [id] ASC
    )
     WITH (PAD_INDEX =OFF ,STATISTICS_NORECOMPUTE=OFF ,IGNORE_DUP_KEY=OFF , ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON[PRIMARY]

    )ON[PRIMARY]

     

    注册时,填入注册信息,老是提示:注册失败,请重新注册!

    2010年12月9日 7:10

答案

  • 您好,

    1、在SqlConnection con = new SqlConnection("server='(local)';database='Register';uid='sa';pwd='sa'");
    这句设置断点,然后单步跟踪,看哪里出错,我贴一个数据库连接供参考:"Data Source=(local);Initial Catalog=Register; Integrated Security=SSPI;"

    2、将运行时构建的strsql值,放到Sql server中执行,看看会不会出错。

    2010年12月9日 7:31
    版主
  • 完整点的例子道理都是一样的

    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MengXianHui.mdb;Persist Security Info=True";
    string QueryString = "Insert Into [Document] (Title, Content, Author, CreateDate) Values(@Title, @Content, @Author, @CreateDate)";
    OleDbConnection cn = new OleDbConnection(ConnectionString);
    cn.Open();
    OleDbCommand cmd = new OleDbCommand(QueryString, cn);
    cmd.Parameters.AddWithValue("@Title", Title);
    cmd.Parameters.AddWithValue("@Content", Content);
    cmd.Parameters.AddWithValue("@Author", Author);
    cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
    cmd.ExecuteNonQuery();
    cn.Close();
    cn.Dispose();
    
    

     


    【孟子E章】
    2010年12月9日 14:08
    版主

全部回复

  • 您好,

    1、在SqlConnection con = new SqlConnection("server='(local)';database='Register';uid='sa';pwd='sa'");
    这句设置断点,然后单步跟踪,看哪里出错,我贴一个数据库连接供参考:"Data Source=(local);Initial Catalog=Register; Integrated Security=SSPI;"

    2、将运行时构建的strsql值,放到Sql server中执行,看看会不会出错。

    2010年12月9日 7:31
    版主
  • 你是说

    "注册错误,含有非法字符,请重新输入!";

    这个老出现么 ,那一定是if条件满足了才提示 的啊


    【孟子E章】
    2010年12月9日 14:04
    版主
  • SQL操作应该使用参数化

    sql = "insert into Register(username,xxx) Values(@username,@xxx)";

    cmd.Parameters.AddWithValue("@username",TextBox1.Text);

    cmd.Parameters.AddWithValue("@xxx",TextBox2.Text);

     

    ....

    cmd.ExecuteNonQuery();


    【孟子E章】
    2010年12月9日 14:06
    版主
  • 完整点的例子道理都是一样的

    string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MengXianHui.mdb;Persist Security Info=True";
    string QueryString = "Insert Into [Document] (Title, Content, Author, CreateDate) Values(@Title, @Content, @Author, @CreateDate)";
    OleDbConnection cn = new OleDbConnection(ConnectionString);
    cn.Open();
    OleDbCommand cmd = new OleDbCommand(QueryString, cn);
    cmd.Parameters.AddWithValue("@Title", Title);
    cmd.Parameters.AddWithValue("@Content", Content);
    cmd.Parameters.AddWithValue("@Author", Author);
    cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
    cmd.ExecuteNonQuery();
    cn.Close();
    cn.Dispose();
    
    

     


    【孟子E章】
    2010年12月9日 14:08
    版主