Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
Could you please correct the below code

I have to take value from database and assign it to Detailview item template label

 <fields>                                                                    <asp:TemplateField HeaderText="Company Name">
                                                                        <itemtemplate>
                                                                            <asp:Label ID="complbl" Text = "Hi" runat = "server">
                                                                        </itemtemplate>
                                                                      
                                                                    <asp:TemplateField HeaderText="Company Address">
                                                                        <itemtemplate>
                                                                            <asp:Label ID="addlbl" runat="server" Visible="true">
                                                                        </itemtemplate>
                                                                                                                                        
</fields>                                                                

while (reader.Read())
        {
            doDate = reader["DoDate"].ToString();
            doNumber = reader["DoNumber"].ToString();
            customerId = reader["CusId"].ToString();
            comp = reader["CompName"].ToString();

            

            DoNotb.Text = doNumber;
            doDatetb.Text = doDate;
            cidtb.Text = customerId;
            Label compname = DetailsView1.Rows[1].Cells[0].FindControl("complbl");
            comp = compname.Text;
            compname.Text = comp;
 
        }


I am getting error as "
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Thanks
Posted
Updated 27-Nov-11 20:01pm
v2

Corrected code as
XML
Collapse | Copy Code
 <fields>                                                                    <asp:TemplateField HeaderText="Company Name">
                                                                        <itemtemplate>
                                                                            <asp:Label ID="complbl" Text = "Hi" runat = "server" text='<%# Eval("complbl")%>' >
                                                                        </itemtemplate>

                                                                    <asp:TemplateField HeaderText="Company Address">
                                                                        <itemtemplate>
                                                                            <asp:Label ID="addlbl" runat="server" Visible="true"  text='<%# Eval("complbl")%>' >

                                                                        </itemtemplate>

</fields>
 
Share this answer
 
Comments
Lancy.net 28-Nov-11 4:38am    
Thanks for your reply .. same error continuing
ASP.NET
 <fields>                                                                    
   <asp:templatefield headertext="Company Name" xmlns:asp="#unknown">
        <itemtemplate>
            <asp:label id="complbl" Text='<%# Eval("complbl")%>' runat="server">
        </itemtemplate>
                                                                      
        <asp:templatefield headertext="Company Address">
        <itemtemplate>
        <asp:label id="addlbl" runat="server" Text='<%# Eval("compAddress")%>' visible="true">
        </itemtemplate>
</fields>


At Server side you just need to assign datatable to DataSource of gridview.
There should be column complbl,compAddress in DataTable

let me know if any queries
Mark as solution if its solved your problem
 
Share this answer
 
v2
Comments
Lancy.net 28-Nov-11 4:45am    
Thanks sagar...again error is "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900