Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.net-强类型数据集的使用_Asp.net_Sql_Gridview_Ado.net_Dataset - Fatal编程技术网

ASP.net-强类型数据集的使用

ASP.net-强类型数据集的使用,asp.net,sql,gridview,ado.net,dataset,Asp.net,Sql,Gridview,Ado.net,Dataset,我有一个非常简单的产品SQL表,我想用asp.net GridView编辑它 1我使用Insert/Select/Update命令为products表创建了一个数据集/适配器。 2在Default.aspx中,我有以下网格: <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView

我有一个非常简单的产品SQL表,我想用asp.net GridView编辑它

1我使用Insert/Select/Update命令为products表创建了一个数据集/适配器。 2在Default.aspx中,我有以下网格:

   <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="Produkt" />
        <asp:BoundField DataField="ProductManufacturer" HeaderText="Hersteller" />
        <asp:BoundField DataField="ProductPrice" HeaderText="Preis" />
    </Columns>
</asp:GridView> <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="Produkt" />
        <asp:BoundField DataField="ProductManufacturer" HeaderText="Hersteller" />
        <asp:BoundField DataField="ProductPrice" HeaderText="Preis" />
    </Columns>
</asp:GridView>
问题是,ds.hasChanges总是错误的。因此,在编辑并单击“更新”之后,它只需加载原始数据


有什么问题?

不要在ASP.NET中使用静态数据集!所有请求共享此对象,这将导致锁定和其他线程问题,包括数据不一致性。除此之外,所有对象都是在页面生命周期结束时处理的,这可能是使其成为静态的原因。但是更新甚至不需要原始数据源。看一看或其他教程了解更多信息。嗨,蒂姆,谢谢你,但我只是添加了静态,因为我认为它可能与我的问题有关,它以前是私有的。我的意思是,我可以直接使用适配器进行更新,但是为什么我不能使用类型化数据集呢?访问器和静态修饰符是正交的概念,所以私有和静态之间没有关系。为什么要使用数据集?它将在每次回发时销毁,因为HTTP是无状态协议。因此,您每次都在创建一个空白的新数据集。此外,数据集的HasChanges始终为false,因为它从未更改过。这根本不是GridView更新的工作方式,数据源在回发中无论如何都是空的。谢谢,你是对的,当然,我不知道我在那里是怎么想的。但是,在MSDN示例中,它们只是更新会话中的表,而不是数据库。我就是找不到一个好的例子。很难相信,没有比使用诸如dt.Rows[row.DataItemIndex][Description]=TextBoxrow.Cells[2]。控件[0]。Text;在会话中存储DataTable/DataSet不是最佳做法,即使它的工作方式与静态相反,并且在用户较少的intranet应用程序中可能是最佳选择。但是,一般不要这样做。您应该搜索一个关于GridView的好教程,例如,还可以使用键、OldValues和NewValues集合作为参数。如果使用TemplateFields,还可以通过ID找到控件并创建可读代码。
public partial class Default : System.Web.UI.Page
{
    public static dsProducts ds = new dsProducts();

    protected void GridBind()
    {
        ProductTableAdapter tableAdapter = new ProductTableAdapter();
        tableAdapter.Fill(ds.Product);
        GridView1.DataSource = ds.Product;
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridBind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        GridBind();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (ds.HasChanges()==true)
        {
            ProductTableAdapter adapter = new ProductTableAdapter();
            adapter.Update(ds.Product.Rows[e.RowIndex]);
        }

        // Exit edit mode
        this.GridView1.EditIndex = -1;
        // Update the grid
        GridBind();

    }

}