【问题标题】:C# Strongly typed dataset validation approachC#强类型数据集验证方法
【发布时间】:2016-03-27 13:10:25
【问题描述】:

我正在尝试“学习”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();
            }
        }
    }
}

我试图用上面的代码做的是,除非用户输入名字、公民身份证号码并提供电话号码,否则表单不应该提交数据。对于人员 ID,我使用了一个序列,通过附加到表适配器的标量查询调用。

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

【问题讨论】:

  • 我认为下面的例子越来越接近link

标签: c# .net


【解决方案1】:

您需要像下面的代码一样注册事件

        public Form1()
        {
            InitializeComponent();

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

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

【讨论】:

  • 它没有帮助。我想要实现的是,在按下保存按钮时,应验证所有与错误提供程序相关的单元格并阻止用户为人员 ID 单元格生成新序列。
猜你喜欢
  • 2011-01-08
  • 1970-01-01
  • 2011-03-22
  • 2023-04-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 2010-10-07
  • 2017-03-23
相关资源
最近更新 更多