[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

XML Problem while using C#

I have written the below code and I am trying to have the information collected from the XML document put into a label so I can reference this is my ASP.Net code. What am I doing wrong? Sorry for the n00b question, I am still learning the language.



<script language=c# runat=server>

protected XmlDocument oXmlDoc;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new XmlDocument();
 try
 {
  oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 XmlNode oNode = oXmlDoc.DocumentElement;
 XmlNodeList StockQuote = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Trade");
 XmlNodeList LastTrade = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Date");
 
Label lbltrade, lblquote;
 
lbltrade.Text = LastTrade.Item(0).InnerText;
lblquote.Text = StockQuote.Item(0).InnerText;
 
 
}
 
</script>
0
Chris_Lombardi
Asked:
Chris_Lombardi
  • 10
  • 9
1 Solution
 
Chris_LombardiAuthor Commented:
Compiler Error Message: CS0165: Use of unassigned local variable 'lbltrade'

That is the error I am getting
0
 
joesthebighmoeCommented:
That means an object is being used before it has been instantiated.
I don't use the built-in form object's provided by microsoft, but it would appear you need to do this...
Label lbltrade = new Label();
Label lblquote = new Label();
0
 
Chris_LombardiAuthor Commented:
odd
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Chris_LombardiAuthor Commented:
for some reason i cannot post my code anymore. weird. In any case, That worked, but when trying to display the data in the label, nothing shows. Even when I replaced the data i retrieved from the XML document with plain text.. Any ideas?

0
 
joesthebighmoeCommented:
My assumption is that you need to give the label more information. Like where it lives on the form. Are there more properties to the label, like XY coordinates, Location, etc? Are there multiple constructors on the label that could point you in the right direction?
When you do this kind of ASP.Net development, don't you usually drag and drop labels onto a UI, and then use them?
0
 
Chris_LombardiAuthor Commented:
Maybe I am going about this the wrong way. That script I wrote lives at the top of my ASP.NET page and I just want those 2 labels to show up in a certain location when the web site is loaded. There is no form input.
0
 
joesthebighmoeCommented:
I understand there is no form input. But when you design a web page using ASP.Net in the drag-and-drop sort of way, do you not drag and drop labels onto a form?
In the past (XML aside) how would you make the webpage say "Hello World?"

0
 
joesthebighmoeCommented:
Then again, maybe you are going about this the wrong way. Can you tell me more about your environment and the website, maybe I could help you get something done quickly.
Is this Visual Studio 2005 and ASP.Net?
0
 
Chris_LombardiAuthor Commented:
Static Web Site with an .aspx extension. There is not much to it, I converted out of ColdFusion ASAP so we do not have much functionality in place. All I need to do is grab 2 elements from an XML file which my script does successfully and have them written to the homepage. The script trigges when the page is loaded, I just don't know how to make these a variable I can access later down the page.

I code everything in notepad still for the most part and if I were to write Hello World I would use teh Response.Write(); method.
0
 
joesthebighmoeCommented:
So then this would work eh?
Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText);
0
 
Chris_LombardiAuthor Commented:
Negative. The error I receive is  Compiler Error Message: CS0103: The name 'LastTrade' does not exist in the current context


That script lives at the top of the page and then there is some HTML and then I have the below code wrapped in an HTML table.

<% Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText); %>

0
 
joesthebighmoeCommented:
I see,  the code you posted showed it all together.
I am trying to wrap my head around doing this as a static page in notepad. I'll get back to you in a second.
0
 
joesthebighmoeCommented:
Mr Lombardi,
Do this....
Add the variablea at the top of your page just above where you declared your xml doc
private string LastTrade;
private string StockQuote;
Then set them in your routine.
After your HTML add this
<% Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText); %>
%>
0
 
Chris_LombardiAuthor Commented:
It works great when it is all together as seen below, but the problem is wherever I put the script, it always show at the top of the page which is why I am trying to make variables that i can use elsewhere on the page.


protected XmlDocument oXmlDoc;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new XmlDocument();
 try
 {
  oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 XmlNode oNode = oXmlDoc.DocumentElement;
 XmlNodeList StockQuote = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Trade");
 XmlNodeList LastTrade = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Date");
 
Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText);
 
 
}
 
0
 
Chris_LombardiAuthor Commented:
trying now
0
 
joesthebighmoeCommented:
Oops, your code should read
Response.Write(LastTrade)
LastTrade should be set to the node's  inner text in the routine.
Yikes, hope I did not confuse you there.
0
 
Chris_LombardiAuthor Commented:
SUCCESS!!!!!!!!!!!!!!!!!!!!! http://www.drs.com/index2.aspx The stock price and date are showing up perfectly! It needs to be formatted but it is showing and that is a huge win! Thank you very much.

So the logic is I should have declared the variable outside the Page_Load() method otherwise the variable would be locked in that method and not accessible via variables further down the page?
0
 
joesthebighmoeCommented:
This worked for me:
Not pretty, but it works.
<HTML>
<BODY>
This is my test page<BR>
<script language=c# runat=server>


protected System.Xml.XmlDocument oXmlDoc;
private string m_ValueIWant;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new System.Xml.XmlDocument();
 try
 {
  //oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
    oXmlDoc.LoadXml(@"<test><DocElem>50</DocElem></test>");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 System.Xml.XmlElement oElement = oXmlDoc.DocumentElement;

m_ValueIWant= oElement.InnerText;

}
 
</script>
<BR>This is the Value I want: <%=m_ValueIWant%>!
</BODY>
</HTML>
0
 
joesthebighmoeCommented:
Yes, all variables have scope. When you declare them outside the load routine then the scope is the entire page of code. This is often called module scope. Coders often detone that with an "m".

private string m_StockQuote;

Good Luck,
Joe
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now