I have a form with multiple TextBox
and NumericUpDown
And DateTimePicker
. I want to get programmatically all value of this controls and insert them to sql server data base
I tried this code
foreach (Control contrl in this.Controls)
{
if (contrl.Name != "")
{
vv.Add(contrl.Text);
//MessageBox.Show(contrl.Name);
}
}
it gets me the correct values of TextBox
and NumericUpDown
but not DateTimePicker
DateTimePicker
and NumericUpDown
use Value
property to get it's content, TextBox
use Text
property.
So you need to check the type of each control
foreach (Control contrl in this.Controls)
{
if (contrl.Name != "")
{
if(contrl is TextBox)
vv.Add(((TextBox)contrl).Text);
else if(contrl is DateTimePicker)
vv.Add(((DateTimePicker)contrl).Value);
else if(contrl is NumericUpDown)
vv.Add(((NumericUpDown)contrl).Value);
}
}