2

I have been coding a gridview into my website for a day or so now but simply cannot get it to work, fixing one problem just creates more issues.

When selecting edit on a particular record, the gridview goes into the edit item template as it should, but once I have changed the records I want to change, I click update, and then once the gridview returns to item template, all fields on the record are empty?

Here is the GridView code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="PlantID" 
        DataSourceID="SqlDataSource1" AllowSorting="True" CellPadding="4" 
        ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"
        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ButtonType="Button" ShowEditButton="true" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
            <asp:CommandField ButtonType="Button" ShowCancelButton="true" />
            <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
            <asp:BoundField DataField="PlantID" HeaderText="PlantID" InsertVisible="False" 
                ReadOnly="True" SortExpression="PlantID" />
            <asp:TemplateField HeaderText="Latin Name">
                <ItemTemplate>
                    <%# Eval("LatinName") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtLatinName" Text='<%# Eval("LatinName") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Popular Name">
                <ItemTemplate>
                    <%# Eval("PopularName") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtPopularName" Text='<%# Eval("PopularName")%>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Season">
                <ItemTemplate>
                    <%# Eval("Season") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtSeason" Text='<%# Eval("Season") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Aftercare">
                <ItemTemplate>
                    <%# Eval("Aftercare") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtAftercare" Text='<%# Eval("Aftercare") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Categories">
                <ItemTemplate>
                    <%# Eval("Categories") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtCategories" Text='<%# Eval("Categories") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                    <%# Eval("Description") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtDescription" Text='<%# Eval("Description") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Uses">
                <ItemTemplate>
                    <%# Eval("Uses") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtUses" Text='<%# Eval("Uses") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FoliageColour">
                <ItemTemplate>
                    <%# Eval("FoliageColour") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Textbox runat="server" ID="txtFoliageColour" Text='<%# Eval("FoliageColour") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Flower Colour">
                <ItemTemplate>
                    <%# Eval("FlowerColour") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtFlowerColour" Text='<%# Eval("FlowerColour") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FloweringPeriod">
                <ItemTemplate>
                    <%# Eval("FloweringPeriod") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtFloweringPeriod" Text='<%# Eval("FloweringPeriod") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Height">
                <ItemTemplate>
                    <%# Eval("Height") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtHeight" Text='<%# Eval("Height") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Spread">
                <ItemTemplate>
                    <%# Eval("Spread") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtSpread" Text='<%# Eval("Spread") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Instructions">
                <ItemTemplate>
                    <%# Eval("Instructions") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtInstructions" Text='<%# Eval("Instructions") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Price">
                <ItemTemplate>
                    <%# Eval("Price") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtPrice" Text='<%# Eval("Price") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="SELECT * FROM [Plants]" 
        DeleteCommand="DELETE FROM [Plants] WHERE [PlantID] = ?" 
        InsertCommand="INSERT INTO [Plants] ([PlantID], [LatinName], [PopularName], [Season], [Aftercare], [Categories], [Description], [Uses], [FoliageColour], [FlowerColour], [FloweringPeriod], [Height], [Spread], [Instructions], [Price]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
        UpdateCommand="UPDATE [Plants] SET [LatinName] = ?, [PopularName] = ?, [Season] = ?, [Aftercare] = ?, [Categories] = ?, [Description] = ?, [Uses] = ?, [FoliageColour] = ?, [FlowerColour] = ?, [FloweringPeriod] = ?, [Height] = ?, [Spread] = ?, [Instructions] = ?, [Price] = ? WHERE [PlantID] = ?">
        <DeleteParameters>
            <asp:Parameter Name="PlantID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="PlantID" Type="Int32" />
            <asp:Parameter Name="LatinName" Type="String" />
            <asp:Parameter Name="PopularName" Type="String" />
            <asp:Parameter Name="Season" Type="String" />
            <asp:Parameter Name="Aftercare" Type="String" />
            <asp:Parameter Name="Categories" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Uses" Type="String" />
            <asp:Parameter Name="FoliageColour" Type="String" />
            <asp:Parameter Name="FlowerColour" Type="String" />
            <asp:Parameter Name="FloweringPeriod" Type="String" />
            <asp:Parameter Name="Height" Type="String" />
            <asp:Parameter Name="Spread" Type="String" />
            <asp:Parameter Name="Instructions" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="LatinName" Type="String" />
            <asp:Parameter Name="PopularName" Type="String" />
            <asp:Parameter Name="Season" Type="String" />
            <asp:Parameter Name="Aftercare" Type="String" />
            <asp:Parameter Name="Categories" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="Uses" Type="String" />
            <asp:Parameter Name="FoliageColour" Type="String" />
            <asp:Parameter Name="FlowerColour" Type="String" />
            <asp:Parameter Name="FloweringPeriod" Type="String" />
            <asp:Parameter Name="Height" Type="String" />
            <asp:Parameter Name="Spread" Type="String" />
            <asp:Parameter Name="Instructions" Type="String" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="PlantID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

Here is the code behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Staff : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindDataPlant();
    }
}

private void BindDataPlant()
{
    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath("~/App_Data/LincolnGardenCentre.accdb;"));

    try
    {
        Conn.Open();
        if (Conn.State.ToString() == "Open")
            Page.Title = "Connection Successful";
        else
            Page.Title = "Connection Unsuccessful";

        OleDbCommand Comm = new OleDbCommand();
        Comm.Connection = Conn;
        Comm.CommandText = "Select * FROM Plants;";

        OleDbDataAdapter DA = new OleDbDataAdapter();
        DA.SelectCommand = Comm;
        DataSet DS = new DataSet();
        OleDbCommandBuilder CB = new OleDbCommandBuilder(DA);
        DA.Fill(DS, "PLANTS");
    }
    catch (Exception e)
    {
        Response.Redirect("Error.aspx");
    }
    finally
    {
        Conn.Close();
        Conn.Dispose();
    }
}

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

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    e.Cancel = true;
    GridView1.EditIndex = -1;
    BindDataPlant();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

    int PlantID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

    TextBox txtLatinName = (TextBox)row.FindControl("txtLatinName");
    TextBox txtPopularName = (TextBox)row.FindControl("txtPopularName");
    TextBox txtSeason = (TextBox)row.FindControl("txtSeason");
    TextBox txtAftercare = (TextBox)row.FindControl("txtAftercare");
    TextBox txtCategories = (TextBox)row.FindControl("txtCategories");
    TextBox txtDescription = (TextBox)row.FindControl("txtDescription");
    TextBox txtUses = (TextBox)row.FindControl("txtUses");
    TextBox txtFoliageColour = (TextBox)row.FindControl("txtFoliageColour");
    TextBox txtFlowerColour = (TextBox)row.FindControl("txtFlowerColour");
    TextBox txtFloweringPeriod = (TextBox)row.FindControl("txtFloweringPeriod");
    TextBox txtHeight = (TextBox)row.FindControl("txtHeight");
    TextBox txtSpread = (TextBox)row.FindControl("txtSpread");
    TextBox txtInstructions = (TextBox)row.FindControl("txtInstructions");
    TextBox txtPrice = (TextBox)row.FindControl("txtPrice");

    OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath("~/App_Data/LincolnGardenCentre.accdb;"));

    try
    {
        Conn.Open();
        if (Conn.State.ToString() == "Open")
            Page.Title = "Connection Successful";
        else
            Page.Title = "Connection Unsuccessful";

        OleDbCommand Comm = new OleDbCommand();
        Comm.Connection = Conn;

        Comm.Parameters.Add("@PlantID", OleDbType.Integer).Value = PlantID.ToString();
        Comm.Parameters.Add("@LatinName", OleDbType.VarChar).Value = txtLatinName.Text;
        Comm.Parameters.Add("@PopularName", OleDbType.VarChar).Value = txtPopularName.Text;
        Comm.Parameters.Add("@Season", OleDbType.VarChar).Value = txtSeason.Text;
        Comm.Parameters.Add("@Aftercare", OleDbType.VarChar).Value = txtAftercare.Text;
        Comm.Parameters.Add("@Categories", OleDbType.VarChar).Value = txtCategories.Text;
        Comm.Parameters.Add("@Description", OleDbType.VarChar).Value = txtDescription.Text;
        Comm.Parameters.Add("@Uses", OleDbType.VarChar).Value = txtUses.Text;
        Comm.Parameters.Add("@FoliageColour", OleDbType.VarChar).Value = txtFoliageColour.Text;
        Comm.Parameters.Add("@FlowerColour", OleDbType.VarChar).Value = txtFlowerColour.Text;
        Comm.Parameters.Add("@FloweringPeriod", OleDbType.VarChar).Value = txtFloweringPeriod.Text;
        Comm.Parameters.Add("@Height", OleDbType.VarChar).Value = txtHeight.Text;
        Comm.Parameters.Add("@Spread", OleDbType.VarChar).Value = txtSpread.Text;
        Comm.Parameters.Add("@Instructions", OleDbType.VarChar).Value = txtInstructions.Text;
        Comm.Parameters.Add("@Price", OleDbType.Decimal).Value = txtPrice.Text;

        Comm.CommandText = "UPDATE Plants SET LatinName = @LatinName, PopularName = @PopularName, Season = @Season, Aftercare = @Aftercare, Categories = @Categories, Description = @Description, Uses = @Uses, FoliageColour = @FoliageColour, FlowerColour = @FlowerColour, FloweringPeriod = @FloweringPeriod, Height = @Height, Spread = @Spread, Instructions = @Instructions, Price = @Price WHERE PlantID = @PlantID";
        Comm.Connection = Conn;
        Comm.ExecuteNonQuery();

        GridView1.EditIndex = -1;
        BindDataPlant();
    }
    catch (Exception ex)
    {
        Response.Redirect("Error.aspx");
    }
    finally
    {
        Conn.Close();
        Conn.Dispose();
    }
}

}

Can anyone shed any light onto what is happening here?

Thanks

4

1 回答 1

0

删除网格标记中的 DataSourceId 并在 BindDataPlant 方法中绑定网格

GridView1.DataSource = DS;

GridView1.DataBind()

声明后 DA.Fill(DS, "PLANTS");

它会解决你的问题。

于 2013-04-17T17:21:16.260 回答