对象引用未设置为DataReader

时间:2016-02-26 13:54:45

标签: vb.net

我有一个excel文档,我尝试用数据库中的值填充表单,我有一些DropDownList创建就像那样

<ContentTemplate>
                &nbsp; <asp:DropDownList ID="DDL_Transporter" runat="server" Width = "157px" 
                        DataSourceID="SqlDataSource2" DataTextField="transporter" 
                        DataValueField="transporter">
                        <asp:ListItem></asp:ListItem>
                </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="Data Source=xxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx" 
                        SelectCommand="SELECT [transporter] FROM [Transporter] where [active]= 'active'"></asp:SqlDataSource>
                </ContentTemplate>

我不知道问题出在哪里,但当我尝试填写所有框时会显示错误:

  

&#39; /&#39;中的服务器错误应用

     

对象引用未设置为对象的实例。

     

第46行:结束尝试第47行:第48行:
  如果Header_DataReader.HasRows那么第49行:做   而Header_DataReader.Read第50行:

我的代码在这里:

如果Page.IsPostBack = False那么

            Dim con As New SqlConnection
            Dim cmd As New SqlCommand

            con = FunctionConnection()
            cmd.Connection = con

            cmd.CommandText = "GetFillData"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@OrderID", RowID)
            Dim Header_DataReader As SqlDataReader = Nothing
            Try
                con.Open()
                Header_DataReader = cmd.ExecuteReader
            Catch ex As Exception

            End Try

            If Header_DataReader.HasRows Then
                Do While Header_DataReader.Read

                    BOX_SampleOrder.Enabled = False
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("DVMOrderNumber")) = False Then
                        BOX_SampleOrder.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("DVMOrderNumber"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("Requester")) = False Then
                        BOX_Requester.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("Requester"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ProjectNumber")) = False Then
                        BOX_ProjectNumber.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ProjectNumber"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("CostResponsable")) = False Then
                        BOX_PersonInvoiced.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("CostResponsable"))
                    End If
                    BOX_PersonInvoiced.Text = Generals.TrimMyString(BOX_PersonInvoiced.Text)

                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("SampleOrderType")) = False Then
                        DDL_SampleCategory.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("SampleOrderType"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("TotalSamples")) = False Then
                        BOX_Quantity.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("TotalSamples"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("CostCenter")) = False Then
                        BOX_CostCenter.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("CostCenter"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("OrderDescription")) = False Then
                        BOX_Description.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("OrderDescription"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("FinalCustomerAddress")) = False Then
                        BOX_RecipientName.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("FinalCustomerAddress"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("TargetDate")) = False Then
                        BOX_DesiredDeliveryDate.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("TargetDate"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("AssemblyPlannedDate")) = False Then
                        BOX_AssemblyPlanedDate.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("AssemblyPlannedDate"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("LogisticsResponsable")) = False Then
                        DDL_Coordonator.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("LogisticsResponsable"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ShippingMode")) = False Then
                        DDL_ShippingMode.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ShippingMode"))
                    End If
                    DDL_Transporter.Items.Clear()
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ExpressShipment")) = False Then
                        DDL_Transporter.Items.Add(Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ExpressShipment")))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ShippingAddress")) = False Then
                        BOX_ShippingAddress.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ShippingAddress"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("Urgent")) = False Then
                        RBL_Urgent.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("Urgent"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("HazardousMaterial")) = False Then
                        RBL_Hazardous.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("HazardousMaterial"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("HasBOM")) = False Then
                        RBL_BOM.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("HasBOM"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("HasSerialComponents")) = False Then
                        RBL_SerialComponents.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("HasSerialComponents"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("PrototypeComponents")) = False Then
                        RBL_PrototypeComponents.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("PrototypeComponents"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ReadyForAssembly")) = False Then
                        RBL_ReadyAssembly.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ReadyForAssembly"))
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("Status")) = False Then
                        DDL_OrderStatus.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("Status"))
                    Else
                        DDL_OrderStatus.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("OrderType")) = False Then
                        DDL_OrderCategory.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("OrderType"))
                    Else
                        DDL_OrderCategory.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ExtraQty")) = False Then
                        BOX_ExtraQuantity.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ExtraQty"))
                    Else
                        BOX_ExtraQuantity.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("PackagingType")) = False Then
                        DDL_PackagingType.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("PackagingType"))
                    Else
                        DDL_PackagingType.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("SampleLabeling")) = False Then
                        BOX_SampleLabeling.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("SampleLabeling"))
                    Else
                        BOX_SampleLabeling.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("AssemblySite")) = False Then
                        DDL_AssemblySite.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("AssemblySite"))
                    Else
                        DDL_AssemblySite.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("AssemblyDate")) = False Then
                        BOX_AssemblyDate.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("AssemblyDate"))
                    Else
                        BOX_AssemblyDate.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("Destination")) = False Then
                        BOX_Destination.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("Destination"))
                    Else
                        BOX_Destination.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ShippmentPlannedDate")) = False Then
                        BOX_ShippmentPlannedDate.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ShippmentPlannedDate"))
                    Else
                        BOX_ShippmentPlannedDate.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ShippmentDate")) = False Then
                        BOX_ShippmentDate.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ShippmentDate"))
                    Else
                        BOX_ShippmentDate.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ShippingCost")) = False Then
                        BOX_ShippingCost.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ShippingCost"))
                    Else
                        BOX_ShippingCost.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("MaterialCost")) = False Then
                        BOX_TestingCost.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("MaterialCost"))
                    Else
                        BOX_TestingCost.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("ExtraNotice")) = False Then
                        BOX_ExtraNotice.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("ExtraNotice"))
                    Else
                        BOX_ExtraNotice.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("DelayMotive")) = False Then
                        BOX_DelayMotive.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("DelayMotive"))
                    Else
                        BOX_DelayMotive.Text = Nothing
                    End If
                    If Header_DataReader.IsDBNull(Header_DataReader.GetOrdinal("Comments")) = False Then
                        BOX_Comments.Text = Header_DataReader.GetValue(Header_DataReader.GetOrdinal("Comments"))
                    Else
                        BOX_Comments.Text = Nothing
                    End If
                Loop
            End If



            If con.State = ConnectionState.Open Then
                con.Close()
            End If
end if

1 个答案:

答案 0 :(得分:0)

显然此代码存在问题:

con.Open()
Header_DataReader = cmd.ExecuteReader

Header_DataReader的值仍为Nothing 您可以捕获异常以查看执行命令

的错误

或者你可以这样做:

If Header_DataReader IsNot Nothing Then
If Header_DataReader.HasRows Then


End If
End If