0

I am working on a college project and have a gridview that I have to add more rows to. I have two text fields and a button. Everything seems to be fine except the button part of the code in the .cs file What am I doing wring?

Its to make a list of Wines with an ID and a Title and a Year:

Wine.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Wine.aspx.cs" Inherits="WineNotProject2.AdminPages.Wine" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <br /><br />
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">

        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />

        </Columns>
    </asp:GridView>


<p><asp:Label ID="lblTitle" runat="server" Text="Wine Title"></asp:Label><br />
<asp:TextBox ID="txtTitle" runat="server" Width="176px"></asp:TextBox><br />

<p><asp:Label ID="lblYear" runat="server" Text="Wine Year"></asp:Label><br />
<asp:TextBox ID="txtYear" runat="server" Width="176px"></asp:TextBox><br />
</p>


<asp:Button ID="btnAdd" runat="server" Text="Add Wine" OnClick="btnAdd_Click" />

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [id], [Title], [Year] FROM [Wine]">
    </asp:SqlDataSource>


</asp:Content>

Wine.aspx.cs:

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

namespace WineNotProject2.AdminPages
{
    public partial class Wine : System.Web.UI.Page
    {
        private WineEntities10 ent2 = new WineEntities10();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RefreshGrid();
            }
        }
            private void RefreshGrid()
            {
                GridView2.DataSource = ent2.Wines.ToList();
                GridView2.DataBind();
            }
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                Wine w2 = new Wine();
                w2.Title = txtTitle.Text;
                w2.Year = txtYear.Text;
                ent2.Wines.AddObject(w2);
                ent2.SaveChanges();

            }

        }
    }

UPDATE:

The error message is:

Error 3 'WineNotProject2.AdminPages.Wine' does not contain a definition for 'Year' and no extension method 'Year' accepting a first argument of type 'WineNotProject2.AdminPages.Wine' could be found (are you missing a using directive or an assembly reference?) C:\Visual Studio 2010\Projects\WineNotProject7\WineNotProject2\AdminPages\Wine.aspx.cs 30 20 WineNotProject2

4

3 回答 3

1

I think you need to call RefreshGrid after you have added the new Wine:

protected void btnAdd_Click(object sender, EventArgs e)
{
    Wine w2 = new Wine();
    w2.Title = txtTitle.Text;
    w2.Year = txtYear.Text;
    ent2.Wines.AddObject(w2);
    ent2.SaveChanges();
    RefreshGrid(); // <-------
}

Maybe you also need to parse the year to int:

w2.Year = int.Parse(txtYear.Text);
于 2013-03-06T21:15:15.827 回答
0

Your code was looking good ,But you miss call the RefreshGrid() end of add button

for

protected void btnAdd_Click(object sender, EventArgs e)
            {
                Wine w2 = new Wine();
                w2.Title = txtTitle.Text;
                w2.Year = txtYear.Text;
                ent2.Wines.AddObject(w2);
                ent2.SaveChanges();
                **RefreshGrid**

            }

If ent2.SaveChanges(); is not working ,then use ent2.SubmitChanges();

于 2013-03-06T21:17:25.717 回答
0

The problem is you have two classes with same name Wine.

public partial class **Wine** : System.Web.UI.Page

**Wine** w2 = new **Wine**();

Rename ASP.Net page's name to WineList so that

its class name should become public partial class **WineList**

enter image description here

于 2013-03-07T04:43:50.583 回答