The name 'HttpContext' does not exist in the current context
Asked Answered
J

3

13

I am trying to convert some vb.net to C#, but I keep getting errors. At the moment, I am getting the error in the title.

The problem line is:

string[] strUserInitials = HttpContext.Current.Request.ServerVariables("LOGON_USER").Split(Convert.ToChar("\\"));

Anyone know why this is happening?

I am working on a webservice (asmx file).

I have the following at the top of the code:

using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
Junna answered 4/8, 2011 at 11:4 Comment(3)
As it's a WebService, you could try using OperationContext instead?Spacetime
I'm now getting The name 'OperationContext' does not exist in the current context.Junna
I think it's the [] brackets as @alun posted belowHescock
F
13

You need [] instead of () :

string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split(System.Convert.ToChar(@"\"));
Frasier answered 4/8, 2011 at 11:10 Comment(4)
Now getting The name 'Convert' does not exist in the current contextJunna
Convert is in the system namespace so either change it to System.Convert or add using System;Frasier
@shirowanen: Why do you use Convert? Use '\\'.Hescock
add System.Web.dll reference, check how to do that here, hope helps some one.Trotman
H
21

You have to reference to System.Web and import the namespace System.Web:

using System.Web;

I would not use Convert at all:

string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split('\\'));
Hescock answered 4/8, 2011 at 11:11 Comment(0)
F
13

You need [] instead of () :

string[] strUserInitials = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].Split(System.Convert.ToChar(@"\"));
Frasier answered 4/8, 2011 at 11:10 Comment(4)
Now getting The name 'Convert' does not exist in the current contextJunna
Convert is in the system namespace so either change it to System.Convert or add using System;Frasier
@shirowanen: Why do you use Convert? Use '\\'.Hescock
add System.Web.dll reference, check how to do that here, hope helps some one.Trotman
C
3

put using System.Web; and using System; into the source file...

Carolincarolina answered 4/8, 2011 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.