Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic textbox error Collection was modified; enumeration operation

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
Hi. I going thorough a datareader and at the same time dynamically creating text boxes and I'm getting an error Collection was modified; enumeration operation may not execute

What I'm trying to do is display a list of items (books).
Title, Author, price. I'm doing this with a datareader.

I want to add a textbox as the reader reads so they can enter the qty they want and then when the hit a button collect all the TB's that have a qty.

Problem is with creating the TB's

thanks



 
Where and how are you adding the textboxes? Can you show your code so we can help?
 
Well I don't know if I made it better or worse. Now all the TB's are dumping out on the bottom of my form but I at least got past the error. How can I get them inside the "<td>" cell


Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmd As New OleDbCommand
Dim DRClassics As OleDbDataReader

Dim conn As New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
cmd.CommandText = ("Select author, title,isbn,price from books where active=0 and category='classics' and indx <= 20 order by author, title  ")
 cmd.Connection = conn
    conn.Open()

 DRClassics = cmd.ExecuteReader

Response.Write("<table border='1'>")

  Dim iSBN As String = Nothing
 Dim tb As TextBox
   While DRClassics.Read
                
    tb = New TextBox
    tb.ID = DRClassics("ISBN").ToString
                
    iSBN = DRClassics("isbn")
     Response.Write("<tr><td  width='250px' class='bodylist'>")
Response.Write("<font color='darkblue' size='1'>" & DRClassics(0))
Response.Write("<td width='250px'><font color='darkblue' size='1'>" & DRClassics("title"))
Response.Write("<td><font color='darkblue' size='1'>" & DRClassics("ISBN"))
Response.Write("<td><font color='darkblue' size='1'>")
.Write(DRClassics("Price"))
Response.Write("<td>")
form1.Controls.Add(tb)
            
 End While

  Response.Write("</table>")

 End Sub
 
First, using response.write to output to your page is bad practice. You should create the objects you need and add them to the page's controls collection.
Second, you don't need a datareader for this. A grid would be better or a repeater.
 
Why are you not a using a repeater control to do this?

Try the code(not tested) below not sure if this code below will work.
Code:
     Response.Write("<tr><td  width='250px' class='bodylist'>")
Response.Write("<font color='darkblue' size='1'>" & DRClassics(0))
Response.Write("<td width='250px'><font color='darkblue' size='1'>" & DRClassics("title"))
Response.Write("<td><font color='darkblue' size='1'>" & DRClassics("ISBN"))
Response.Write("<td><font color='darkblue' size='1'>")
.Write(DRClassics("Price"))
form1.Controls.Add(tb)
Response.Write("<td></tr>")

Sunil
 
Ok I'll go give the gridview a try. Can I just add an input fieldd to the grid.
 
cool, I already added the textbox. I might have te same probelm with giving it a different name though. I may be able to key of the isbn fies as its a unique number.


Taks
Code:
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="isbn"
            DataSourceID="AccessDataSource1" Width="555px">
            <Columns>
                <asp:BoundField DataField="author" HeaderText="author" SortExpression="author" />
                <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                <asp:BoundField DataField="isbn" HeaderText="isbn" ReadOnly="True" SortExpression="isbn" />
                <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />      
                <asp:TemplateField HeaderText="Qty"><ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Width="25" MaxLength="3"></asp:TextBox>
                   </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            
        </asp:GridView>
 
A control inside the gridview will get a ASP.Net generated name while HTML is being rendered. If you want to access the controls inside you will have to use FindControl method of GridViewRow.


Sunil
 
Sunil

Ok I got it. Works great. Thank you very much,.

JB thanks for pointing me in the right direction.


-dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top