I get the following error when editing a GridView
:
Conversion failed when converting date and/or time from character string.
I think the error is on Purchase Date Column I try to convert the data type to date but it is still not work any idea
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text;
string supplier = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsupplier")).Text;
string unitprice = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtunitprice")).Text;
string pstatus = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlpstatus")).Text;
DateTime pdate = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).ToString();
string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
String strsession = "update tblPRS_Sparepart set supplier='" + supplier.ToString() + "',UP_VATI ='" + unitprice + "',Purchasing_Status ='" + pstatus + "',Purchase_date ='" + pdate + "' where ID='" + ID + "'";
SqlCommand cmd = new SqlCommand(strsession, conn);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
conn.Close();
BindData();
}
<asp:TemplateField HeaderText="Purchase Status">
<ItemTemplate>
<asp:Label ID="lblPstatus" runat="server" Text='<%#Eval("Purchasing_Status") %>'>'> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblpstatus" runat="server" Text='<%# Eval("Purchasing_Status")%>' Visible = "false"></asp:Label>
<asp:DropDownList ID = "ddlpstatus" runat = "server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Status">
<ItemTemplate>
<asp:Label ID="lblPDate" runat="server" dataformatstring="{0:dd/MM/yyyy}" ItemStyle-Width="70px" ItemStyle-Wrap="false" Text='<%#Eval("Purchase_date","{0:dd/MM/yyyy}") %>'>'> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblpdate" runat="server" Text='<%# Eval("Purchase_date","{0:dd-MM-yyyy}")%>' Visible = "false"></asp:Label>
<asp:TextBox ID="txtpdate" runat="server" Text='<%# Eval("Purchase_date","{0:dd-MM-yyyy}")%>' ></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtpdate">
</asp:CalendarExtender>
</EditItemTemplate>
</asp:TemplateField>
It looks like you aren't actually passing the text contents of the control as the datetime conversion parameter.
Try changing:
DateTime pdate = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).ToString();
to
DateTime pdate = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).Text);
try change
String strsession = "update tblPRS_Sparepart set supplier='" + supplier.ToString() + "',UP_VATI ='" + unitprice + "',Purchasing_Status ='" + pstatus + "',Purchase_date ='" + pdate + "' where ID='" + ID + "'";
to
String strsession = "update tblPRS_Sparepart set supplier='" + supplier.ToString() + "',UP_VATI ='" + unitprice + "',Purchasing_Status ='" + pstatus + "',Purchase_date ='" + pdate .ToString() + "' where ID='" + ID + "'";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.