none
how to judge the data RRS feed

  • Question

  • how to judge the data inputing in textbox whether it match the field of table in database.

    i mean you input the data to textbox ,and the process  judge the data you inputing,if attribute of  data match the attribute of the field of table in database,and then show "complete insert",or show the attribute of inputing data error.

     

    Wednesday, June 6, 2007 12:31 AM

Answers

  •  

    Hi

     

    If the data type of the field is known. You can use the method supported by the Convert class.The Convert class converts a base data type to another base data type. This class returns a type whose value is equivalent to the value of a specified type. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String. You can handle this conversion in the TextBox.Validating Event. e.g.

    Code Snippet

            private void textBox1_Validating(object sender, CancelEventArgs e)

            {

                try

                {

                    bool a = Convert.ToBoolean(this.textBox1.Text);

                    this.textBox2.Text = a.ToString();//you insert method

                }

                catch

                {

                    MessageBox.Show("Data Error!");

                    e.Cancel = true;

                    this.textBox1.SelectAll();

                }

            }

     

    If the data type is not known. You can binding the TextBox to the datacolmn. It will automatically judge the data. e.g.

    Code Snippet

            private void ChildFrm2_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("isFull", typeof(bool));

                dt.Rows.Add(true);

     

                this.textBox1.DataBindings.Add("Text", dt, "isFull",true);

                this.textBox1.DataBindings[0].BindingComplete += new BindingCompleteEventHandler(ChildFrm2_BindingComplete);

            }

     

            void ChildFrm2_BindingComplete(object sender, BindingCompleteEventArgs e)

            {

                if (e.Cancel)

                {

                    MessageBox.Show("Data Error!");

                    this.textBox1.SelectAll();

                }

                else

                    this.textBox2.Text = this.textBox1.Text;//you insert method

            }

     

    Hope this helps.

     

    Regards

    Friday, June 8, 2007 8:39 AM