在ASP.NET中进行错误检查 [英] Error check in asp.net

查看:91
本文介绍了在ASP.NET中进行错误检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

I have a code like this:

private void checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return;
    }
}



我在保存代码之前在buttion_click事件中声明了此函数.



This function i have declare in buttion_click event before the save code.

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            checkError();
            SqlConnection con = database.GetConnection();
            SqlCommand com = new SqlCommand("spRegistration", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@Name", txtName.Text);
            com.Parameters.AddWithValue("@UserId", txtUserId.Text);
            com.Parameters.AddWithValue("@Password", txtPasword.Text);
            com.Parameters.AddWithValue("@Date", txtDate.Text);
            com.ExecuteNonQuery();
            WebMsgBox.Show("Accounts Created Successfully");
            clearFields();

        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Error Message:" + ex.Message);
        }
      
    }



然后,如果单击文本框中没有任何值的保存按钮,则将空值插入数据库.我该怎么解决.
请帮我.

在此先感谢



Then if we click save button without any value in textbox, the null value will be insert into the database. How can i solve it.
Please help me.

Thanks in advance

推荐答案

RequiredFieldValidator[^], perhaps.


由于无效使用而插入的布尔值返回true或false,所以这是怎么回事


Insted of void use return a boolean that is true or false so here is how it goes


private bool checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return false;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return false;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return false;
    }
    else return true;

}







protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if(checkError())
{
  SqlConnection con = database.GetConnection();
  SqlCommand com = new SqlCommand("spRegistration", con);
  com.CommandType = CommandType.StoredProcedure;
  com.Parameters.AddWithValue("@Name", txtName.Text);
  com.Parameters.AddWithValue("@UserId", txtUserId.Text);
  com.Parameters.AddWithValue("@Password", txtPasword.Text);
  com.Parameters.AddWithValue("@Date", txtDate.Text);
  com.ExecuteNonQuery();
  WebMsgBox.Show("Accounts Created Successfully");
  clearFields();
 }

}
catch (Exception ex)
{
WebMsgBox.Show("Error Message:" + ex.Message);
}

}


更改错误例程以返回布尔值:
Change your error routine to return a bool:
private bool checkError()
{
    if (txtName.Text == string.Empty)
    {
        WebMsgBox.Show("Please Enter User Name");
        txtName.Focus();
        return true;
    }
    if (txtUserId.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter UserId");
        txtUserId.Focus();
        return true;
    }
    if (txtPasword.Text.Trim() == string.Empty)
    {
        WebMsgBox.Show("Please Enter Password");
        txtPasword.Focus();
        return true;
    }
    return false;
}

然后只需检查返回代码:

Then just check the return code:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (!checkError())
            {
                SqlConnection con = database.GetConnection();
                ....
                clearFields();
            }
        }
        catch (Exception ex)
        {
            WebMsgBox.Show("Error Message:" + ex.Message);
        }
      
    }


这篇关于在ASP.NET中进行错误检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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