Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guyss
I need some help..
I have a gridview .In that i took text box and in that textbox i am showing date and time
At the RowUpdating event of gridview it doesnt allow me ..its showing error cannot implicitly convert string to system.DateTime

in SQL table the field is-
SQL
StockDate datetime

this is my RowUpdating events code-
C#
  objBusinessUI.StockDate =((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();

and i aslo try 
objBusinessUI.StockDate =Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim());

its show runtime error .

Please help me..
Thxxx in advance
Posted
Updated 2-Oct-13 18:59pm
v2
Comments
[no name] 3-Oct-13 1:00am    
What's the error you are getting...??
indrajeet jadhav 3-Oct-13 1:02am    
at the time of coding. when i am adding Convert.ToDateTime.'cannot implicitly convert string to system.DateTime' and the debugging/running 'String was not recognized as a valid DateTime.'its FormatException
Azee 3-Oct-13 1:19am    
When you debug, what value does this line return
((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text ?
indrajeet jadhav 3-Oct-13 1:41am    
it returns 1/1/0001 12:00:00 AM and Format Exception throws
indrajeet jadhav 3-Oct-13 1:43am    
its cant able to debug..error showing coding time

C#
objBusinessUI.StockDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");

try this.
 
Share this answer
 
may be this error occurred on datetime casting. you can try as

DateTime dt = new DateTime();
dt = DateTime.ParseExact(your date in string format and if dd/MM/yyyy format, "dd/MM/yyyy", null);
now use dt in future.
 
Share this answer
 
When you retrive data from sql you use this command

select convert(varchar(10),Stockdate,103) as stdate from tableName

it will return date in dd/mm/yyyy format
than you can easily bind this to text box
text='<%Eval("stdate")%>'
 
Share this answer
 
Using Find Control method you can find out the Text box that contains date.
Now assign this text box text property to specific object property.

C#
TextBox txtdate= (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
objBusinessUI.StockDate=txtdate.text;

or 

objBusinessUI.StockDate=DateTime.Parse(txtdate.text);


if StockDate datatype is date.

Hope this one help you.
 
Share this answer
 
TextBox txtdate= (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
objBusinessUI.StockDate=txtdate.text;

or

objBusinessUI.StockDate=DateTime.Parse(txtdate.text);
 
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