Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys...i m back again....i still have some issues regarding code conversion of vb.net to c#.

here is my vb.net 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


And here is my converted c# code (using online conversion tool) -

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;
   }
Here is the error ---

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


If I replace this Strings keyword to String then the InStr which is its associated functions 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:10pm
v2

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.

—SA
 
Share this answer
 
Comments
Maciej Los 26-May-15 2:16am    
5ed!
Sergey Alexandrovich Kryukov 26-May-15 2:19am    
Thank you Maciej.
—SA
See this: String.IndexOf[^] - returns zero-based index of the first occurrence of the specified string in this instance, as per InStr[^].

Note: you should stop doing it! Try to understand what code does and write it in your way. You'll never learn anything! Even if VB.NET and C# are quite similiar, there's a lot of differences!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-May-15 2:19am    
This is a really useful advice, my 5.
—SA
Maciej Los 26-May-15 2:21am    
Thank you, Sergey.

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