Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello everyone,
I am using a delgate object in my Web Application, in which I have to show some data in a control placed on the design page. I am not able to access any control of the .aspx page on my code behind as it shows exception that "use new keyword to initialize...." etc. I am not able to understand that why is this happening. Any if there is any solid reason for this to happen, then plz let me know that how could I show this record in a textbox or label. The line which is catching the exception is shown with a "//" below :
 
        private void SSLCertificate()
        {
            var servername = "www.xyz.com";
 
            Response.Write("\n\nFetching SSL cert for {0}\n" + servername);
            TcpClient client = new TcpClient(servername, 443);
            SslStream sslStream = new SslStream(client.GetStream(), false, callback, null);
 
            try
            {
                sslStream.AuthenticateAsClient(servername);
            }
            catch (AuthenticationException ex)
            {
               Response.Write("Exception: {0}"+ ex.Message);
                if (ex.InnerException != null)
                {
                    Response.Write("Inner exception: {0}"+ ex.InnerException.Message);
                }
                Response.Write("Authentication failed - closing the connection.");
            }
            client.Close();
           }
 
        static RemoteCertificateValidationCallback callback = delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslError)
        {
            SSLCert objSSLCert = new SSLCert();
             X509Certificate2 x509  = new X509Certificate2(cert);
          
            string str = x509.Subject;
           
            objSSLCert.txtDisplay.Text = "\n" + x509.Issuer.ToString(); //This line throwing error as null reference exception for textbox. 
        
            if (sslError != SslPolicyErrors.None)
            {
                LogError();
            }
 
            return false;
        };
 
[System.NullReferenceException] = {"Object reference not set to an instance of an object."}, This is the actual exception caught, and "use the "new" keyword to create an object instance.", is showing under the troubleshooting tips in exception box. Which gives an idea like I am trying to use an object without instantiating it, the object which is referred is textbox (I am sure for that). I want to know that how I use any asp control (textbox or label) for showing my record.
 
Thanks and Regards
Anurag
Posted 23-Nov-10 0:57am
Edited 23-Nov-10 17:33pm
v7
Comments
Sunasara Imdadhusen at 23-Nov-10 6:13am
   
What will be the value of x509 before this line objSSLCert.txtDisplay.Text ??
@nuraGGupt@ at 23-Nov-10 6:18am
   
X509 is an object of X509Certificate2 class. I am getting all the value of its properties. The error is for textbox only.
 
For example if you check it for www.yahoo.com ( i made it www.xyz.com in question), you will get X509.Subject as:
CN=www.yahoo.com, O=Yahoo Inc., L=Sunnyvale, S=California, C=US, SERIALNUMBER=2g8aO5wI1bKJ2ZD588UsLvDe3gTbg8DU
 
and X509.Issuer as:
OU=Equifax Secure Certificate Authority, O=Equifax, C=US.
Henry Minute at 23-Nov-10 7:23am
   
It might help to get an answer if you posted the actual error message. '"use new keyword to initialize...." etc' is a little imprecise and could refer to any of several errors.
 
BTW: If you decide to do this, do not add it as a comment, edit your original question to include the information. Clue: use the 'Improve Question' widget.
@nuraGGupt@ at 23-Nov-10 22:51pm
   
See, I am working from last 2 years in .net technologies, which include 7-8 web projects and 3 Desktop Applications including one in WPF. Now, you may understand that I may be having this much concept that if a control is not on a design page, we can not use it on the code behind this way.

Now, the people who are down-voting me here, or providing 5 points to others, I request them, just to run this code in there VS for once. You need to just take a textbox named as txtDisplay at your design page, and call the method "SSLCertificate()" on the pageload. Rest of all will be clear to you that why I am pushing this thing. I dont want to make an issue, but want to know the reason for this, as I have got way more experienced people here. I want to know exactly where I am wrong.

@nuraGGupt@ at 23-Nov-10 22:58pm
   

As of now, I have got an other way round for this task, and thats working fine. But still, i am desperate to know that

exactly where am I wrong ?

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I'm a bit confused what you're trying to achieve here. You have the following line:
SSLCert objSSLCert = new SSLCert();
and then you try to set the value in txtDisplay.Text here:
objSSLCert.txtDisplay.Text = "\n" + x509.Issuer.ToString();
Is this meant to be a form? If so, unless your constructor is initializing txtDisplay, it would look like that's your problem - txtDisplay is null, so you can't set the Text property as there is no instance. Set a breakpoint on this line and inspect objSSLCert to see what is in there.
  Permalink  
v2
Comments
Nishant Sivakumar at 23-Nov-10 7:48am
   
Proposed as answer, voted 5!
@nuraGGupt@ at 23-Nov-10 7:51am
   
this is not a form. I have mentioned it in my question that it is a Web Application. The debugger shows me this for my obj:
base {System.Web.UI.Page} = {WebSecurity.SSLCert} //Here WebSecurity is my application name and SSLCert is the class name on which I am working.
and this is correct. Its the delegate method, which is not letting me use asp controls on my page. And I dont know why is it happening.
@nuraGGupt@ at 23-Nov-10 7:58am
   
Do we need to initialize controls in Web Applications also? Heard this for the first time.
Pete O'Hanlon at 23-Nov-10 8:26am
   
That's a bit rude. I will answer you when I see fit; I do not work to your timescales.
 
It looks to me like your architecture could be wrong. Are you trying to update an already created instance? If so, you shouldn't be creating a new instance.
@nuraGGupt@ at 23-Nov-10 8:31am
   
no, its not like i am updating any instance, its a total new work and new instance.You can copy and run the code whenever you get free. BTW, I am really sorry if I sounded rude, but I am a bit frustrated coz of this problem, and that's why I changed my question title also, as now it doesn't seems a silly problem to me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is difficult to understand your problem completely from your post. I am assuming that SSLCert in your code is the type of your UserControl. You can't create the instance of a usercontrol and access the child controls. You have to use either LoadControl method or use the instance of the usercontrol in the parent page.
 
Please post more details about your code for getting help from CP.
  Permalink  
Comments
@nuraGGupt@ at 23-Nov-10 7:42am
   
Oh, I am sorry, i didn't cleared it, SSLCert is my class name on which I am working, and since I am working in a delegate handler, I needed to make an object of the same class in which I am working, to use its controls. Hope now you got an idea of what I am doing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Rin it under the debugger to find out which of the objects is null. It's really not that hard to figure this stuff out on your own if you would just use the tools you already have on hand.
  Permalink  
Comments
@nuraGGupt@ at 23-Nov-10 7:37am
   
I have run this under debugger, and its showing the exception on the textbox, which is not understandable to me. You can also try and run the code. It may help you understand the problem better.

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

  Print Answers RSS
0 OriginalGriff 275
1 DamithSL 265
2 CPallini 235
3 Maciej Los 185
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 23 Nov 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100