简体   繁体   English

ASP.NET GridView更新

[英]ASP.NET GridView Updating

I get the following error when editing a GridView : 编辑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. 看来您实际上并没有传递控件的文本内容作为datetime转换参数。

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 + "'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM