Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

while I am trying to get all ids of my anchor tags I got an error "Object Reference is not set as Instance of an Object"
What i actually trying to do is:
In my aspx page i wrote code as,
<form id="form1" runat="server">
     <asp:Label ID="lblQues" runat="server" /><br />
    <asp:Button ID="btnAns" runat="server" Text="Get Anchor Ids" OnClick="btnAns_Click" /><br />
    <asp:Label ID="lblAns" runat="server" />  
    </form>

In my aspx.cs page i write code as,
 protected void Page_Load(object sender, EventArgs e)
        {           
            HtmlAnchor htmlAnchor = new HtmlAnchor();
            htmlAnchor.ID = "anchor";
            htmlAnchor.HRef = "#";
            htmlAnchor.InnerText = "anchor";
            lblQues.Controls.Add(htmlAnchor);
         }
 private string ids="";
 protected void btnAns_Click(object o, EventArgs a)
         {
             foreach (Control c in lblQues.Controls)
                 if (c is HtmlAnchor)
                 {
                    ids = ids+ " " + c.ID;
                     Type type = c.GetType();
                     string id = type.Name;
                  }
             lblAns.Text = ids;
         }


on button click i got an error as "Object Reference is not set as Instance of an Object" at c.ID.
Can you suggest how to do this.

Thanks,
Sindhu.
Posted
Updated 13-Feb-14 7:04am
v4
Comments
Richard MacCutchan 13-Feb-14 9:14am    
ids = ids.ToString()
What does ids refer to at this point?
Naga Sindhura 13-Feb-14 12:45pm    
here ids reers to htmlAnchor.ID = "anchor"; that means all htmlAnchor id's
Richard MacCutchan 13-Feb-14 12:47pm    
How do you figure that, since it is a local string variable that has not been initialised to anything?
Naga Sindhura 13-Feb-14 13:02pm    
Hi Richard, I initialized a vale for string ids,still i am getting an error.
Richard MacCutchan 13-Feb-14 13:13pm    
Please don't just tell us that you are getting "an error"; give us the full details, line of code, error type, variables involved, etc.

Remove the .Tostring() from ids.
Please see the below code

C#
protected void btnAns_Click(object o, EventArgs a)
{
    foreach (Control c in lblQues.Controls)
         if (c is HtmlAnchor)
         {
            ids = ids + " " + c.ID;
             Type type = c.GetType();
             string id = type.Name;
          }
    lblAns.Text = ids;
}
 
Share this answer
 
v3
Comments
Naga Sindhura 13-Feb-14 12:47pm    
hi, i am getting an error (ids = ids + " " + c.ID;) because o c.ID) could you help me for this
Richard MacCutchan 13-Feb-14 12:49pm    
Please use <pre> tags around your code snippets, as I have done on your behalf.
Richard MacCutchan 13-Feb-14 12:56pm    
This won't work either. You cannot use ids in an expression before it has been initialised to some value.
Quote:
ids = ids.ToString() + " " + c.ID;
The Exception is due to ids, which is not defined anywhere.

Why are you using that? Correct your logic.
 
Share this answer
 
Comments
Naga Sindhura 13-Feb-14 12:55pm    
hi, globally i created variable for ids as string. i am getting an error because of c.ID.
That may be globally declared, but does it contain any value? Check by putting a debugger in this event only.
Also check if C.ID contains any data or not.
Change your code to:
C#
private string ids;
protected void btnAns_Click(object o, EventArgs a)
{
    ids = "";
    foreach (Control c in lblQues.Controls)
        if (c is HtmlAnchor)
        {
            ids += " " + c.ID;

            // what are these two lines supposed to do?
            Type type = c.GetType();
            string id = type.Name;
        }
    lblAns.Text = ids;
}
 
Share this answer
 
Comments
Naga Sindhura 13-Feb-14 13:08pm    
Hi Richard MacCutchan, i tried to get id name of all htmlAnchor using Type. Actually that is not required.
Thanks to all I got the Answer for my question
foreach (Control c in lblQues.Controls)
                 if (c is HtmlAnchor)
                 {       
                     string pp = c.ClientID;
                     ids = ids + " " + pp;
                 }
 
Share this answer
 

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