编辑GridView
时出现以下错误:
从字符串转换日期和/或时间时转换失败。
我认为错误发生在购买日期列中我尝试将数据类型转换为日期但是它仍然无法正常工作
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>
答案 0 :(得分:0)
看起来您实际上并没有将控件的文本内容作为日期时间转换参数传递。
尝试更改:
DateTime pdate = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).ToString();
到
DateTime pdate = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).Text);
答案 1 :(得分:0)
尝试更改
String strsession = "update tblPRS_Sparepart set supplier='" + supplier.ToString() + "',UP_VATI ='" + unitprice + "',Purchasing_Status ='" + pstatus + "',Purchase_date ='" + pdate + "' where ID='" + ID + "'";
到
String strsession = "update tblPRS_Sparepart set supplier='" + supplier.ToString() + "',UP_VATI ='" + unitprice + "',Purchasing_Status ='" + pstatus + "',Purchase_date ='" + pdate .ToString() + "' where ID='" + ID + "'";