Solved

Gridview Error

Posted on 2012-03-21
3
282 Views
Last Modified: 2012-03-21
I have a gridview and am having some trouble referencing the ZipCode field.  

Code Behind
 Protected Sub btnAddSelectedCoverage_Click(sender As Object, e As EventArgs) Handles btnAddSelectedCoverage.Click
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            For Each gvRow As GridViewRow In gdvZipList.Rows
                Dim chkBox As CheckBox = CType(gvRow.FindControl("RowLevelCheckBox"), CheckBox)
                Dim zip As TextBox = CType(gvRow.FindControl("ZipCode"), TextBox)
                If chkBox.Checked Then
                    Dim cmd As New System.Data.SqlClient.SqlCommand
                    cmd.Connection = cnn
                    cmd.CommandType = Data.CommandType.StoredProcedure
                    cmd.CommandText = "dbo.VendorCoverageInsert"

                    cmd.Parameters.Add("@VendorId", Data.SqlDbType.VarChar).Value = Session("VendorId").ToString
                    cmd.Parameters.Add("@UserId", Data.SqlDbType.VarChar).Value = Session("VendorId").ToString
                    cmd.Parameters.Add("@Selected", Data.SqlDbType.Bit).Value = chkBox.Checked
                    cmd.Parameters.Add("@Coverage", Data.SqlDbType.Char).Value = zip.ToString

                    cnn.Open()
                    cmd.ExecuteScalar()
                    cnn.Close()
                End If
            Next
            Dim dt As New System.Data.DataTable
            Dim sRedirect As String = "VendorAddEditCoverage.aspx?VID=" + Session("VendorId").ToString
            Response.Redirect(sRedirect)
        Finally
            cnn.Close()
        End Try
    End Sub

Open in new window


Gridview
<asp:GridView ID="gdvZipList" runat="server" AutoGenerateColumns="False" 
                            CssClass="mGrid" Width="257px">
                            <Columns>
                                <asp:TemplateField HeaderText="Select">
                               
            <EditItemTemplate>
                                        <asp:CheckBox ID="RowLevelCheckBox" runat="server" Checked='<%# Bind("ZipCode") %>' />
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="RowLevelCheckBox" runat="server" Checked='<%# Bind("ZipCode") %>' 
                                            Enabled="true" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" />
                            </Columns>
                             
                        </asp:GridView>

Open in new window

0
Comment
Question by:rcowen00
  • 2
3 Comments
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
If zip is a textbox, then its value would be zip.Text rather than zip.ToString.
0
 

Author Comment

by:rcowen00
Comment Utility
I switched it to .text and still getting the Object reference not set to an instance of an object. error.
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
Oh, I see now the ZipCode field is a bound field. In that case you don't want to cast it as a textbox. You can access its value directly if you know the row and column. The row is whatever row the loop is currently on so...

cmd.Parameters.Add("@Coverage", Data.SqlDbType.Char).Value = gvRow.Cells(2).Text

...where 2 is the column number (zero index).
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

10 Experts available now in Live!

Get 1:1 Help Now