Solved

GridView does not update with changed data

Posted on 2012-12-29
2
298 Views
Last Modified: 2012-12-30
Hello,

My GridView is not updating, even though when I perform a load, add, edit, or delete I specifically call a subroutine (called Binddata) to refetch the data and rebind it. What could be wrong ?

The data is there, and when I do a page refresh it gets added (and sometimes will actually appear twice).

I am extra confused because I also do a Binddata in my Page load - so I should have already done 2 databinds - but none are appearing !

<script runat="server">
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)        
        
        If Not Page.IsPostBack Then

            MultiView1.ActiveViewIndex = 0
            
        End If

        ' Bind the datagrid        
        BindData
           
    End Sub

    Sub BindData()

        ' Prepare the datagrid
        Editgrid.Columns(0).Visible = "false"
        EditGrid.Columns(1).Visible = "false"
        EditGrid.Columns(2).HeaderText = Ttext(26)
        EditGrid.Columns(3).HeaderText = Ttext(27)
        EditGrid.Columns(4).HeaderText = Ttext(28)
        EditGrid.Columns(5).HeaderText = Ttext(29)
        EditGrid.Columns(6).HeaderText = Ttext(30)
        EditGrid.Columns(7).HeaderText = Ttext(13)

        ' Get data to fill datagrid
        Dim MySQL5 As String = "SELECT g.gamenum, l.shortname as Gamelocation, g.gamedatetime as Gamedates, g.gametype, lgh.groupname + ' : Practice', ct.lastname + ', ' + ct.firstname as Snackresp, ct1.lastname + ', ' + ct1.firstname as Refresp FROM games g, contacts ct, contacts ct1, locations l, leagues lgh, leagues lga, leaguesetup ls WHERE g.gamelocnum = l.locnum and g.snacknum = ct.usernum and g.refereenum = ct1.usernum and lgh.groupnum = g.hometeamnum and lga.groupnum = g.awayteamnum and g.leaguenum = " & Session("leaguenum") & " order by g.gamedatetime desc"
        Dim objCmd5 As New SqlDataAdapter(MySQL5, MyConn)
        objCmd5.Fill(ds, "gametable")
        EditGrid.DataSource = ds.Tables("gametable").DefaultView
        EditGrid.DataBind()
    End Sub
        
    
    Protected Sub AddItem(ByVal sender As Object, ByVal e As System.EventArgs)
         
	    Gamedates = picker2.Text
	    Gametimes = Gametime.Text
        
        cmd.Connection = MyConn
        cmd.Connection.Open()
        
        ' Insert the new item                    
        cmd.CommandText = "Insert into games (Leaguenum, Gametype, Gamelocnum, Gamedatetime, Hometeamnum, Awayteamnum, needref, refereenum, needsnack, snacknum, Gamestatus) values (" & Session("Leaguenum") & ", '" & Inputtype & "', " & Rinklocation.SelectedItem.Value & ",Convert(datetime,'" & gamedates & " " & Gametimes & "'), " & Hometeam.SelectedItem.Value & ", " & Awayteam.SelectedItem.Value & ", '" & needrefs.SelectedItem.Text & "', " & Refentry & ", '" & needsnacks.SelectedItem.Text & "'," & Snackentry & ", 'Not played')"
        cmd.ExecuteNonQuery()
        
        cmd.Connection.Close()
	
         BindData
        
    End Sub
    
    Protected Sub EditItem(ByVal sender As Object, ByVal e As System.EventArgs)
 
            cmd.Connection = MyConn
            cmd.Connection.Open()

            Gamenum = Request.QueryString("gnum")
		
            gamedates = picker2.Text
            Gametimes = Gametime.text
    
            cmd.CommandText = "Update games set Gametype =  '" & Gametype.SelectedItem.Text & "', Gamelocnum = " & Rinklocation.SelectedItem.Value & ", Gamedatetime = Convert(datetime,'" & gamedates & " " & Gametimes & "'), Gamestatus = 'Not played',  Eventtitle = '', Eventdesc = '', Hometeamnum = " & Hometeam.SelectedItem.Value & ", Awayteamnum = " & Awayteam.SelectedItem.Value & ", needref = '" & needrefs.SelectedItem.Text & "', refereenum = " & Refentry & ", needsnack = '" & needsnacks.SelectedItem.Text & "', snacknum = " & Snackentry & " where Gamenum = " & Session("myitemnum")
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            
             BindData
        
    End Sub
    
    Protected Sub DeleteItem(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
 
        ' Get the gamenum
        Dim selectedRecord As DataKey = EditGrid.DataKeys(EditGrid.SelectedIndex)
        Gamenum = selectedRecord.Value
    
        cmd.Connection = MyConn
        cmd.Connection.Open()
    
        ' Delete game 
        cmd.CommandText = "Update games set gamestatus = 'disabled' WHERE gamenum = " & Gamenum
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

         BindData
        
    End Sub
        
    Protected Sub LoadItem(ByVal sender As Object, ByVal e As EventArgs)
  
        Dim selectedRecord As DataKey = EditGrid.DataKeys(EditGrid.SelectedIndex)
        MyItemnum = selectedRecord.Value
        Session("myitemnum") = MyItemnum

        cmd.Connection = MyConn
        cmd.Connection.Open()
        cmd.CommandText = "Select Gametype, Gamelocnum, right(Convert(varchar,gamedatetime,0),7) as MyTime, Convert(varchar,gamedatetime,101)as DTT, Hometeamnum, Awayteamnum, needref, Refereenum, needsnack, snacknum from games where Gamenum = " & Session("myitemnum")
               MyReader = cmd.ExecuteReader()
		
                While MyReader.Read()
                    Agametype = MyReader("Gametype").ToString()
                    TGamelocnum = MyReader.GetInt32(1)
                    Gametime.Text = MyReader("MyTime").ToString()
                    picker2.Text = MyReader("DTT").ToString()
                        THometeamnum = MyReader("Hometeamnum")
                        TAwayteamnum = MyReader("Awayteamnum")
                        Tneedref = MyReader("needref")
                        Tneedsnack = MyReader("needsnack")
                        Tsnacknum = MyReader("snacknum")

                End While
		
                MyReader.Close()
                cmd.Connection.Close()
            
            ' Fill the datagrid
            BindData
 
    End Sub
     
</script>

<asp:Content ID="Content3" ContentPlaceHolderID="Mycontent" runat="Server">   
    
    <table>
        <tr>
            <td class="WizardTitle">
                <asp:Label ID="Pagetitle" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="WizardInstructions">
                <asp:Label ID="Helporerror" runat="server"></asp:Label></td>
        </tr>
        <tr>
            <td class="WizardContent">
                <div align="center">
                <table>
                    <tr>
                        <td style="padding: 5 0 20 0">
                            <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                                <asp:View ID="Instructions" runat="server">
                                    <h5>
                                        <asp:Label ID="GeneralInstructions" runat="server" />
                                    </h5>
                                </asp:View>
                                <asp:View ID="Managepages" runat="server">
                                        <asp:Panel ID="ShowForm" runat="server">
                                        <table class="InsideBox">
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="eventTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px; width:200px">
                                                    <asp:Label ID="eventtypeLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Gametype" OnSelectedIndexChanged="CheckEvent" AutoPostBack="true" Width="110" runat="server" />
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px; width:250px">
                                                    <asp:Label ID="gamedateLabel" runat="server" /><br />
                                                    <asp:TextBox ID="picker2" runat="server" Width="150" />&nbsp;&nbsp;<asp:Image ID="Image1" runat="server"
                                                        ImageUrl="~/images/calendar1.gif" ImageAlign="Middle" />
                                                    <ajaxToolkit:CalendarExtender runat="server" TargetControlID="picker2" 
                                                        PopupButtonID="Image1" />
                                                    <asp:RequiredFieldValidator ID="CompareValidator4" runat="server" ControlToValidate="picker2"
                                                        Display="None" ErrorMessage="You must enter a date" />
                                                    <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender4"  runat="server" TargetControlID="CompareValidator4" /> 
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px; width:250px">
                                                    <asp:Label ID="gametimeLabel" runat="server" Text="Game time"/><br />
                                                        <asp:TextBox ID="Gametime" runat="server" Width="150" />
                                                        <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="Gametime"
                                                            Mask="99:99" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
                                                            MaskType="Time" AcceptAMPM="true" ErrorTooltipEnabled="False" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2">
                                                    <asp:Label ID="locationLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Rinklocation" Width="150" runat="server" DataTextField="Shortname"
                                                        DataValueField="Locnum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td class="aptfont2">
                                                    <asp:Label ID="hometeamLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Hometeam" Width="200" runat="server" DataTextField="Hometeamname"
                                                        DataValueField="Hometeamnum" OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true">
                                                    </asp:DropDownList> 
                                                </td>
                                                <td class="aptfont2">
                                                    <asp:Label ID="awayteamLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Awayteam" Width="200" runat="server" DataTextField="Awayteamname"
                                                        DataValueField="Awayteamnum" OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true">
                                                    </asp:DropDownList> 
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="refTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px"> 
                                                    <asp:Label ID="refreqdLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="needrefs" Width="110" runat="server" OnSelectedIndexChanged="DisplayRef" AutoPostBack="true" >
                                                    </asp:DropDownList>
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px"> 
                                                    <asp:Label ID="gamerefLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Gameref" Width="200" runat="server" DataTextField="Fullname"
                                                        DataValueField="Usernum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="snacksTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" style="text-align:left; font-size: 10pt">
                                                    <asp:Label ID="informLabel" runat="server" />
                                                </td>        
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px" >                                                                                           
                                                    <asp:Label ID="snacksreqdLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="needSnacks" Width="110" runat="server"  OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true" />
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px">                                                                                           
                                                    <asp:Label ID="snackLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Snacks" Width="200" runat="server" DataTextField="Players"
                                                        DataValueField="Usernum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="4" align="right">
                                                    <asp:Button ID="insertButton" OnClick="AddItem" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="updateButton" OnClick="EditItem" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="canceladdButton" CausesValidation="False" OnClick="CancelAdd" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="canceleditButton" OnClick="CancelEdit" runat="server" />
                                                </td>
                                            </tr>
                                        </table>
                                        </asp:Panel>
                                        <table>
                                        <tr>
                                            <td class="AdminTitle">
                                                <asp:Label ID="schedulegridLabel" runat="server" /></td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div style="width: 725; height: 300; overflow: auto" align="center">
                                                    <asp:GridView ID="EditGrid" runat="server" Width="700" CellPadding="3" 
                                                        DataKeyNames="gamenum" RowStyle-Font-Size="10pt" HeaderStyle-Font-Size="10pt"
                                                        HeaderStyle-ForeColor="white" HeaderStyle-BackColor="SteelBlue" AutoGenerateColumns="false"
                                                        OnSelectedIndexChanged="LoadItem" allowPaging="true" PageSize="30" OnRowDeleting="DeleteItem">
                                                        <Columns>
                                                            <asp:CommandField ItemStyle-Width="70" ShowSelectButton="true" ButtonType="Button"
                                                                ItemStyle-HorizontalAlign="Center" ControlStyle-Font-Size="9pt" ControlStyle-BackColor="steelblue"
                                                                ControlStyle-ForeColor="white" />
                                                            <asp:TemplateField ShowHeader="False" ItemStyle-Width="60"  ItemStyle-HorizontalAlign="Center">
                                                                <ItemTemplate>
                                                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                                                        OnClientClick='return confirm("Are you sure you want to delete this entry?");'
                                                                        Text="Delete" Commandname="Delete" CommandArgument='<%# Eval("gamenum") %>'  />
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:BoundField DataField="Gametype" ItemStyle-Font-Size="11px" ItemStyle-Width="75" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:TemplateField ItemStyle-Font-Size="11px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" >
                                                                <ItemTemplate>
                                                                    <asp:Label ID="myDates" runat="server" Text='<%# MyTools.Makedate(Eval("Gamedates"),"shorttime") %>' />
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:BoundField DataField="Gamelocation" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:BoundField DataField="Teams" ItemStyle-Font-Size="11px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:BoundField DataField="Snackresp" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" />                                                      
                                                            <asp:BoundField DataField="Refresp" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" /> 
                                                        </Columns>
                                                    </asp:GridView>
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </asp:View> 
                            </asp:MultiView>
                        </td>
                    </tr>
                </table>
                </div>
            </td>
        </tr>
    </table>
</asp:Content>

Open in new window

0
Comment
Question by:pbissegger
2 Comments
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38729026
Try inside the not page.ispostback
If Not Page.IsPostBack Then

            MultiView1.ActiveViewIndex = 0

       ' Bind the datagrid        
        BindData()            
        End If

Open in new window

0
 

Author Comment

by:pbissegger
ID: 38731630
Seems to have solved the issue ... although for the life of me, I do not understand why not updating the GridView solves the problem that the GridView is not updating...

Peter
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question