Solved

Asp .net CType(e.Item.Cells(2).Controls(0), TextBox) ----> Error: Out of bound

Posted on 2003-11-28
4
2,231 Views
Last Modified: 2009-01-08
I am trying to put a edit button in the DataGrid, so when clicked would display a textbox to edit the record.

Here is the code:
------------------------------------------------------------------------------------------
   Private Sub MyDG_EditCommand(ByVal source As Object, ByVal e As  System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDG.EditCommand

        MyDG.EditItemIndex = e.Item.ItemIndex()
        MyDG.DataBind()

        Dim key As String = MyDG.DataKeys(e.Item.ItemIndex).ToString

        Dim CatName, CatImage, CatThumb, CatDesc As String
        Dim tb As TextBox


        tb = CType(e.Item.Cells(2).Controls(0), TextBox) 'Here is where I am getting ERROR
        CatName = tb.Text

-------------------------------------------------------------------------------------------------------

It gives me following ERROR:
-------------------------------------------------------------------------------------------------------
Specified argument was out of the range of valid values. Parameter name: index

Line 149:        tb = CType(e.Item.Cells(2).Controls(0), TextBox)
-------------------------------------------------------------------------------------------------------

I Debug the program and try to get the values and it gave me following result:

?e.item.cells.count  ----------------------> 6
?e.item.cells.controls.count -------------> 0      'I think here is the problem because it is zero

But I don't know what does it mean and how would I solve it.

Please Help !!!

0
Comment
Question by:cajunworks
4 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 9837847
How do you write you EditTemplate in your HTML ? Can you  post the code ?
The problem is realy in e.item.cells.controls.count because there are no controls int the current cell

B..G
0
 
LVL 12

Expert Comment

by:roverm
ID: 9838034
Try something like this:

tb = CType(e.Item.Cells(2).Cells(0).Controls(0), TextBox)

So access cell 0 inside cell 2.
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 250 total points
ID: 9841077
cajunworks,
The easier way to solve the problems is use FindControl function.
eg:
tb =CType(e.Item.FindControl("TextBoxNameInEdit"), TextBox)

'You just need to put the control name when you defined in EditItemTemplate
eg:
       <EditItemTemplate>
         <asp:TextBox ID="TextBoxNameInEdit" Columns="4" Runat="server" />
       </EditItemTemplate>

Regards
x_com
0
 
LVL 2

Expert Comment

by:NaliniP
ID: 9841499
what X_COM said is correct.. you can use the <EDITITEM TEMPLATE> to put a textbox while editing..its great

Nalini
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

17 Experts available now in Live!

Get 1:1 Help Now