Solved

asp.net - issues with the gridview...

Posted on 2011-09-09
9
317 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Runtime Error 2 28
Error on link 14 39
I need help making a collection from a class in VB.Net 3 27
Not seen Link button 5 17
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

15 Experts available now in Live!

Get 1:1 Help Now