如果数据库中已存在值,如何进行错误验证? [英] How to have error validation if value exist already in the database?

查看:53
本文介绍了如果数据库中已存在值,如何进行错误验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框Web表单,即用户名,密码和IC号。如果输入的IC编号与数据库中的
IC匹配,则会将用户名和密码插入该记录中。它没有找到任何匹配的IC号,它不会插入任何记录,它会说IC不存在。目前lblErrorMessage.Text ="IC不存在"; catch异常中的
不起作用。如果输入的IC在数据库中不存在,它将重定向到已注册,即使如此。





using System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web .UI;

使用System.Web.UI.WebControls;

使用System.Data.SqlClient;

使用System.Configuration;

使用System.Web.Se curity;

使用System.Web.UI.HtmlControls;



公共部分类寄存器:System.Web.UI.Page

{

    protected void Page_Load(object sender,EventArgs e)

    {

$
    }¥b $ b    protected void btnCreate_Click(object sender,EventArgs e)

    {

        if(Page.IsValid)

        {

            using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [" sacpConnectionString"]。ConnectionString))

            {

               试试
                {

                    SqlCommand cmd = new SqlCommand();

                    Guid guid;

                    guid = Guid.NewGuid();

                    string sql = @" UPDATE patient 

                    SET 

                    pUserName = @pUserName,

                    pPassword = @pPassword

                   在哪里pIC = @ pIC" ;;

                    cmd.Parameters.AddWithValue(" @ pIC",txtIC.Value);

                    cmd.Parameters.AddWithValue(" @ pUsername",txtUsername.Value);

                    cmd.Parameters.AddWithValue(" @ pPassword",txtPassword.Value);
$


                    cmd.Connection = con;

                    cmd.CommandText = sql;

                    con.Open();



                    cmd.ExecuteNonQuery();
$


                    Session.Add(" Username",txtUsername.Value);

                    Session.Add(" Password",txtPassword.Value);

                    FormsAuthentication.SetAuthCookie(txtUsername.Value,true);

                    Response.Redirect(" registered.aspx");
$


                }¥b $ b                catch(例外)

                {

                    lblErrorMessage.Text ="IC不存在";

                }¥b $ b               终于

                {

                    con.Close();

                }




            }¥b $ b        }¥b $ b    } b $ b}



解决方案

任何帮助请?

I have three textbox in the web form, which is username, password and IC number. if the IC number the person entered, matches the IC in the database, it will insert the username and password into that record. It it doesn't found any matching IC number, it will not insert any record and it will say IC does not exist. Currently the lblErrorMessage.Text = "IC does not exist"; in the catch exception doesn't work. It will just redirect to registered, even tho if the IC entered doesn't exist in the database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    Guid guid;
                    guid = Guid.NewGuid();
                    string sql = @"UPDATE patient 
                    SET 
                    pUserName = @pUserName,
                    pPassword = @pPassword
                    WHERE pIC = @pIC";
                    cmd.Parameters.AddWithValue("@pIC", txtIC.Value);
                    cmd.Parameters.AddWithValue("@pUsername", txtUsername.Value);
                    cmd.Parameters.AddWithValue("@pPassword", txtPassword.Value);

                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    con.Open();

                    cmd.ExecuteNonQuery();

                    Session.Add("Username", txtUsername.Value);
                    Session.Add("Password", txtPassword.Value);
                    FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
                    Response.Redirect("registered.aspx");

                }
                catch (Exception)
                {
                    lblErrorMessage.Text = "IC does not exist";
                }
                finally
                {
                    con.Close();
                }


            }
        }
    }
}


解决方案

any help pls?


这篇关于如果数据库中已存在值,如何进行错误验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆