Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

Am doing a VB code conversion into C# and facing some below listed errors

Below is my VB CODE

VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
       Page.Response.Expires = -2400

       If Page.Session("auth_EmployeeID") <> Convert.ToInt32(Page.User.Identity.Name) Then Call Reset_Session_Data(Page)

       If _restrictto_groupid <> 0 Then
           If InStr(Session("user_groups"), "~" & _restrictto_groupid & "~") = 0 Then
               'USER DOES NOT HAVE RIGHTS FOR THIS PAGE
               Server.Transfer("/access_denied.aspx")
           End If
       End If

       Dim filename As String
       Dim fileandquery As String

       fileandquery = Page.Request.Path & "?index=5" 'System.IO.Path.GetFileName(Request.Path) & "?index=5"
       Dim query_pos As Integer
       query_pos = InStr(fileandquery, "?")
       If query_pos <> 0 Then
           filename = Left(fileandquery, query_pos - 1)
       Else
           filename = fileandquery
       End If

       If _parent_Nav_Area <> "" Then filename = _parent_Nav_Area

       'Page.Trace.Write("navfix",filename)

       Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
       Dim command As New SqlCommand("sproc_CP_Navigation_PathGet", conn)
       command.CommandType = CommandType.StoredProcedure

       Dim param As SqlParameter = command.Parameters.Add("@filename", SqlDbType.VarChar, 100)
       param.Value = filename

       conn.Open()
       Dim pathReader As SqlDataReader = command.ExecuteReader()
       nav_path.Text = ""

       Dim access_granted As Boolean = False

       If pathReader.HasRows Then
           Do While pathReader.Read()
               If pathReader.Item("depth") <> 1 Then
                   nav_path.Text &= " :: "
               Else
                   nav_path.Text &= "> "
               End If

               access_granted = False
               If pathReader.Item("security_group_required") Is DBNull.Value Then
                   access_granted = True
               Else
                   access_granted = Check_GroupMembership(pathReader.Item("security_group_required"))
               End If

               If Not (pathReader.Item("link") Is DBNull.Value) And access_granted Then
                   If InStr(pathReader.Item("link"), filename) = 0 Or _parent_Nav_Area <> "" Then
                       nav_path.Text &= "<a href=""" & Page.Request.ApplicationPath & "flashnav/" & pathReader.Item("link") & """>" & pathReader.Item("label") & "</a>"
                   Else
                       nav_path.Text &= pathReader.Item("label")
                   End If
               Else
                   nav_path.Text &= pathReader.Item("label")
               End If
           Loop
       End If

       pathReader.Close()
       conn.Close()
       pathReader = Nothing
       command = Nothing
       conn = Nothing
   End Sub



Here is my C# CODE

C#
protected void Page_Load(object sender, EventArgs e)
   {
       Page.Response.Expires = -2400;

       if (Page.Session["auth_EmployeeID"] != (Page.User.Identity.Name).ToString())
       {
           Reset_Session_Data(Page);
       }

       if (_restrictto_groupid != 0)
       {
           if (String.InStr(Session["user_groups"], "~" + _restrictto_groupid + "~") == 0)
           {
               //USER DOES NOT HAVE RIGHTS FOR THIS PAGE
               Server.Transfer("access_denied.aspx");
           }
       }

       string filename = null;
       string fileandquery = null;

       fileandquery = Page.Request.Path + "?index=5";
       //System.IO.Path.GetFileName(Request.Path) & "?index=5"
       int query_pos = 0;
       query_pos = String.InStr(fileandquery, "?");
       if (query_pos != 0)
       {
           filename = String.Left(fileandquery, query_pos - 1);
       }
       else
       {
           filename = fileandquery;
       }

       if (!string.IsNullOrEmpty(_parent_Nav_Area))
           filename = _parent_Nav_Area;

       //Page.Trace.Write("navfix",filename)

       SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
       SqlCommand command = new SqlCommand("sproc_CP_Navigation_PathGet", conn);
       command.CommandType = CommandType.StoredProcedure;

       SqlParameter param = command.Parameters.Add("@filename", SqlDbType.VarChar, 100);
       param.Value = filename;

       conn.Open();
       SqlDataReader pathReader = command.ExecuteReader();
       nav_path.Text = "";

       bool access_granted = false;

       if (pathReader.HasRows)
       {
           while (pathReader.Read())
           {
               if (Convert.ToInt32(pathReader["depth"]) != 1)
               {
                   nav_path.Text += " :: ";
               }
               else
               {
                   nav_path.Text += "> ";
               }

               access_granted = false;
               if (object.ReferenceEquals(pathReader["security_group_required"], DBNull.Value))
               {
                   access_granted = true;
               }
               else
               {
                   access_granted = Check_GroupMembership(pathReader["security_group_required"]);
               }

               if ((!object.ReferenceEquals(pathReader["link"], DBNull.Value)) & access_granted)
               {
                   if (String.InStr(pathReader["link"], filename) == 0 | !string.IsNullOrEmpty(_parent_Nav_Area))
                   {
                       nav_path.Text += "<a href="\""" page.request.applicationpath="">" + pathReader["label"] + "</a>";
                   }
                   else
                   {
                       nav_path.Text += pathReader["label"];
                   }
               }
               else
               {
                   nav_path.Text += pathReader["label"];
               }
           }
       }
       pathReader.Close();
       conn.Close();
       pathReader = null;
       command = null;
       conn = null;
   }

after conversion VB to C# of the code below is the error:
C#
if (String.InStr(pathReader["link"], filename) == 0 | string.IsNullOrEmpty(_parent_Nav_Area))

The name ' Strings' does not exists in current context.

If I replace this Strings keyword to String then the InStr which is its associated fuctions gets error: String does not contain a definition of 'InStr', I known to this fact that 'InStr' is not accessible in C# can anyone please suggest an alternative for the same..
Posted
Updated 25-May-15 20:59pm
v2

You can use IndexOf[^] method of string.
But you can use also visualbasic original routines adding a reference to Microsoft.VisualBasic and taking care of indexes that are not zero based. You may want read this[^].
 
Share this answer
 
Comments
Maciej Los 26-May-15 3:17am    
A 5!
Last few days are full of questions about conversion errors...
Frankie-C 26-May-15 5:06am    
Thanks
Yes really a lot... :)
I already answered this question here: conversion of vb.net to c#[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-May-15 3:15am    
5ed. This is incredible: a re-post after re-post, and after all our warning.
Such things should entail the abuse reports on this member's account. Or even report at the abuse forum.
—SA
Maciej Los 26-May-15 3:18am    
Yeah...
Thank you, Sergey.
You can always translate code (not "convert"!) automatically. Please see my past answer:
Code Interpretation, C# to VB.NET[^].

Most reliable and quality method is using open-source ILSpy.

You got a very good advice by Maciej Los in his recent answer: stop doing it! Do follow this advice.
Also, stop re-posting the same question; this is considered as abuse.

—SA
 
Share this answer
 
Comments
Maciej Los 26-May-15 3:15am    
I was bit more lazy and added only reference (link) to my past answer - see solution 2 ;)
[EDIT] I forgot to mention that your answer has been upvoted by me ;)
Sergey Alexandrovich Kryukov 26-May-15 3:31am    
Thank you...
Same thing...
—SA
you can use "contains"
e.g.
C#
pathReader["link"].contains(filename)

or
C#
Convert.ToString(pathReader["link"]).contains(filename)


It returns boolean true if filename string exist in pathreader(link) & false otherwise
 
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