簡體   English   中英

ASP.NET GridView更新

[英]ASP.NET GridView Updating

編輯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>

看來您實際上並沒有傳遞控件的文本內容作為datetime轉換參數。

嘗試更改:

DateTime pdate = Convert.ToDateTime((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).ToString();

DateTime pdate = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpdate")).Text);

嘗試改變

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM