equivalent C# for VB.NET "Session"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi groups,

Can anyone give me the equivalent C# sharp code for this VB.ET code,

:: VB.NET ::

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles MyBase.Load
'This event routine in the loginCheck user control fires when the
protected
'page loads. If the user hasn't logged in, loginCheck redirects the
user to
'the page specified by LoginPageURL for validation. If the user has
logged in,
'he or she gets access to the protected page.

If Not Enabled Then Exit Sub
If IsNothing(Session("LoginType")) Then Exit Sub
If CType(Session("LoginType"), _
String).Length > 0 Then
If IsNothing(Session(LoginPageURL & _
"_Valid")) Then
Session("LoginTarget") = _
Request.ServerVariables _
("Script_Name")
Response.Redirect(LoginPageURL)
Else
If CType(Session(LoginPageURL & _
"_Valid"), String).Length = 0 Then
Session("LoginTarget") = _
Request.ServerVariables _
("Script_Name")
Response.Redirect(LoginPageURL)
End If
End If
End If
End Sub

C#?

I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.asax.cs(42):
'System.Web.HttpApplication.Session' denotes a 'property' where a 'method'
was expected

Anyone has any suggestions?

Thanks. -Dale
 
<"=?Utf-8?B?ZGFsZSB6aGFuZw==?=" <dale
(e-mail address removed)>> wrote:

I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.asax.cs(42):
'System.Web.HttpApplication.Session' denotes a 'property' where a 'method'
was expected

Anyone has any suggestions?

That suggests that you're using it as:

Session("LoginType") when you should be using Session["LoginType"] -
i.e. you should be using the indexer of the Session property.
 
Thanks, Jon. That problem was sloved.

But a few new ones:
1. VB:: Response.Redirect _
(Session("LoginTarget"))

C#::?
Response.Redirect(Session["LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): The best
overloaded method match for 'System.Web.HttpResponse.Redirect(string)' has
some invalid arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): Argument
'1': cannot convert from 'object' to 'string'

2.VB:: Dim sPage As String = Session("LoginTarget")
C#:: string sPage = Session["LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(108): Cannot
implicitly convert type 'object' to 'string'

3.VB::
Private Function CheckPass(ByVal User As String, ByVal Password As
String) As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
private bool CheckPass(string User, string Password)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string sConn = Connect();
string sSQL;
CheckPass = false; ???
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(131): Method
'passwordProtectCSharp.loginControl.CheckPass(string, string)' referenced
without parentheses

4. VB:: Date.Now.ToString
c#??

5. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString.Length)
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(272):
'System.DateTime.ToString(string)' denotes a 'method' which is not valid in
the given context

Many thanks. -Dale
Jon Skeet said:
<"=?Utf-8?B?ZGFsZSB6aGFuZw==?=" <dale
(e-mail address removed)>> wrote:

I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.asax.cs(42):
'System.Web.HttpApplication.Session' denotes a 'property' where a 'method'
was expected

Anyone has any suggestions?

That suggests that you're using it as:

Session("LoginType") when you should be using Session["LoginType"] -
i.e. you should be using the indexer of the Session property.
 
dale zhang said:
Thanks, Jon. That problem was sloved.

But a few new ones:
1. VB:: Response.Redirect _
(Session("LoginTarget"))

C#::?
Response.Redirect(Session["LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): The best
overloaded method match for 'System.Web.HttpResponse.Redirect(string)' has
some invalid arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): Argument
'1': cannot convert from 'object' to 'string'

You'll need to cast to string:

Response.Redirect ((string) Session["LoginTarget"]);

The fact that you didn't in VB suggests that you had Option Strict
turned off - a bad idea!
2.VB:: Dim sPage As String = Session("LoginTarget")
C#:: string sPage = Session["LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(108): Cannot
implicitly convert type 'object' to 'string'

Same again.
3.VB::
Private Function CheckPass(ByVal User As String, ByVal Password As
String) As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
private bool CheckPass(string User, string Password)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string sConn = Connect();
string sSQL;
CheckPass = false; ???
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(131): Method
'passwordProtectCSharp.loginControl.CheckPass(string, string)' referenced
without parentheses

Not sure on that - what is "CheckPass = false" meant to do, exactly? Is
there a variable called CheckPass somewhere? Don't forget that C# is
case-sensitive, whereas VB.NET isn't.
4. VB:: Date.Now.ToString
c#??
DateTime.Now.ToString()

5. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString.Length)
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(272):
'System.DateTime.ToString(string)' denotes a 'method' which is not valid in
the given context

Use ToString().Length rather than ToString.Length. VB.NET doesn't make
you use brackets when there aren't any parameters, unfortunately -
making method calls look like properties :(
 
Jon,

Your answers are very helpful. We are close to success. A few more stand as
below:
1: VB::
Private Function Connect() As String
Dim sConnect As String
'this value could go directly in the Global.asax.vb declarations
Select Case CType(Application("DBType"), String).ToLower
Case "sqlserver"
sConnect =
ConfigurationSettings.AppSettings.Get("SQLConnection")
'Return "Provider=SQLOLEDB.1;Integrated
Security=SSPI;Persist Security Info=False;Initial
Catalog=PasswordProtect;Data Source=(local)"
Case "access"
sConnect =
ConfigurationSettings.AppSettings.Get("AccessConnection")
'Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
End Select
Return sConnect
End Function

C#::
private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
}
return sConnect;
}
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(229): Use of
unassigned local variable 'sConnect'

It in fact complains about switch input. I mean it can not switch and assign
value. Can not switch happened later as well.

2. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString().Length)

c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(264): Cannot
implicitly convert type 'int' to 'bool'

3. VB:: sArray = WebPath.Split("/")
C#:: sArray = WebPath.Split("/");
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): The best
overloaded method match for 'string.Split(params char[])' has some invalid
arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): Argument
'1': cannot convert from 'string' to 'char[]'

Thanks. -Dale

Jon Skeet said:
dale zhang said:
Thanks, Jon. That problem was sloved.

But a few new ones:
1. VB:: Response.Redirect _
(Session("LoginTarget"))

C#::?
Response.Redirect(Session["LoginTarget"]);
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): The best
overloaded method match for 'System.Web.HttpResponse.Redirect(string)' has
some invalid arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(84): Argument
'1': cannot convert from 'object' to 'string'

You'll need to cast to string:

Response.Redirect ((string) Session["LoginTarget"]);

The fact that you didn't in VB suggests that you had Option Strict
turned off - a bad idea!
2.VB:: Dim sPage As String = Session("LoginTarget")
C#:: string sPage = Session["LoginTarget"];
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(108): Cannot
implicitly convert type 'object' to 'string'

Same again.
3.VB::
Private Function CheckPass(ByVal User As String, ByVal Password As
String) As Boolean
Dim dbConn As OleDbConnection
Dim dbCmd As OleDbCommand
Dim dbDR As OleDbDataReader
Dim sConn As String = Connect()
Dim sSQL As String
CheckPass = False
C#::
private bool CheckPass(string User, string Password)
{
OleDbConnection dbConn;
OleDbCommand dbCmd;
OleDbDataReader dbDR;
string sConn = Connect();
string sSQL;
CheckPass = false; ???
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(131): Method
'passwordProtectCSharp.loginControl.CheckPass(string, string)' referenced
without parentheses

Not sure on that - what is "CheckPass = false" meant to do, exactly? Is
there a variable called CheckPass somewhere? Don't forget that C# is
case-sensitive, whereas VB.NET isn't.
4. VB:: Date.Now.ToString
c#??
DateTime.Now.ToString()

5. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString.Length)
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(272):
'System.DateTime.ToString(string)' denotes a 'method' which is not valid in
the given context

Use ToString().Length rather than ToString.Length. VB.NET doesn't make
you use brackets when there aren't any parameters, unfortunately -
making method calls look like properties :(
 
dale zhang said:
C#::
private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
}
return sConnect;
}
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(229): Use of
unassigned local variable 'sConnect'

It in fact complains about switch input. I mean it can not switch and assign
value. Can not switch happened later as well.

The problem is that if the DBType is neither "access" nor "sqlserver",
what are you expecting it to return?

Either set a "default" case, or assign a value to sConnect to start
with.
2. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString().Length)

c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(264): Cannot
implicitly convert type 'int' to 'bool'

Change it to:

if (datData.ToString().Length > 0)
3. VB:: sArray = WebPath.Split("/")
C#:: sArray = WebPath.Split("/");
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): The best
overloaded method match for 'string.Split(params char[])' has some invalid
arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): Argument
'1': cannot convert from 'string' to 'char[]'

Change it to WebPath.Split('/')
 
I think you ar right: the DBType is neither "access" nor "sqlserver". I could
not understand how this happens.

it is defined in Global.asax.vb as
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'Choices: "SQLServer","Access"
Application("DBType") = "Access"
End Sub

C#::
void Application_BeginRequest(object sender, EventArgs e)
{
// Choices: "SQLServer","Access"
Application["DBType"] = "Access";
}

I am using it in loginControl.ascx.cs as
private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":

How come the global variable can not be passed in?

Thank you. I hope this is the last one. -Dale

Jon Skeet said:
dale zhang said:
C#::
private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
}
return sConnect;
}
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(229): Use of
unassigned local variable 'sConnect'

It in fact complains about switch input. I mean it can not switch and assign
value. Can not switch happened later as well.

The problem is that if the DBType is neither "access" nor "sqlserver",
what are you expecting it to return?

Either set a "default" case, or assign a value to sConnect to start
with.
2. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString().Length)

c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(264): Cannot
implicitly convert type 'int' to 'bool'

Change it to:

if (datData.ToString().Length > 0)
3. VB:: sArray = WebPath.Split("/")
C#:: sArray = WebPath.Split("/");
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): The best
overloaded method match for 'string.Split(params char[])' has some invalid
arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): Argument
'1': cannot convert from 'string' to 'char[]'

Change it to WebPath.Split('/')
 
seems my post is not on. here is the 2nd try.

you are right: Application("DBType") did not pass. it is defined in
global.asax.vb as below:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'Choices: "SQLServer","Access"
Application("DBType") = "Access"
End Sub
C#::
void Application_BeginRequest(object sender, EventArgs e)
{
// Choices: "SQLServer","Access"
Application["DBType"] = "Access";
}
I am using it in loginControl.ascx.cs as previous post. why not pass?

hope this is last one. thanks a lot. -dale

Jon Skeet said:
dale zhang said:
C#::
private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
}
return sConnect;
}
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(229): Use of
unassigned local variable 'sConnect'

It in fact complains about switch input. I mean it can not switch and assign
value. Can not switch happened later as well.

The problem is that if the DBType is neither "access" nor "sqlserver",
what are you expecting it to return?

Either set a "default" case, or assign a value to sConnect to start
with.
2. VB:: If (datData.ToString).Length Then
C#:: if (datData.ToString().Length)

c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(264): Cannot
implicitly convert type 'int' to 'bool'

Change it to:

if (datData.ToString().Length > 0)
3. VB:: sArray = WebPath.Split("/")
C#:: sArray = WebPath.Split("/");
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): The best
overloaded method match for 'string.Split(params char[])' has some invalid
arguments
c:\inetpub\wwwroot\passwordProtectCSharp\loginControl.ascx.cs(294): Argument
'1': cannot convert from 'string' to 'char[]'

Change it to WebPath.Split('/')
 
dale zhang said:
seems my post is not on. here is the 2nd try.

you are right: Application("DBType") did not pass. it is defined in
global.asax.vb as below:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'Choices: "SQLServer","Access"
Application("DBType") = "Access"
End Sub
C#::
void Application_BeginRequest(object sender, EventArgs e)
{
// Choices: "SQLServer","Access"
Application["DBType"] = "Access";
}
I am using it in loginControl.ascx.cs as previous post. why not pass?

hope this is last one. thanks a lot. -dale

Sorry, I don't understand your question. What do you mean by "why not
pass?"?
 
I am wondering why global application state variable Application("DBType")
can not be accessed by all files. I think I can figure this out later. I hard
code the option for now.

The C# code can run. But I have a default page which has a hyperlink to a
private page. The private page has login check. Somehow, the Page_Init and
Page_Load in the private page do not get executed (do not break). Hence I can
not do login check. Is this caused by CodeBehind (=privatePage)?

Thanks. -dale
Jon Skeet said:
dale zhang said:
seems my post is not on. here is the 2nd try.

you are right: Application("DBType") did not pass. it is defined in
global.asax.vb as below:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
'Choices: "SQLServer","Access"
Application("DBType") = "Access"
End Sub
C#::
void Application_BeginRequest(object sender, EventArgs e)
{
// Choices: "SQLServer","Access"
Application["DBType"] = "Access";
}
I am using it in loginControl.ascx.cs as previous post. why not pass?

hope this is last one. thanks a lot. -dale

Sorry, I don't understand your question. What do you mean by "why not
pass?"?
 
dale zhang said:
I am wondering why global application state variable Application("DBType")
can not be accessed by all files. I think I can figure this out later. I hard
code the option for now.

It's not a variable - it's a property of the Page instance, so unless
you have a reference to the page, you won't be able to get at it.
The C# code can run. But I have a default page which has a hyperlink to a
private page. The private page has login check. Somehow, the Page_Init and
Page_Load in the private page do not get executed (do not break). Hence I can
not do login check. Is this caused by CodeBehind (=privatePage)?

Wouldn't like to say, I'm afraid - I suggest you ask in the ASP.NET
newsgroup, with a complete example.
 
I looked at the ASP.NET newsgroup here, I could not find a way to attach
files to the message board. Any idea? Thanks. -Dale
 
what is the reason for a private page?

if the page shows in a browser, it would have fired four page events - init,
pre-render, unload (scratching my head for the other one). The debugger
breaking into the page has nothing to do with the page event firing.

one thing you can do is to break before you call the * private page. Then
you can step into the call to see what is going.

unless you have some funky logic or design requirement, you should implement
the login page as an ordinary aspx page. I doubt that is the reason for your
problem though, but i thought i would mention it.


--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
 
dale zhang said:
I looked at the ASP.NET newsgroup here, I could not find a way to attach
files to the message board. Any idea? Thanks. -Dale

Yes - use a "proper" newsreader and connect to msnews.microsoft.com and
post a message that way, rather than using the web interface. I'm sure
there's a way of posting via the web interface, but I don't use it
myself, far preferring a tool designed specifically for newsgroups. You
can use Outlook Express if you want, although there are many other
newsreaders available too.
 
Thank you all for your helps. Especially from Jon.

My C# one is fully functional. All wired things have been fixed. The main
reasons of the problem are:

1. auto converter is not right all the time.
2. the VB project was from VS.NET 1.0. Too old. we need to update those
related like Page_Init() in VS 2003.

-Dale

Jon Skeet said:
<"=?Utf-8?B?ZGFsZSB6aGFuZw==?=" <dale
(e-mail address removed)>> wrote:

I used an auto tool to do this and got:

The MS VS 2003 always complains "Session" as below:

c:\inetpub\wwwroot\passwordProtectCSharp\Global.asax.cs(42):
'System.Web.HttpApplication.Session' denotes a 'property' where a 'method'
was expected

Anyone has any suggestions?

That suggests that you're using it as:

Session("LoginType") when you should be using Session["LoginType"] -
i.e. you should be using the indexer of the Session property.
 
Back
Top