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
  • Don't you need to call RefreshGrid in btnAdd_Click? Commented Mar 6, 2013 at 21:11
  • No I dont have to. But that way I can see if the button click added the info? Do you think it will work if I leave it out? Commented Mar 6, 2013 at 21:14
  • My problem seems to be at: w2.Year = txtYear.Text; ent2.Wines.AddObject(w2); Commented Mar 6, 2013 at 21:15
  • Why don't you need to tell the grid that the source has changed? Why do you think that the problem(what problem at all?) is at ent2.Wines.AddObject? You have to clarify your issue. Commented Mar 6, 2013 at 21:17

3 Answers 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);
Sign up to request clarification or add additional context in comments.

2 Comments

I will try but the problem is not with the refresh, but with the fact that the btnAdd doesn't even see the w2.Year from the wine.aspx page Sorry if I wasn't clear.
Visual Studio gives me an error: Does not contain a definition for Year. Sorry for not being clear enough. New at this.
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

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.