how to update c# sql server record using datatimepicker in gridview.

  • Thread starter Thread starter AftabSoomro
  • Start date Start date
A

AftabSoomro

Guest
Following is the code for mouse click on data grid view WHEN I CLICK ON THE GRID GET THE ERROR = (Additional information: String was not recognized as a valid DateTime.)


private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
EntrID = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
DateTime dt = DateTime.ParseExact(dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),"dd-MM-yyyy",CultureInfo.InstalledUICulture);
dateTimePicker1.Value = dt;
DateTime dt1 = DateTime.ParseExact(dataGridView1.SelectedRows[0].Cells[3].Value.ToString(), "dd-MM-yyyy", CultureInfo.InstalledUICulture);
dateTimePicker2.Value = dt1;
txtSalary.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
txtPaidAmount.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();

}


HERE IS UPDATE CODE

if (IsValid())
{
SqlCommand cmd = new SqlCommand("INSERT INTO TStaffSalaries VALUES(@StaffID,@DateofSlip,@DateofPayment,@Salary,@Paid)", con);

cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@StaffID", txtStaffID.Text);
cmd.Parameters.AddWithValue("@DateofSlip", dateTimePicker1.Text.ToString(), "dd-MM-yyyy");
cmd.Parameters.AddWithValue("@DateofPayment", dateTimePicker2.Text);
cmd.Parameters.AddWithValue("@Salary", txtSalary.Text);
cmd.Parameters.AddWithValue("@Paid", txtPaidAmount.Text);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Staff Salaries added successfully!");
txtSalary.Clear();
txtPaidAmount.Clear();
txtStaffID.Clear();
dateTimePicker1.MaxDate = DateTime.Today;
dateTimePicker2.MaxDate = DateTime.Today;
GetStaffSalariesRecords();

}

Continue reading...
 
Back
Top