Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2011, 02:28 AM
Registered User
 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Gridview not updating

Hi,

I hope someone can help me please. Basically I can't update my database after I have made my row editable. I don't get an error. Below is the relevant code for the aspx page:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView2_RowDataBound"
onrowediting="ExistingPredictionsGridView_RowEditi ng"
onrowupdating="ExistingPredictionsGridView_RowUpda ting">
<Columns>
<asp:CommandField showeditbutton="true"
headertext="Edit Controls"/>
<asp:BoundField DataField="Home_Team" HeaderText="Home Team" />
<asp:TemplateField HeaderText="Home Team">
<ItemTemplate>
<asp:Label ID="Home_Prediction" runat="server" Text='<%# Eval("Home_Prediction") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="HomeGoalsDropDownListExisting" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="">Select</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="HomeGoalsDropDownListRequiredValidator" runat="server"
ControlToValidate="HomeGoalsDropDownListExisting"
InitialValue=""
ErrorMessage="Please choose a home goal(s)">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team">
<ItemTemplate>
<asp:Label ID="Away_Prediction" runat="server" Text='<%# Eval("Away_Prediction") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="AwayGoalsDropDownListExisting" runat="server" AppendDataBoundItems="True">
<asp:ListItem Value="">Select</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="AwayGoalsDropDownListRequiredValidator" runat="server"
ControlToValidate="AwayGoalsDropDownListExisting"
InitialValue=""
ErrorMessage="Please choose an away goal(s)">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Away_Team" HeaderText="Away Team" />
</Columns>
</asp:GridView>

Below is the relevant code from the aspx.cs file:

protected void ExistingPredictionsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
using (PredictionsEntities1 myEntities = new PredictionsEntities1())
{

GridViewRow row = GridView2.Rows[e.RowIndex];
DropDownList ddHome = GridView2.Rows[e.RowIndex].FindControl("HomeGoalsDropDownListExisting") as DropDownList;
DropDownList ddAway = GridView2.Rows[e.RowIndex].FindControl("AwayGoalsDropDownListExisting") as DropDownList;


PredictionsTable myPredictionsTables;

MembershipUser currentUser;
currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
var WeekNoID = Convert.ToString(WeekNoDropDownList.SelectedValue) ;
myPredictionsTables = new PredictionsTable();
myPredictionsTables.HomePrediction = Convert.ToInt32(e.NewValues[ddHome.SelectedValue]);
myPredictionsTables.AwayPrediction = Convert.ToInt32(e.NewValues[ddAway.SelectedValue]);

myPredictionsTables.UserID = currentUserId;

GridView2.EditIndex = -1;
GridView2.DataBind();
myEntities.SaveChanges();

var yourPredictions = myEntities.sp_getViewYourPredictions(WeekNoID, currentUserId);
GridView2.DataSource = yourPredictions;
GridView2.DataBind(); //Shows the user the latest grid as a confirmation

}
}

Just to explain, but basically this page loads with a drop down list containing all the weeks of a football season. After the user makes a selection it will show either the first grid where the user can make predictions or the second grid above where they already have made their predictions but they can edit them before the deadline passes.

Thanks in advance,

Dave
 
Old October 20th, 2011, 02:12 PM
Registered User
 
Join Date: May 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Now sorted

I've managed to sort this myself.

Resolve and Close





Similar Threads
Thread Thread Starter Forum Replies Last Post
prob.with updating in gridview... knightmare102 ASP.NET 2.0 Professional 1 September 24th, 2007 08:52 AM
editing and updating a gridview sarah lee ASP.NET 2.0 Basics 2 December 22nd, 2006 05:45 AM
GridView not updating ropio ASP.NET 2.0 Basics 0 July 12th, 2006 10:52 AM
Editing Updating Sorting an Unbound Gridview Robby ASP.NET 2.0 Professional 5 March 22nd, 2006 10:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.