槿汐369 2024-12-06 16:00 采纳率: 0%
浏览 9

asp.net网页问题

asp.net,用户注册模块,try()的不会执行会弹catch()的错误信息但是不知道哪错了。求解答。
注册界面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="WebApplication1.register" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 600px;
        }
        .auto-style2 {
            width: 100%;
        }
        .auto-style3 {
            width: 100px;
        }
        .auto-style4 {
            width: 243px;
        }
        .auto-style5 {
            width: 100px;
            text-align: right;
        }
        .auto-style6 {
            width: 100px;
            text-align: right;
            height: 23px;
        }
        .auto-style7 {
            width: 243px;
            height: 23px;
        }
        .auto-style8 {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="楷体" Font-Size="XX-Large" ForeColor="Blue" Text="用户注册"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <table class="auto-style2">
                        <tr>
                            <td class="auto-style5">用户名:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_User_Name" runat="server" TabIndex="1" Width="234px" AutoPostBack="True" OnTextChanged="txt_User_Name_TextChanged"></asp:TextBox>
                            </td>
                            <td>*<asp:Button ID="btn_Check" runat="server" TabIndex="2" Text="检测用户名" OnClick="btn_Check_Click" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_User_Name" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style6">密码:</td>
                            <td class="auto-style7">
                                <asp:TextBox ID="txt_User_Pwd" runat="server" TabIndex="3" TextMode="Password" Width="234px"></asp:TextBox>
                            </td>
                            <td class="auto-style8">*<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_User_Pwd" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style5">确认密码:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_ReUser_Pwd" runat="server" TabIndex="4" TextMode="Password" Width="234px"></asp:TextBox>
                            </td>
                            <td>*<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_ReUser_Pwd" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_User_Pwd" ControlToValidate="txt_ReUser_Pwd" ErrorMessage="两次输入的密码不一致"></asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style5">姓名:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_Rel_Name" runat="server" TabIndex="5" Width="234px"></asp:TextBox>
                            </td>
                            <td>*<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txt_Rel_Name" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style5">性别:</td>
                            <td class="auto-style4">
                                <asp:DropDownList ID="DDL_Sex" runat="server" TabIndex="6">
                                    <asp:ListItem></asp:ListItem>
                                    <asp:ListItem></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="auto-style5">出生日期:</td>
                            <td class="auto-style4">
                                <asp:DropDownList ID="DDL_Year" runat="server" TabIndex="7">
                                </asp:DropDownList><asp:DropDownList ID="DDL_Month" runat="server" TabIndex="8">
                                </asp:DropDownList><asp:DropDownList ID="DDL_Day" runat="server" TabIndex="9">
                                </asp:DropDownList></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="auto-style6">联系地址:</td>
                            <td class="auto-style7">
                                <asp:TextBox ID="txt_Address" runat="server" TabIndex="10" Width="234px"></asp:TextBox>
                            </td>
                            <td class="auto-style8"></td>
                        </tr>
                        <tr>
                            <td class="auto-style5">邮政编码:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_PostalCode" runat="server" TabIndex="11" Width="234px"></asp:TextBox>
                            </td>
                            <td>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_PostalCode" ErrorMessage="邮政编码格式不符" ValidationExpression="\d{6}"></asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style5">联系电话:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_Tel" runat="server" TabIndex="12" Width="234px"></asp:TextBox>
                            </td>
                            <td>*<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txt_Tel" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txt_Tel" ErrorMessage="联系电话格式不符" ValidationExpression="(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}"></asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style5">手机:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_Mobile" runat="server" TabIndex="13" Width="234px"></asp:TextBox>
                            </td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="auto-style5">身份证号:</td>
                            <td class="auto-style4">
                                <asp:TextBox ID="txt_ID_Card" runat="server" TabIndex="14" Width="234px"></asp:TextBox>
                            </td>
                            <td>*<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txt_ID_Card" ErrorMessage="此项必填"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txt_ID_Card" ErrorMessage="身份证号格式不符" ValidationExpression="\d{17}[\d|X]|\d{15}"></asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style3">&nbsp;</td>
                            <td class="auto-style4">
                                <asp:Button ID="btn_Register" runat="server" TabIndex="15" Text="注册" OnClick="btn_Register_Click" />
                                <asp:Button ID="btn_Catch" runat="server" TabIndex="16" Text="重填" OnClick="btn_Catch_Click" />
                                <asp:Label ID="Label2" runat="server" Font-Size="Smaller" ForeColor="Red" Text="带*的为必填项"></asp:Label>
                            </td>
                            <td>&nbsp;</td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Labinfo" runat="server" Font-Size="Smaller" ForeColor="Red"></asp:Label>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>



实现注册功能代码

using System;
using System.Data;//内存数据库的命名空间
using System.Data.SqlClient;//SQL Server命名空间
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;//加密密码命名空间
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
            if (Page.IsPostBack == false)
            {
                for (int year = 1970; year <= 2024; year++)
                    DDL_Year.Items.Add(year.ToString());
                for (int month = 1; month < 13; month++)
                    DDL_Month.Items.Add(month.ToString());
                for (int day = 1; day < 32; day++)
                    DDL_Day.Items.Add(day.ToString());
            }
            btn_Check.CausesValidation = false;
            btn_Catch.CausesValidation = false;
        }

        protected void btn_Check_Click(object sender, EventArgs e)
        {   
            SqlConnection Con = new SqlConnection();//1.连接对象
            Con.ConnectionString = "server=.;database=网上听歌;uid=sa;pwd=123";

            SqlCommand Com = new SqlCommand();//2.命令对象
            Com.Connection = Con;//对谁做
            Com.CommandText = "select * from 会员表 where 会员名='" + txt_User_Name.Text + "'";
            Response.Write(Com.CommandText);//测试语句

            SqlDataAdapter Da = new SqlDataAdapter();//.3.数据适配器对象
            Da.SelectCommand = Com;//接管命令对象控制权(查看数据库的控制权)

            DataSet Ds = new DataSet();//4.声明内存数据库

            try
            {
                Con.Open();//打开连接
                Ds.Clear();//清空内存数据库
                Da.Fill(Ds, "会员表");//把上述查询结果对应的二维表填充到内存数据库,命名为【会员表】
                Con.Close();//离线访问开始
                if (Ds.Tables["会员表"].Rows.Count != 0)//说明这个名字被占用了
                    Labinfo.Text = "对不起,此用户名已被注册,请输入其它用户名";
                else
                    Labinfo.Text = "恭喜您,此用户名可以使用!";
            }
            catch (Exception)
            {
                Labinfo.Text = "请检查连接字符串或SQL语句书写是否正确!";
                Con.Close();
            }

        }

        protected void txt_User_Name_TextChanged(object sender, EventArgs e)
        {
            //1.创建一个数据库工具
            DB db = new DB();
            //2.准备一个SQL语句,作为数据库工具的参数
            string SqlStr = "select * from 会员表 where 会员名='" + txt_User_Name.Text + "'";
            Response.Write(SqlStr);

            //3.定义内存数据库Ds,接收查询结果
            DataSet Ds = new DataSet();
            try
            {
                Ds.Clear();//清空内存数据库,准备接收数据
                Ds = db.GetDataTableBySql(SqlStr);//获取数据的实际过程
                if (Ds.Tables[0].Rows.Count == 0)
                    Labinfo.Text = "恭喜!,此用户名可以使用!";
                else
                    Labinfo.Text = "此用户名已经被占用!";
            }
            catch (Exception)
            {
                Labinfo.Text = "没有得到任何数据,请检查SQL语句";
            }
          
        }

        protected void btn_Catch_Click(object sender, EventArgs e)
        {
            txt_User_Name.Focus();//用户名所在文本框获得输入焦点(光标)
            txt_Address.Text = "";
            txt_ID_Card.Text = "";
            txt_Mobile.Text = "";
            txt_PostalCode.Text = "";
            txt_Rel_Name.Text = "";
            txt_ReUser_Pwd.Text = "";
            txt_Tel.Text = "";
            txt_User_Name.Text = "";
            txt_User_Pwd.Text = "";
        }

        protected void btn_Register_Click(object sender, EventArgs e)
        {
            string Md5_User_Pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txt_User_Pwd.Text, "MD5");
            DB db = new DB();//1.声明一个具体的工具
            string SqlStr = "insert into 会员表(会员名,密码,姓名,性别,出生日期,联系地址,邮政编码,联系电话,手机,身份证号) values('" + txt_User_Name.Text + "','" + Md5_User_Pwd + "','" + txt_Rel_Name.Text + "','" + DDL_Sex.SelectedItem.Text + "','" + DDL_Year.SelectedItem.Text + "-" + DDL_Month.SelectedItem.Text + "-" + DDL_Day.SelectedItem.Text + "','" + txt_Address.Text + "','" + txt_PostalCode.Text + "','" + txt_Tel.Text + "','" + txt_Mobile.Text + "','" + txt_ID_Card.Text + "')";
            Boolean InsertResult;//布尔型数据,代表数据更新是否成功
            InsertResult = db.UpdateDataBySql(SqlStr);
            //Response.Write(SqlStr);
            if (InsertResult == true)
                Labinfo.Text = "注册成功!";
            else
            {
                Labinfo.Text = "注册失败!";
                txt_User_Name.Focus();//光标定位到用户名框
            }

           
        }
    }
}

  • 写回答

1条回答 默认 最新

  • 越重天 新星创作者: Java技术领域 2024-12-06 21:13
    关注

    本答案参考 多次询问 ChatGPT-3.5 后选择的最佳回答,若是满意,还请采纳,谢谢

    从您提供的代码来看,在 btn_Check_Click 方法和 txt_User_Name_TextChanged 方法的 try 块中都进行了数据库操作,然后在 catch 块中提示检查连接字符串或 SQL 语句书写是否正确以及没有得到任何数据,请检查 SQL 语句。但是仅从这段代码中,很难确切地判断出具体是哪里出现了问题导致 try 块中的操作没有成功执行而进入了 catch 块。

    一些可能的排查方向:

    1. 确认数据库的连接是否正常。可以检查连接字符串中的服务器名称、数据库名称、用户名和密码是否正确,以及数据库服务是否正在运行。
    2. 检查 SQL 语句的正确性。确保在查询会员表时,表名和列名的拼写正确,以及查询条件的逻辑正确。
    3. 查看数据库中是否存在相关的数据,以验证查询结果是否符合预期。例如,检查会员表中是否真的存在与输入的用户名相同的记录,或者是否存在其他可能影响查询结果的因素。

    如果您能够提供更多的错误信息或上下文,例如具体的错误提示、数据库的状态等,将有助于更准确地诊断问题所在。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月6日