Inaa Inzii
Inaa Inzii

Reputation: 103

GridView: Can't insert values into another database from gridview

I'm trying to insert values from gridview to a database but I get error: Here is my design : http://pastebin.com/vEm3ciPU

I'm getting

"Conversion failed when converting date and/or time from character string." "Line 32: dataCommand.ExecuteNonQuery();"

What can I do? Thank you, I'm using a gridview and sqldatasource to bind datatable to gridview.

Upvotes: 1

Views: 289

Answers (1)

Pankaj
Pankaj

Reputation: 10105

Replace the following

dataCommand.Parameters.AddWithValue("Name", e.OldValues[0]); 
dataCommand.Parameters.AddWithValue("Value", e.OldValues[2]); 
dataCommand.Parameters.AddWithValue("Total", e.OldValues[3]); 
dataCommand.Parameters.AddWithValue("Date", e.OldValues[4]);

With Following

dataCommand.Parameters.AddWithValue("Name", e.OldValues[0]); 
dataCommand.Parameters.AddWithValue("Value", e.OldValues[1]); 
dataCommand.Parameters.AddWithValue("Total", e.OldValues[2]); 
DateTime dt; 
if (DateTime.TryParse(e.OldValues[3].ToString(), out dt)) 
    dataCommand.Parameters.AddWithValue("Date", dt); 
else
    dataCommand.Parameters.AddWithValue("Date", DateTime.Now); 

I am assuming below mentioned schema.

enter image description here

Upvotes: 1

Related Questions