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
|