Asp.net如何使用验证控制代码

发布于 2024-10-21 22:43:15 字数 3298 浏览 0 评论 0原文

实际上我正在设计我的网页,因为我输入了图书 ID 和
书名。键入,然后单击按钮 图书可用 转到另一个页面,然后不可用 显示错误消息 图书不可用 我的项目(图书馆管理系统) Asp.net sql server 如何验证页面以及如何设置验证控制以及如何设置验证控制代码...向我发送完整详细信息

My code
using System;
using System.Collections;
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 Bookcheck : System.Web.UI.Page
{
      public Int64 Sid;
    protected void Page_Load(object sender, EventArgs e)
    {
        string Id;
        Id = Request.QueryString.Get(0);

    if (!(IsPostBack == true))
    {
        if (Request.QueryString.Get(1) == "G")
        {
            Sid = Convert.ToInt64(Id);
            if (PopulatedRecord (Sid ) == false)
            {

            }
        }
    }
}
private Boolean PopulatedRecord(Int64 Id)
{
    DataSet DS;
    DS = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    SqlConnection Cnn = new SqlConnection();
    string connectionstring;
    connectionstring = @"Datasource=DEVI\SQLEXPRESS;Intial catalog=librarymanagement;Integrated Security=SSPI";
    Cnn.ConnectionString = connectionstring;
    if (Cnn.State != ConnectionState.Open)
        Cnn.Open();
    cmd.Connection = Cnn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "usbinsertdatainto";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@bookId",txtid );
    da.SelectCommand = cmd;
    try
    {
        da.Fill(DS);
    }
    catch (Exception ex)
    {
        throw new ApplicationException(
            "!!! An Error Occured While Update Record In Dtl_SecurityCapital_Upload." + ex.Message);
        lblerror.Visible = true;
        lblerror.Text = "!!! An Error Occured While " + ex.Message.ToString();
        return false;
    }
    if (DS.Tables[0].Rows.Count > 0)
    {
        txtid.Text = DS.Tables[0].Rows[0]["bookId"].ToString();
        txtnb.Text = DS.Tables[0].Rows[0]["Nameofthebook"].ToString();
    }
    cmd.Dispose();
    Cnn.Close();
    Cnn.Dispose();
    return true;
}


}
        protected void  Button2_Click(object sender, EventArgs e)
         {
        if(Page.IsValid )
        {
        Response.Redirect ("issueofbook.aspx?bookid="+txtid.Text  

                                             +"&Nameofthebook="+txtnb.Text);
          }
      }
    }      

                   Error
                               Line 59:             throw new ApplicationException(
                               Line 60:                 "!!! An Error Occured While  
                                                      Update                              

                               Record In Dtl_SecurityCapital_Upload." + ex.Message);
                                Line 61:             lblerror.Visible = true;
                                   Line 62:             lblerror.Text = "!!! An Error 
                                                Occured While " +                          
                                                             ex.Message.ToString();

第 63 行:返回 false;

Actually im designing my web page, in that i have typed in the book id and
Nameofthebook. typed and then click the button the book are available is go to another page and then not available is display the error message is book is not available My project (Library management system) Asp.net sql server how to validate the page and how to set validate control and how to set validate control code... send me full details

My code
using System;
using System.Collections;
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 Bookcheck : System.Web.UI.Page
{
      public Int64 Sid;
    protected void Page_Load(object sender, EventArgs e)
    {
        string Id;
        Id = Request.QueryString.Get(0);

    if (!(IsPostBack == true))
    {
        if (Request.QueryString.Get(1) == "G")
        {
            Sid = Convert.ToInt64(Id);
            if (PopulatedRecord (Sid ) == false)
            {

            }
        }
    }
}
private Boolean PopulatedRecord(Int64 Id)
{
    DataSet DS;
    DS = new DataSet();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    SqlConnection Cnn = new SqlConnection();
    string connectionstring;
    connectionstring = @"Datasource=DEVI\SQLEXPRESS;Intial catalog=librarymanagement;Integrated Security=SSPI";
    Cnn.ConnectionString = connectionstring;
    if (Cnn.State != ConnectionState.Open)
        Cnn.Open();
    cmd.Connection = Cnn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "usbinsertdatainto";
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@bookId",txtid );
    da.SelectCommand = cmd;
    try
    {
        da.Fill(DS);
    }
    catch (Exception ex)
    {
        throw new ApplicationException(
            "!!! An Error Occured While Update Record In Dtl_SecurityCapital_Upload." + ex.Message);
        lblerror.Visible = true;
        lblerror.Text = "!!! An Error Occured While " + ex.Message.ToString();
        return false;
    }
    if (DS.Tables[0].Rows.Count > 0)
    {
        txtid.Text = DS.Tables[0].Rows[0]["bookId"].ToString();
        txtnb.Text = DS.Tables[0].Rows[0]["Nameofthebook"].ToString();
    }
    cmd.Dispose();
    Cnn.Close();
    Cnn.Dispose();
    return true;
}


}
        protected void  Button2_Click(object sender, EventArgs e)
         {
        if(Page.IsValid )
        {
        Response.Redirect ("issueofbook.aspx?bookid="+txtid.Text  

                                             +"&Nameofthebook="+txtnb.Text);
          }
      }
    }      

                   Error
                               Line 59:             throw new ApplicationException(
                               Line 60:                 "!!! An Error Occured While  
                                                      Update                              

                               Record In Dtl_SecurityCapital_Upload." + ex.Message);
                                Line 61:             lblerror.Visible = true;
                                   Line 62:             lblerror.Text = "!!! An Error 
                                                Occured While " +                          
                                                             ex.Message.ToString();

Line 63: return false;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2024-10-28 22:43:15

亲爱的,
使用 RequiredFieldValidator (ASP .NET 控件)用于检查 TextBox 是否为空。
或者
使用 JavaScript 检查文本框是否为空

标记:

<asp:TextBox ID="txtNo" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ErrorMessage="No Required " ControlToValidate="txtNo" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Name Required " ControlToValidate="txtName" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

按钮单击事件:

protected void Button1_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Response.Redirect("~/Newpage.aspx?no=" + txtNo.Text + "&name=" + txtName.Text); 
    }
}

Dear,
Use RequiredFieldValidator (ASP.NET control) for checking TextBox are empty or not.
or
use JavaScript to check Textbox is empty or not

Markup:

<asp:TextBox ID="txtNo" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ErrorMessage="No Required " ControlToValidate="txtNo" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Name Required " ControlToValidate="txtName" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

Button click event :

protected void Button1_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Response.Redirect("~/Newpage.aspx?no=" + txtNo.Text + "&name=" + txtName.Text); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文