Solved

Gridview Error

Posted on 2012-03-21
3
284 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
ID: 37750372
If zip is a textbox, then its value would be zip.Text rather than zip.ToString.
0
 

Author Comment

by:rcowen00
ID: 37750385
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
ID: 37750443
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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