Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add date from DateTimePicker to database table & also want to retrieve date from database table to DateTimePicker.

Basically,My datagridview get updated at each operation(insert,update,delete) & it has edit button. when I click on edit button all the values from datagridview row are set to the controls in my form(label,textbox) correspondingly.

Main form code :

C#
private void btnSAdd_Click(object sender, EventArgs e)
       {
          // to insert value in database I've passed all values to class
           SavingsDetails sd = new SavingsDetails(lblSTId.Text,cmbSGID,cmbSMId,dtpInstallmentDate.Text,txtInstallmentAmt.Text);
           sd.Add();
           setSavingsGridView();
       }


code in SavingsDetails Class :

C#
class SavingsDetails
   {
       protected int STId,MId,GId,Amt;
       DateTime SDate;
       Connection con;

       public SavingsDetails(string _STId, int _GId, int _MId, DateTime _SDate, string _Amt)
       {
           con = new Connection();
           STId = Convert.ToInt16(_STId);
           GId = _GId;
           MId = _MId;
           SDate = _SDate;
           Amt = Convert.ToInt32(_Amt);
       }

       public void Add()
       {
           con.Execute("insert into tblSavingsDetails(STId,STDate,GId,MId,SavingInstallment) values("+ STId +","+ SDate +","+ GId +","+ MId +","+ Amt +"");
       }


To retrieve from database to form controls :

C#
private void dgvSavingsDetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0) //code for setting values in controls on clicking edit image in gridview
            {
                btnSUpdate.Enabled = true;
                lblSTId.Text = dgvSavingsDetails.Rows[e.RowIndex].Cells[2].Value.ToString();
                cmbSGrpName.SelectedValue = dgvSavingsDetails.Rows[e.RowIndex].Cells[3].Value;
                cmbSMName.SelectedValue = dgvSavingsDetails.Rows[e.RowIndex].Cells[4].Value;
                dtpInstallmentDate.Text = dgvSavingsDetails.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtInstallmentAmt.Text = dgvSavingsDetails.Rows[e.RowIndex].Cells[6].Value.ToString();
            }
            else if (e.ColumnIndex == 1)
            {
                //code for deletion
            }
        }


Please Help.
Posted
Updated 11-Jul-14 2:07am
v3
Comments
[no name] 9-Jul-14 8:15am    
Sure lots of people can help. What have you tried and what specifically is it that you would need help with?
[no name] 9-Jul-14 8:24am    
Have you tried something?
Sneha_10 9-Jul-14 8:58am    
You can check my code which I tried.

Try:
C#
using (SqlCommand cmd = new SqlComand("INSERT INTO MyTable (myDateColumn) VALUES (@DT)", con))
   {
   cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);
   cmd.ExecuteNonQuery();
   }

And:
C#
using (SqlCommand cmd = new SqlComand("SELECT myDateColumn FROM MYTable", con))
   {
   SqlDataReader read = cmd.ExecuteReader();
   while (read.Read())
      {
      DateTime dt = (DateTime) read["myDateColumn"];
      ...
      }
   }
 
Share this answer
 
Comments
Raul Iloc 9-Jul-14 8:42am    
You have my vote!
Sneha_10 9-Jul-14 8:57am    
Thannk you for this but I've posted my code. Please check & help.
Sneha_10 9-Jul-14 9:39am    
My code for query execution is in different class method. So parameters' method won't work for me.
I've solved this myself. Thank you for your help.
Replaced dtpInstallmentDate.Text with dtpInstallmentDate.Value.Date & In Add() method, updated SDate.ToShortDateString() in values of insert query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900