如果数据库中已存在值,如何进行错误验证?

编程入门 行业动态 更新时间:2024-10-26 16:35:48
本文介绍了如果数据库中已存在值,如何进行错误验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有三个文本框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?

更多推荐

如果数据库中已存在值,如何进行错误验证?

本文发布于:2023-10-19 18:17:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!