Link to home
Start Free TrialLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on

asp.net - issues with the gridview...

when the user makes selections in the gridview during runtime it doesnt submit every row that they select, but it renadomly selects and applies to the db...
Protected Sub Submit_Picks_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
        
        
        ' find username
        
        Dim cb_home_team_spread As String
        Dim cb_away_team_spread As String
        Dim cb_col1_1st_star As String
        Dim cb_col1_2nd_star As String
        Dim cb_col1_3rd_star As String
        Dim cb_col1_lock As String
        Dim cb_col1_parley As String
        Dim cb_col2_over_spread As String
        Dim cb_col2_under_spread As String
        Dim cb_col2_1st_star As String
        Dim cb_col2_2nd_star As String
        Dim cb_col2_3rd_star As String
        Dim cb_col2_lock As String
        Dim cb_col2_parley As String
               
        Dim i As Long
        i = 1
        For Each dr As GridViewRow In GridView1.Rows
                       
            Dim k As String = dr.RowIndex
            
            
            If (CType(dr.FindControl("checkbox1"), CheckBox)).Checked Then
                cb_home_team_spread = "True"
            Else
                cb_home_team_spread = ""
            End If
            
            If (CType(dr.FindControl("checkbox2"), CheckBox)).Checked Then
                cb_away_team_spread = "True"
            Else
                cb_away_team_spread = ""
            End If
            
            If (CType(dr.FindControl("checkbox3"), CheckBox)).Checked Then
                cb_col1_1st_star = "True"
            Else
                cb_col1_1st_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox4"), CheckBox)).Checked Then
                cb_col1_2nd_star = "True"
            Else
                cb_col1_2nd_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox13"), CheckBox)).Checked Then
                cb_col1_3rd_star = "True"
            Else
                cb_col1_3rd_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox5"), CheckBox)).Checked Then
                cb_col1_lock = "True"
            Else
                cb_col1_lock = ""
            End If
            
                      
            If (CType(dr.FindControl("checkbox7"), CheckBox)).Checked Then
                cb_col2_over_spread = "True"
            Else
                cb_col2_over_spread = ""
            End If
            
            If (CType(dr.FindControl("checkbox8"), CheckBox)).Checked Then
                cb_col2_under_spread = "True"
            Else
                cb_col2_under_spread = ""
            End If
            
            If (CType(dr.FindControl("checkbox9"), CheckBox)).Checked Then
                cb_col2_1st_star = "True"
            Else
                cb_col2_1st_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox10"), CheckBox)).Checked Then
                cb_col2_2nd_star = "True"
            Else
                cb_col2_2nd_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox11"), CheckBox)).Checked Then
                cb_col2_3rd_star = "True"
            Else
                cb_col2_3rd_star = ""
            End If
            
            If (CType(dr.FindControl("checkbox12"), CheckBox)).Checked Then
                cb_col2_lock = "True"
            Else
                cb_col2_lock = ""
            End If
            
        
            
                      
            ' --------------------------------------------------------------------------------------------------------------
            ' DATA VALIDATION
            ' ----------------------------------------------------------------------------------------
            
            If cb_home_team_spread <> "" And cb_away_team_spread <> "" Then
                Label9.Text = "You can only select HOME_TEAM_SPREAD or AWAY_TEAM_SPREAD, not both"
                Exit Sub
            End If
            
            If cb_col2_over_spread <> "" And cb_col2_under_spread <> "" Then
                Label9.Text = "You can only select OVER_SPREAD or UNDER_SPREAD, not both"
                Exit Sub
            End If
            
            If cb_home_team_spread <> "" And cb_away_team_spread <> "" And cb_col1_1st_star = "" Then
                Label9.Text = "When selecting HOME_TEAM_SPREAD, you must also select a star or lock."
                Exit Sub
            End If
            
            If cb_home_team_spread = "" And cb_away_team_spread <> "" And cb_col1_1st_star = "" Then
                Label9.Text = "When selecting AWAY_TEAM_SPREAD, you must also select a star or lock."
                Exit Sub
            End If
            
            
            
            
            ' --------------------------------------------------------------------------------------------------------------
            ' POPULATE  LEADERBOARD_Dog_show
            ' ----------------------------------------------------------------------------------------
            
            If cb_home_team_spread <> "" Or cb_away_team_spread <> "" Then
            
            
                      
                Dim BSP_ID As String
                BSP_ID = Session("sessionBSP_ID")
                Dim SQLConn1 As SqlConnection = New SqlConnection()
                Dim cmd1 As SqlCommand
            
            
                Dim SQLStr1 As String
                Dim Err1 As String

                SQLConn1.ConnectionString = "Network Library=DBMSSOCN; Data Source=xx.xx.xx.xx; Initial Catalog=cvb; Trusted_Connection=true;User ID=; Password="
                
                
                
                SQLStr1 = ("INSERT INTO GAME_TRACKER_dog_show (BSP_ID,USERNAME,GAME_ID,HOME_TEAM_SPREAD,AWAY_TEAM_SPREAD,COL_1_1ST_STAR,COL_1_2ND_STAR, COL_1_3RD_STAR,COL_1_LOCK, OVER_SPREAD,UNDER_SPREAD,COL_2_1ST_STAR,COL_2_2ND_STAR,COL_2_3RD_STAR,COL_2_LOCK) VALUES " & _
                              "(@BSP_ID,@USERNAME,@GAME_ID,@HOME_TEAM_SPREAD,@AWAY_TEAM_SPREAD,@COL_1_1ST_STAR,@COL_1_2ND_STAR, @COL_1_3RD_STAR,@COL_1_LOCK, @OVER_SPREAD,@UNDER_SPREAD,@COL_2_1ST_STAR,@COL_2_2ND_STAR,@COL_2_3RD_STAR,@COL_2_LOCK) ")
                SQLConn1.Open()
                cmd1 = New SqlCommand(SQLStr1, SQLConn1)
                cmd1.Parameters.AddWithValue("@BSP_ID", Session("BSP_ID"))
                cmd1.Parameters.AddWithValue("@USERNAME", Session("SessionUsername"))
                cmd1.Parameters.AddWithValue("@GAME_ID", Session("Game_id" & k))
                cmd1.Parameters.AddWithValue("@HOME_TEAM_SPREAD", cb_home_team_spread)
                cmd1.Parameters.AddWithValue("@AWAY_TEAM_SPREAD", cb_away_team_spread)
                cmd1.Parameters.AddWithValue("@COL_1_1ST_STAR", cb_col1_1st_star)
                cmd1.Parameters.AddWithValue("@COL_1_2ND_STAR", cb_col1_2nd_star)
                cmd1.Parameters.AddWithValue("@COL_1_3RD_STAR", cb_col1_3rd_star)
                cmd1.Parameters.AddWithValue("@COL_1_LOCK", cb_col1_lock)
                ' cmd1.Parameters.AddWithValue("@COL_1_PARLEY", cb_col1_parley)
                cmd1.Parameters.AddWithValue("@OVER_SPREAD", cb_col2_over_spread)
                cmd1.Parameters.AddWithValue("@UNDER_SPREAD", cb_col2_under_spread)
                cmd1.Parameters.AddWithValue("@COL_2_1ST_STAR", cb_col2_1st_star)
                cmd1.Parameters.AddWithValue("@COL_2_2ND_STAR", cb_col2_2nd_star)
                cmd1.Parameters.AddWithValue("@COL_2_3RD_STAR", cb_col2_3rd_star)
                cmd1.Parameters.AddWithValue("@COL_2_LOCK", cb_col2_lock)
                ' cmd1.Parameters.AddWithValue("@COL_2_PARLEY", cb_col2_parley)
                
                i = i + 1
                
                Try
                    Err1 = cmd1.ExecuteNonQuery()
                Catch ex As Exception
                Finally
                    SQLConn1.Close()
                End Try
        
                
            End If
                
        Next
        
        Label2.Text = "Your Pick(s) have been submitted. You can view your picks on Game_Tracker_dog_show off the Reports Menu."
        
    End Sub

Open in new window

Avatar of Grant Spiteri
Grant Spiteri
Flag of Australia image

At the start of your for loop add an If statement to check if(dr.RowType!= DataRow) then continue;
Avatar of GlobaLevel

ASKER

What is that statement suppose to do?
Why would I not want it equal to a datarow?
Avatar of Miguel Oz
gspiteri is correct. You need to loop anly data rows, the GridView1.Rows
 contains all rows including headers, but I do not think that is your real issue.

Can you post your markup/relevant code behind regarding binding? I need to check how you keep state and how you bind your gridview to the data.
Okay here is how the gridview is created...on button click
' --------------------------------------------------------------------------------------------------------------
        ' POPULATE  GRIDVIEW 
        ' ----------------------------------------------------------------------------------------
        
        Try
            Dim BSP_ID As String
            BSP_ID = Session("SessionBSP_ID")
            Dim sqlConnection As String
  
            Dim now_date_var As String
            now_date_var = Now.Date.ToString()
            
            Using sqlConn As New SqlConnection
                 sqlConnection = "Data Source=Wxxxx;Initial Catalog=Bxxxxcks;User ID=bxxxr;Password=bsxxxxx!"
                sqlConn.ConnectionString = sqlConnection
                Dim SQLstring As String = "SELECT game_time, away_team,home_team,  away_team_spread,home_team_spread, over_spread,under_spread,game_id FROM  schedule_dog_show WHERE game_time >= '" & now_date_var & "'"
                ' Dim SQLstring As String = "SELECT game_time, away_team,home_team,  away_team_spread,home_team_spread, over_spread,under_spread,game_id FROM  schedule_dogshow"
                Dim TblAdaptar As New SqlDataAdapter(SQLstring, sqlConn)
                Dim Tbl As New DataSet
                Tbl.Clear()
                Try
                    TblAdaptar.Fill(Tbl)
                    Me.GridView1.DataSource = Nothing
                    Me.GridView1.DataSource = Tbl
                    Me.GridView1.DataBind()
                Catch ex As Exception
                End Try
            End Using
        Catch ex As Exception
        End Try



========================================================


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Game_Id"
        CellPadding="4" ForeColor="#333333" 
        onrowdatabound="GridView1_RowDataBound" 
         onrowupdating="chkD2Status_CheckedChanged"     
        AllowPaging="True" AllowSorting="True" Height="200px" Width="900px" 
        Font-Size="Small" style="margin-right: 57px">
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="Game_Time" HeaderText="Game Time" ReadOnly="True" 
                    SortExpression="Game_Time" />
                <asp:TemplateField HeaderText="Away Team/Home Team">
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"home_team") %>' CssClass="MakePicks_col_2_sec_1" ></asp:Label>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
                        <asp:Label ID="Label6" runat="server" Text="@" Font-Bold="True" CssClass="MakePicks_col_2_sec_2" ></asp:Label>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>
                        <asp:Label ID="Label7" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"away_team") %>' CssClass="MakePicks_col_2_sec_3" ></asp:Label>
                        </b>
                        <br />
                        <b>
                        <asp:CheckBox ID="CheckBox1" runat="server"  Text='<%#DataBinder.Eval(Container.DataItem,"home_team_spread") %>' CssClass="MakePicks_col_2_sec_1"></asp:CheckBox>
                      
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:CheckBox ID="CheckBox2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"away_team_spread") %>' CssClass="MakePicks_col_2_sec_3"></asp:CheckBox>
               
                        <br />
                        <br />
                        </b>
                        <asp:CheckBox ID="CheckBox3" runat="server" Text="" CssClass="MakePicks_col_2_sec_1"   oncheckedchanged="chkD1Select_CheckedChanged" AutoPostBack="true" />
                       <img id="Img1" style="height:10px;width:10px" src="https://www.bexxxxks.com/Content/Images/yellow_star.jpg" class="MakePicks_col_2_sec_1_star" alt="" /> 
                       
                        <asp:CheckBox ID="CheckBox4" runat="server" Text="" CssClass="MakePicks_col_2_sec_2"  oncheckedchanged="chkD2Select_CheckedChanged" AutoPostBack="true" />
                        <img id="Img2" style="height:10px;width:10px" src="https://www.bxxxxcks.com/Content/Images/yellow_star.jpg" class="MakePicks_col_2_sec_2_star" alt="" /> 
                        <asp:CheckBox ID="CheckBox13" runat="server" Text="" CssClass="MakePicks_col_2_sec_3"  oncheckedchanged="chkD3Select_CheckedChanged" AutoPostBack="true"   />
                        <img id="Image2" style="height:10px;width:10px" src="https://www.betxxxxks.com/Content/Images/yellow_star.jpg" class="MakePicks_col_2_sec_3_star" alt="" /> 
                        <br />
                        <br />
                        <asp:CheckBox ID="CheckBox5" runat="server" Text="L" CssClass="MakePicks_col_2_sec_2"  oncheckedchanged="chkD4Select_CheckedChanged" AutoPostBack="true"   />
                        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>
                        <%--<asp:CheckBox ID="CheckBox6" runat="server" Text="P" CssClass="MakePicks_col_2_sec_3"  oncheckedchanged="chkD5Select_CheckedChanged" AutoPostBack="true" />--%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Over/Under">
                    <ItemTemplate>
                       		</b>
                        <br />
                        <b>
                        <asp:CheckBox ID="CheckBox7" runat="server"  Text='<%#DataBinder.Eval(Container.DataItem,"over_spread") %>' CssClass="MakePicks_col_3_sec_1"></asp:CheckBox>
                      
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:CheckBox ID="CheckBox8" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"under_spread") %>' CssClass="MakePicks_col_3_sec_3"></asp:CheckBox>
               
                        <br />
                        <br />
                        </b>
                        <asp:CheckBox ID="CheckBox9" runat="server" Text="" CssClass="MakePicks_col_3_sec_1" oncheckedchanged="chkD6Select_CheckedChanged" AutoPostBack="true"  />
                        <img id="Image2" style="height:10px;width:10px" src="https://www.xxxxs.com/Content/Images/yellow_star.jpg" class="MakePicks_col_3_sec_1_star" alt="" /> 
         
                        <asp:CheckBox ID="CheckBox10" runat="server" Text="" CssClass="MakePicks_col_3_sec_2" oncheckedchanged="chkD7Select_CheckedChanged" AutoPostBack="true"  />
                       <img id="Img3" style="height:10px;width:10px" src="https://www.bexxxxxicks.com/Content/Images/yellow_star.jpg" class="MakePicks_col_3_sec_2_star" alt="" /> 
                        <asp:CheckBox ID="CheckBox11" runat="server" Text="" CssClass="MakePicks_col_3_sec_3" oncheckedchanged="chkD8Select_CheckedChanged" AutoPostBack="true"  />
                      <img id="Img4" style="height:10px;width:10px" src="https://www.betxxx.com/Content/Images/yellow_star.jpg" class="MakePicks_col_3_sec_3_star" alt="" /> 
                        <br />
                        <br />
                        <asp:CheckBox ID="CheckBox12" runat="server" Text="L" CssClass="MakePicks_col_3_sec_2"  oncheckedchanged="chkD9Select_CheckedChanged" AutoPostBack="true" />
                        <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>
                       <%-- <asp:CheckBox ID="CheckBox14" runat="server" Text="P" CssClass="MakePicks_col_3_sec_3"  oncheckedchanged="chkD10Select_CheckedChanged" AutoPostBack="true" />
--%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Game_id" HeaderText="Game_id" SortExpression="Game_Id" Visible="false"  />                    
             
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:hiddenfield ID="HF_ItemID" runat="Server" />
                    </ItemTemplate>                
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <EmptyDataTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </EmptyDataTemplate>
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

Open in new window

is this right?? looka datarow is a dot object

    if dr.RowType <> DataControlRowType.DataRow then
yes then the next line continue or wrap the rest of the syntax within the if statement but have it as an equal the other way is alot cleaner though
  if dr.RowType <> DataControlRowType.DataRow then

....

isnt working...
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial