Click here to Skip to main content
16,009,238 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
for (int a = 0; a < dataGridView1.Rows.Count - 1; a++)
{
   string item11 = dataGridView1[0, a].Value.ToString();
  string broktype11 = dataGridView1[1, a].Value.ToString();
  string brokrate11 = dataGridView1[2, a].Value.ToString();
  string tranrate = dataGridView1[3, a].Value.ToString();
  string trantype11 = dataGridView1[4, a].Value.ToString();
  //string setdate11 = dataGridView1[5, a].Value.ToString();
  DateTime t = Convert.ToDateTime(dataGridView1[5, a].Value.ToString());
 
 
  string z = t.Date.ToString("dd/mm/yyyy");
 


  string martype11 = dataGridView1[6, a].Value.ToString();
  string marrate11 = dataGridView1[7, a].Value.ToString();
  SqlCommand cmd1 = new SqlCommand("select AC_code from accountm where name ='" + dataGridView1[8, a].Value.ToString() + "'and compcode='" + Compcls.Gcomp_cd + "' ", con);
  string ACcode = Convert.ToString(cmd.ExecuteScalar());
  cmd.CommandText = ("insert into PITBROK (COMPCODE,ITEMCODE, BROKTYPE,BROKRATE,TRANRATE,TranType,uptostdt,MARTYPE,MARRATE,AC_CODE) values('" + Compcls.Gcomp_cd + "','" + item11 + "','" +broktype11 + "','"+brokrate11+"','"+tranrate+"','"+trantype11+"','"+z.Trim()+"','"+martype11+"','"+marrate11+"','"+ACcode+"') ");
  cmd.ExecuteNonQuery();
  MessageBox.Show("done");


error=The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.



Posted
Updated 3-Jan-12 21:45pm
v2

Hi,

change below line

C#
string z = t.Date.ToString("dd/mm/yyyy");


with

C#
string z = t.Date.ToString("dd/MM/yyyy");


your month should be MM(Capital)

hope this will solve your problem.

thanks
-amit.
 
Share this answer
 
Comments
rosyp 4-Jan-12 4:16am    
thanks amit
but getting same error
AmitGajjar 4-Jan-12 4:18am    
use t.ToString("dd/MM/yyyy") and what datatype in database for uptostdt ?
rosyp 4-Jan-12 4:19am    
uptostdt smalldatetime (4)
devbtl 4-Jan-12 4:20am    
yeah Month should be in capital "dd-MM-yyyy"
AmitGajjar 4-Jan-12 4:21am    
if you are using SQL server 2008 then change datatype to 'Date' instead of 'smalldatetime' as you are passing date only.
There are a couple of issues.
1) As noted by amitgajjar, your format string was incorrect and should have the capital MM for month. The database may also be happier if you use this format string instead. "yyyy-MM-dd"
2) What you haven't verified is the value you are actually trying to convert in the database. Make sure that the string you are passing in is within the valid range of a smalldatetime. The valid range is 1900-01-01 through 2079-06-06.
 
Share this answer
 
use conversion for datefield

convert(varchar(10),'date',105) in query
 
Share this answer
 
v2
Comments
rosyp 4-Jan-12 3:51am    
my column in table is
uptostdt smalldatetime 4
devbtl 4-Jan-12 3:53am    
in which line u are getting error
rosyp 4-Jan-12 4:16am    
cmd.ExecuteNonQuery();
here is my solution
C#
string z = t.Date.ToString("yyyy/MM/dd");
 
Share this answer
 
v2

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