Solved

asp.net - issues with the gridview...

Posted on 2011-09-09
9
316 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:GlobaLevel
9 Comments
 
LVL 9

Expert Comment

by:Grant Spiteri
ID: 36513939
At the start of your for loop add an If statement to check if(dr.RowType!= DataRow) then continue;
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36513979
What is that statement suppose to do?
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36514028
Why would I not want it equal to a datarow?
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 36514296
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Author Comment

by:GlobaLevel
ID: 36514664
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

0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36514766
is this right?? looka datarow is a dot object

    if dr.RowType <> DataControlRowType.DataRow then
0
 
LVL 9

Expert Comment

by:Grant Spiteri
ID: 36515441
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
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36516149
  if dr.RowType <> DataControlRowType.DataRow then

....

isnt working...
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36517078
> if dr.RowType <> DataControlRowType.DataRow then

I think it needs to be

 if dr.RowType = DataControlRowType.DataRow then

Show us a screenshot of the grid, the values expected in DB and the values actually going to db.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now