C#强类型数据集验证方法

时间:2016-03-27 13:10:26

标签: c# .net

我正在努力学习" C#并构建我的第一个数据库驱动数据输入应用程序。我来自Oracle开发,因此想知道我是否做了一些正确的事情,因为我能找到的大多数例子都是处理使用SQL派生的数据集

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace lSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void l_PEOPLEBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {

            if ((string.IsNullOrWhiteSpace(fIRST_NAMETextBox.Text) || (string.IsNullOrWhiteSpace(cIVIL_IDTextBox.Text) ||
                (string.IsNullOrWhiteSpace(tELEPHONE_NUMBERTextbox.Text)))))
            {
                MessageBox.Show("Error, one of the mandatory columns are not filled up");
                return;
            }

            try
            {
                this.Validate();
                this.pERSON_IDTextBox.Text = this.l_PEOPLETableAdapter.ScalarQuery().ToString();
                this.l_PEOPLEBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.uNUDataSet);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception happened, original message: " + ex.Message);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'uNUDataSet.L_PEOPLE' table. You can move, or remove it, as needed.
            this.l_PEOPLETableAdapter.Fill(this.uNUDataSet.L_PEOPLE);
           // this.fIRST_NAMETextBox.Focus();
            this.ActiveControl = this.fIRST_NAMETextBox;

        }

        private void sByName_Click(object sender, EventArgs e)
        {
            this.l_PEOPLETableAdapter.FillByNAME(this.uNUDataSet.L_PEOPLE, this.sByNameText.Text);
        }

        private void fIRST_NAMETextBox_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(fIRST_NAMETextBox.Text))
            {
                eNROLL_errorprovider.SetError(fIRST_NAMETextBox, "Name required");
                fIRST_NAMETextBox.Focus();
            }
        }

        private void cIVIL_IDTextBox_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cIVIL_IDTextBox.Text))
            {
                eNROLL_errorprovider.SetError(cIVIL_IDTextBox, "Civil ID Number required");
                cIVIL_IDTextBox.Focus();
            }
        }

        private void tELEPHONE_NUMBERTextbox_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tELEPHONE_NUMBERTextbox.Text))
            {
                eNROLL_errorprovider.SetError(tELEPHONE_NUMBERTextbox, "Telephone Number required");
                tELEPHONE_NUMBERTextbox.Focus();
                }
            else
            {
                eNROLL_errorprovider.Clear();
            }
        }
    }
}

我尝试使用上面的代码是,除非用户输入名字,民事身份证号码并提供电话号码,否则表格不应提交数据。对于person id,我使用一个序列,通过附加到表适配器的标量查询调用。

当我单击绑定导航器保存按钮时,应该执行什么以触发附加文本列的验证事件,以便错误提供程序将被激活?

1 个答案:

答案 0 :(得分:-1)

您需要注册事件,如下面的代码

        public Form1()
        {
            InitializeComponent();

            tELEPHONE_NUMBERTextbox.Validated += new EventHandler(tELEPHONE_NUMBERTextbox_Validated);
        }

        private void tELEPHONE_NUMBERTextbox_Validated(object sender, EventArgs e)
        {
        }