Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want that from my datagridview only the date should pass from expiry date column to the database.but with the date.time is also going.And in database data type of that column is datatime.and on form i am converting that column value to string format........so please help me out with this problem
Posted

The .NET DateTime structure stores both the Date and Time as given on http://msdn.microsoft.com[^]
"the DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini"
Similarly the DataBase DateTime type also stores both Date, Time.

It is only the format in which you want to display in TextBox, DataGridView, etc. for which you can use one of the format specifier to show the Date, as shown here

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.shortdatepattern(v=vs.80).aspx[^]
 
Share this answer
 
Comments
thakurajay93 11-Mar-12 14:35pm    
thanks for the help.. but can u tell how can i convert that column into shortdatepattern in datagriedview.
ProEnggSoft 11-Mar-12 22:19pm    
Thank you. See my solution 2, as it is difficult to explain in comment.
The DataGridView does not have a DateTime column. But a DateTimePicker control can be hosted in DataGridView cell. An example is given here

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx[^]

In this example in the constructor of CalenderCell the Format of DataTime is set to short date format.
C#
public CalendarCell()
: base()
{
    // Use the short date format.
    this.Style.Format = "d";
}
 
Share this answer
 
Comments
thakurajay93 12-Mar-12 1:24am    
thanks again for the help.But not getting properly.so i have pasted the code ..

private void button3_Click(object sender, EventArgs e)
{
string prodname = null,pack = null,md = null,ed = null,batchno = null,per = null;
double mrp = 0.0d,rate = 0.0d,tamt = 0.0d;
int aq = 0, bq = 0;
SqlConnection conn = new SqlConnection();
connection obj = new connection();
conn.ConnectionString = obj.getconnstring();
SqlDataReader rdr = null;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
prodname = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
pack = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
mrp = Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value);
md = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
ed = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
batchno = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);
aq = Convert.ToInt32(dataGridView1.Rows[i].Cells[6].Value);
bq = Convert.ToInt32(dataGridView1.Rows[i].Cells[7].Value);
rate = Convert.ToDouble(dataGridView1.Rows[i].Cells[8].Value);
per = Convert.ToString(dataGridView1.Rows[i].Cells[9].Value);
tamt = Convert.ToDouble(dataGridView1.Rows[i].Cells[10].Value);

try
{
conn.Open();
SqlCommand cmd = new SqlCommand("Insert into sales_order values('" + clientid + "','" + textBox3.Text + "','" + textBox5.Text + "','" + dateTimePicker2.Value + "','" + textBox8.Text + "','" + prodname + "','" + pack + "','" + mrp + "','" + md + "','" + ed + "','" + batchno + "','" + aq + "','" + bq + "','" + rate + "','" + per + "','" + tamt + "')", conn);
SqlCommand cmd1 = new SqlCommand("insert into Grand_total_client values('" + clientid + "','" + textBox3.Text+"','"+textBox11.Text+"','"+textBox10.Text+"')", conn);
SqlCommand cmd2 = new SqlCommand("update stock set qty = qty - " + aq + " where batch_no = '" + batchno+"'", conn);
SqlCommand cmd3 = new SqlCommand("delete from stock where qty=0", conn);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();


}
//catch (Exception e1) { }
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
MessageBox.Show("Data updated Successfully", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
if (this.dataGridView1.DataSource != null)
{
this.dataGridView1.DataSource = null;
}
else
{
this.dataGridView1.Rows.Clear();
}
}
}
thakurajay93 12-Mar-12 1:25am    
md and ed are the expiry date manufacture date

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