Link to home
Start Free TrialLog in
Avatar of coleenholley
coleenholleyFlag for United States of America

asked on

Using Unbound ASP.Net table inside a Repeater control

I have an ASP.Net Table inside a repeater control that I am using for data entery - since the Gridview is extremely difficult to use in this manner (opening with blank rows and NOT bound to an ObjectDataSource.)  I am having trouble getting the values from controls inside the ASP.Net Table (textboxes, or getting the row count, etc.) I will attach the ASP.Net code and the snippet of code where it is giving me the null object reference.  I know the object is Null, but I don't understand why it is null at this point (I have already entered a row of data in the table.) and the error is happening on the validation function that is supposed to validate the data that is entered into the table.  I'm getting very frusterated with this whold situation - I could really use some help!  Thanks!
Here is my ASP.Net Code:
<asp:Repeater ID="rptrReceiptsEntry" runat="server" EnableViewState="false"> 
     <HeaderTemplate>
	<asp:Table ID=tblRptrheader runat=server CssClass="table3" HorizontalAlign=center Width="100%">
		<asp:TableHeaderRow >
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblCarrFEIN" runat="server" Text="Carrier FEIN" ></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=155px Wrap="false"><asp:Label ID="lblCarrName" runat="server" Text="Carrier Name" ></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=135px Wrap="false"><asp:Label ID="lblModeTy" runat="server" Text="Mode Type"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=105px Wrap="false"><asp:Label ID="lblOrigCity" runat="server" Text="Origin City"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=105px Wrap="false"><asp:Label ID="lblOrigState" runat="server" Text="Origin State"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=105px Wrap="false"><asp:Label ID="lblDestCity" runat="server" Text="Dest City"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=105px Wrap="false"><asp:Label ID="lblDestState" runat="server" Text="Dest State"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblSellerFEIN" runat="server" Text="Seller's FEIN"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=100px Wrap="false"><asp:Label ID="lblRcvdFrom" runat="server" Text="Received From"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblDateRcvd" runat="server" Text="Date Received"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=105px Wrap="false"><asp:Label ID="lblBOL" runat="server" Text="BOL/Manifest"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblNetGal" runat="server" Text="Net Gallons"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblGrossGal" runat="server" Text="Gross gallons"></asp:Label></asp:TableHeaderCell>
			<asp:TableHeaderCell Width=90px Wrap="false"><asp:Label ID="lblBillGal" runat="server" Text="Billed Gallons"></asp:Label></asp:TableHeaderCell>
		</asp:TableHeaderRow>
	</asp:Table>
     </HeaderTemplate>
     <ItemTemplate>
	<asp:Table ID="tblRptrtable" runat="server" CssClass="table3">
		<asp:TableRow>
			<asp:TableCell><asp:TextBox ID="txtCarrFEIN" runat="server" Width=80px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtCarrName" runat="server" Width=150px></asp:TextBox></asp:TableCell>
			<asp:TableCell>
				<asp:DropDownList ID="ddlModeType" runat="server" Width=130px DatasourceID="odsuf_rtdata_mfmo" 
				 DataTextField="data_txt" DataValueField="key_txt" SelectedValue='<%# Eval("Mode Type") %>'>
				</asp:DropDownList>
			</asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtOrigCity" runat="server" Width=100px></asp:TextBox></asp:TableCell>
			<asp:TableCell>
				<asp:DropDownList ID="ddlOrigState" runat="server" Width=100px  DatasourceID="odsuf_rtdata_gsta" 
				 DataTextField="data_txt" DataValueField="key_txt" SelectedValue='<%# Eval("Orig State") %>'>
				</asp:DropDownList>
			</asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtDestCity" runat="server" Width=100px></asp:TextBox></asp:TableCell>
			<asp:TableCell>
                                <asp:DropDownList ID="ddlDestState" runat="server" Width=100px  DatasourceID="odsuf_rtdata_gsta" 
				 DataTextField="data_txt" DataValueField="key_txt" SelectedValue='<%# Eval("Dest State") %>'>
				</asp:DropDownList>
			</asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtSellerFEIN" runat="server" Width=80px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtRcvdFrom" runat="server" Width=100px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtDateRcvd" runat="server" Width=80px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtBOL" runat="server" Width=100px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtNetGal" runat="server" Width=80px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtGrossGal" runat="server" Width=80px></asp:TextBox></asp:TableCell>
			<asp:TableCell><asp:TextBox ID="txtBillGal" runat="server" Width=80px></asp:TextBox></asp:TableCell>
		</asp:TableRow>
	</asp:Table>
     </ItemTemplate>
</asp:Repeater>
 
and here is the code in my VB.net where it is bombing:
 
lsRowCount = Ctype(rptrReceiptsEntry.FindControl("tblRptrtable"), Table).Rows.Count

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

The table is defined for the Repeater item, so you need to use the item, and not the Repeater to use FindControl.

Bob
Avatar of coleenholley

ASKER

Okay - but the problem is that I need the ROW number from the table and this:

lsRowCount = Ctype(rptrReceiptsEntry.FindControl("tblRptrtable"), Table).Rows.Count

Does not work it gives me an "Object reference not set..." error on that piece of code.  

At this point in the program the repeater is NOT bound to anything, I don't want to bind it until it loops through the validation (which is where that code is) and then it will bind in the Save sub.  

The way I have it working on the Gridviews is that on click of the Save button it first calls the validation function, does all the validation to make sure that the data that will be passed to the COBOL RPC is correct.  

So I am wondering if even the table has been rendered inside the repeater it isn't recognizing it since it's not bound to anything yet?  If you have a snippet of code on how I can get the row count from the table inside the Repeater, that would be extremely helpful...Thanks!

Coleen
Coleen,

This is what I meant:

   
Bob
Dim tblRptrtable As Table = CType(Me.Repeater1.Items(0).FindControl("tblRptrtable"), Table)
 
    If tblRptrtable IsNot Nothing Then
      lsRowCount = tblRptrtable.Rows.Count
    End If

Open in new window

Should I do that inside my validation function or put it in the ItemDataBound event?
If you use it in the ItemDataBound event, then it would change to something like this:


Dim tblRptrtable As Table = CType(e.Item.FindControl("tblRptrtable"), Table)
 
    If tblRptrtable IsNot Nothing Then
      lsRowCount = tblRptrtable.Rows.Count
    End If

Open in new window

Thanks - I really didn't think I should do it on the ItemDataBound event because at the point where I need the row count, the table/repater hasn't been bound to anything yet, so it did not make sense to use ItemDataBound for this.  

I just tried this in my Validation function and I got this error: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" on this line of code:

Dim tblRptrtable As Table = CType(Me.rptrReceiptsEntry.Items(0).FindControl("tblRptrtable"), Table)

Any suggestions?
This all depends on where you validation function is defined and called.  It sounds like you don't have any Repeater items at that point.

Bob
Strange.  Okay here is the order of events at this point:
First, I create the table inside the Rpeater:  # 1 Below
Then on Click of Save I do some checking/getting values from the DDL's that the user makes the selections on and call the validate function: #2 Below (I didn't include the code after the call to the Validation Function - I can later if you need me to)
Then in the Validation Function I am checking everything that is entered in the table to make certain that it meets the correct criteria, but before I even get to the real validation part, I need to get the row cound and loop through all the rows that were entered so that each row of data can be verified.  That is where it is bombing - right at the beginning before I even do any validation #3:

So I am at a loss as to why this isn't finding the table - the save button isn't clicked until some data has been entered, so there should be a row found - which is why I'm wondering if it's because this hasn't been bound to an objectdatasource yet...
#1
Private function addBlankRows as DataTable
Dim dtBlankRows as New DataTable
Dim drBlankRows as DataRow
Dim i as Integer = 0
 
dtBlankRows.Columns.Add(New DataColumn("Carrier FEIN", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Carrier Name", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Mode Type", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Orig City", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Orig State", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Dest City", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Dest State", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Seller's FEIN", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Received From", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Date Received", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("BOL/Manifest", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Net Gallons", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Gross Gallons", GetType(String)))
dtBlankRows.Columns.Add(New DataColumn("Billed Gallons", GetType(String)))
		
For i = 0 to 9
	drBlankRows = dtBlankRows.NewRow()
	dtBlankRows.Rows.Add(drBlankRows)
Next
 
rptrReceiptsEntry.DataSource = dtBlankRows
rptrReceiptsEntry.DataBind()
 
Return 	dtBlankRows
End Function
 
#2
Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
'Changed to use the RPC Manager to connect 2/2008 - CH
Try
     Try
	io_misc_func = New misc_func()
     Catch
	'Destroy the objects.
         io_misc_func = Nothing
     End Try
            
     Dim ldt_request_data as New DataTable
     Dim ldr_request_data as DataRow
     Dim i as Integer = 0
     Dim lsFuelCd, lsSchTyCd As String 'lsReqData
     'All code for padding remmed 2/2008 - CH not needed to use RPC Manager
 
     lsFuelCd = ddl_fuelcd.SelectedItem.Text
     lsSchTyCd = ddl_schty.SelectedItem.Text
     psFuelTyCd = lsFuelCd
     psSchdTyCd = lsSchTyCd
       
     If lsFuelCd <> "" Then
        lsFuelCd = Left(lsFuelCd, 3).ToString()
     End If
        
     If (lsFuelCd.Trim() = "") Or (lsFuelCd.Trim() = "") Or (lsFuelCd.Trim() = "") Then
     'If fuel type or fuel code or schdule type is not selected then send out an error.
          psError = "<h3 style='COLOR: red; FONT-FAMILY: Arial'>Please make sure that you have selected Fuel Type, Fuel Code and Schedule Type.</h3>"
          lbl_error.Visible = True
          lbl_error.Text = psError
     Else
          btn_save.Enabled = False
 
          If Validate_repeater() = False Then
               btn_save.Enabled = True
               Return
          End If
      'More code below here....
 
#3
Private Function Validate_repeater() As Boolean
    Try
    Dim lsSchTyCd As String = ""
    Dim lsFuelCd As String = ""
    Dim psMatch As String = ""
    Dim lsTempDdata As String = ""
    Dim i As Integer
    Dim lsRowCount As Integer = 0
    Dim lsCallCode As String = ""
    Dim lsTemp As String = ""
    Dim lsTempRow As String = ""
    Dim lsTempErrmsg As String = ""
    'Data Entered Flag
    Dim lbDataEntered As Boolean = False
    Dim llPos As Integer = 0
	
    lsSchTyCd = ddl_schty.SelectedItem.Value
    lsFuelCd = ddl_fuelcd.SelectedItem.Text.ToString()
    'Retrieve the string of data from lbl_error.Text
    lsTempErrmsg = lbl_error.Text
    'This is to handle an error when "Add Button" is clicked to 
    'save an another set of records.       
'Tried this:    
'lsRowCount = Ctype(rptrReceiptsEntry.FindControl("tblRptrtable"), Table).Rows.Count 
  
    Dim tblRptrtable As Table = CType(Me.rptrReceiptsEntry.Items(0).FindControl("tblRptrtable"), Table)
 
    If tblRptrtable IsNot Nothing Then
      lsRowCount = tblRptrtable.Rows.Count
    End If
 
    For i = 0 To Ctype(rptrReceiptsEntry.FindControl("tblRptrtable"), Table).Rows.Count - lsRowCount
 
    'this is the beginning of the validation code:	
	If Not CType(rptrReceiptsEntry.FindControl("txtCarrFEIN"), TextBox).Text Is Nothing Then
            psCarrFEIN = CType(rptrReceiptsEntry.FindControl("txtCarrFEIN"), TextBox).Text
            psCarrFEIN = psCarrFEIN.Replace("-", "")
        Else
            psCarrFEIN = ""
        End If
     'Lots more validataion code below here...  

Open in new window

If you are dynamically creating a DataTable in memory, then are you persisting that in something like a Session variable, and then re-binding when the page posts back?

Bob
No.  Okay that would make sense, because the click of the Save does a post back. Ugh - this is where I suck at VB!  Suggestions?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! Tthat makes total sense - let me try it and get right back to you...
Works like a charm - thank you!.  Now I just need to get to it to give me back the values from the table in the validation portion.  Now I'm getting an error on this line of code:

CType(rptrReceiptsEntry.FindControl("txtCarrFEIN"), TextBox).Text

Is there a different way to get the values from inside the repeater table than there was with the GridView, or is this again because of the Postback on the Click of Save?  Will I have to put all my variables from the table in Session Variables?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep - you're right!  One of those (Duh!) moments!  Thank you so very much!
Thank you so much for your help - again!