Inserting values from TextBox ComboBox NumericUpDown and DateTimePicker to database C#.Net

Inserting values from TextBox ComboBox NumericUpDown and DateTimePicker to database C#.Net: This article explains you that how to save the data from the multiple controls to the database.In dot net we have different data entry controls.We can use them depending upon the data we have to enter.Here i have mentioned some controls how they will insert to database. TextBox control data always will be in string format, so it will take Text value.ComboBox willalso works like a TextBox.ComboBox is a editable DropDown box.NumericUpDown will take the integer value

so we have to convert its value to int. As this DataTimePicker will takes its value in datatime format.
                                     Clic here for more database insertions

Event: Write this code in btnSave_Click event.

        OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source=D:\WORK AREA\Forms Apps\Thalassemia\Data\thalsemia.accdb"
);
           cn.Open();

 OleDbCommand cmd = new OleDbCommand("insert into dbtable(name,education,age,dateofbirth) values(@nam,@edu,@age,@dobth)",cn);
        cmd.Parameters.AddWithValue("@name",txtName.Text);
        cmd.Parameters.AddWithValue("@edu", cmbEducatn.selectedText.ToString());
        cmd.Parameters.AddWithValue("@age", nudAge.Value.ToString());
        cmd.Parameters.AddWithValue("@dobth", dtpDateOfBirth.Value.ToShortDateString());
        cmd.ExecuteNonQuery();
        MessageBox.Show("Pedegree saved successfully.");
I hope this code will help you.

No comments:

Post a Comment