Solved

VB to C# Conversion

Posted on 2011-03-01
14
359 Views
Last Modified: 2013-12-17
Hi experts,

Please help conversion of following VB codes to C#.

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Master.EnablePersonalization = True
            canEdit = (Me.User.Identity.IsAuthenticated And _
                        (Me.User.IsInRole("Administrators") Or _
                        Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators")))

            If Not Me.IsPostBack Then
                If String.IsNullOrEmpty(Me.Request.QueryString("ForumID")) Then
                    GridView1.DataSourceID = "objBlogs"                             'Initial Entrance
                Else
                    If Integer.Parse(Me.Request.QueryString("ForumID")) = 0 Then    'All Categories
                        GridView1.DataSourceID = "objBlogs"
                    Else
                        GridView1.DataSourceID = "objBlogByID"                      'Category selected
                    End If
                End If

                Dim forumID As String = Me.Request.QueryString("ForumID")
                If forumID = "0" Then forumID = "" 'For objBlogs    03/09/09
                lnkNewThread1.PostBackUrl = _
                    String.Format(lnkNewThread1.PostBackUrl, forumID)
                lnkNewThread2.PostBackUrl = lnkNewThread1.PostBackUrl
                If Not String.IsNullOrEmpty(forumID) Then
                    Dim forum As Blog = BLL.Blogs.Blog.GetBlogByID(Integer.Parse(forumID))
                    Me.Title = String.Format(Me.Title, forum.Title)
                    ddlBlogs.SelectedValue = forumID
                    Globals.pubForumID = forumID
                Else
                    Dim forum As Blog = BLL.Blogs.Blog.GetBlogByID(4) 'Integer.Parse(forumID))
                    Me.Title = String.Format(Me.Title, forum.Title)
                    Globals.pubForumID = forum.ID    'If not selected, set first category number for Create New Thread
                    ddlBlogs.SelectedValue = 0      'But All Categories in the DropDownList 'forum.ID
                End If

                If String.IsNullOrEmpty(Me.Request.QueryString("ForumID")) Then     'Click BLOGS of Main menu bar
                    Page.ClientScript.RegisterStartupScript(GetType(Page), "OpenWindow", "<script language='javascript'>window.open('ShowBlogs.aspx" & "');</script>", False)
                Else
                    If Integer.Parse(Me.Request.QueryString("ForumID")) = 0 Then
                        Page.ClientScript.RegisterStartupScript(GetType(Page), "OpenWindow", "<script language='javascript'>window.open('ShowBlogs.aspx" & "');</script>", False)
                    End If
                End If
            End If
        End Sub

        Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender  'For SortExpression
            Dim gvDetails As GridView = GridView1.Rows(0).FindControl("Gridview2")
            gvDetails.DataBind()
            For Each row As GridViewRow In gvDetails.Rows
                Dim Author As Label = row.FindControl("lblAddedBy")
                Dim Chk As CheckBox = row.FindControl("cboOnlyForAuthor")
                Dim img As Image = row.FindControl("imgKey")
                If Chk.Checked Then
                    If Author.Text <> My.User.Name And Not (Me.User.IsInRole("Administrators")) Then row.Cells(0).Enabled = False
                End If
                row.Cells(4).Visible = canEdit
                row.Cells(5).Visible = canEdit
                row.Cells(6).Visible = canEdit
            Next row
        End Sub

        Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
            If Not CType(e.CommandSource, Control).HasControls Then
                Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
                Dim btnExpand As ImageButton = GridView1.Rows(row.RowIndex).FindControl("btnExpand")
                Dim gvDetails As GridView = GridView1.Rows(row.RowIndex).FindControl("Gridview2")
                'Dim gvdetails As GridView = CType(e.Row.FindControl("GridView2"), GridView)
                gvDetails.DataBind()

                If btnExpand.ImageUrl = "~/Images/expand.gif" Then
                    btnExpand.ImageUrl = "~/Images/collapse.gif"
                    gvDetails.Visible = True
                Else
                    btnExpand.ImageUrl = "~/Images/expand.gif"
                    gvDetails.Visible = False
                End If

                For Each row In gvDetails.Rows
                    row.Cells(4).Visible = canEdit
                    row.Cells(5).Visible = canEdit
                    row.Cells(6).Visible = canEdit
                Next row

            End If
        End Sub
        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then

                Dim o As ObjectDataSource = CType(e.Row.FindControl("odsPosts"), ObjectDataSource)
                o.SelectParameters(0).DefaultValue = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID"))
                e.Row.Cells(0).Visible = False

                Dim gvdetails As GridView = CType(e.Row.FindControl("GridView2"), GridView)
                gvdetails.DataBind()

                For Each row As GridViewRow In gvdetails.Rows
                    row.Cells(4).Visible = canEdit
                    row.Cells(5).Visible = canEdit
                    row.Cells(6).Visible = canEdit
                Next row

            End If
        End Sub
0
Comment
Question by:Takeoutdinner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 35010737
not a perfect tool but it will get you started

http://www.developerfusion.com/tools/convert/vb-to-csharp/
0
 

Author Comment

by:Takeoutdinner
ID: 35010962
Hi,

I am using "http://converter.telerik.com/" for conversion. And I cannot complete it so I want to have your advices. Let's start it with one by one.

1. I have an error at " o.SelectParameters[0].DefaultValue = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID"));" of RowDataBound. Please advise me. Then I will post next one.


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ObjectDataSource o = (ObjectDataSource)e.Row.FindControl("objPosts");
                o.SelectParameters[0].DefaultValue = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID"));

                e.Row.Cells[0].Visible = false;

                GridView gvdetails = (GridView)e.Row.FindControl("GridView2");
                gvdetails.DataBind();

                foreach (GridViewRow row in gvdetails.Rows)
                {
                    row.Cells[4].Visible = canEdit;
                    row.Cells[5].Visible = canEdit;
                    row.Cells[6].Visible = canEdit;
                }
            }
        }
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35010990
what's the error.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:Takeoutdinner
ID: 35011068
Object GridviewRow.DataItem
Gets the underlying data object to which the System.Web.UI.WebControls.GridviewRow object is bound.
Error:
Cannot implicitly convert type 'decimal' to 'string'
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35011117

then

o.SelectParameters[0].DefaultValue

must be expecting a string instead of your converted decimal. ToString() the object value.
0
 

Author Comment

by:Takeoutdinner
ID: 35011326
Please let me know the exact codes.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35011525
o.SelectParameters[0].DefaultValue = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID")).ToString();
0
 

Author Comment

by:Takeoutdinner
ID: 35011699
Thanks.

1. Next one, I have an error at "if (!((Control)e.CommandSource).HasControls)" and "foreach (GridViewRow row in gvDetails.Rows)" of :
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        //if (this.HasControls()) //
        if (!((Control)e.CommandSource).HasControls)
        {
                  GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                  ImageButton btnExpand = (ImageButton)GridView1.Rows[row.RowIndex].FindControl("btnExpand");
                  GridView gvDetails = (GridView)GridView1.Rows[row.RowIndex].FindControl("Gridview2");
                  //Dim gvdetails As GridView = CType(e.Row.FindControl("GridView2"), GridView)
                  gvDetails.DataBind();

                  //gvdetails.Visible = False
                  //gvDetails.PageSize = Integer.Parse(ddlPostsPerPage.SelectedValue)

                  if (btnExpand.ImageUrl == "~/Images/expand.gif") {
                        btnExpand.ImageUrl = "~/Images/collapse.gif";
                        gvDetails.Visible = true;
                  } else {
                        btnExpand.ImageUrl = "~/Images/expand.gif";
                        gvDetails.Visible = false;
                  }

            foreach (GridViewRow row in gvDetails.Rows)
            {
                        row.Cells[4].Visible = canEdit;
                        row.Cells[5].Visible = canEdit;
                        row.Cells[6].Visible = canEdit;
                  }
            }
      }
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35011710
what is the error?
0
 

Author Comment

by:Takeoutdinner
ID: 35012069
if (!((Control)e.CommandSource).HasControls): Operator '!' cannot be applied to operand of type 'method group'

foreach (GridViewRow row in gvDetails.Rows): A local variable named 'row' cannot be declared in this scope because it would give a ifferent meaning to 'row', which is already used in a 'parent or current' scope to denote something else
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35012085
if (!((Control)e.CommandSource).HasControls())


foreach (GridViewRow r in gvDetails.Rows)
0
 
LVL 22

Accepted Solution

by:
p_davis earned 125 total points
ID: 35012089
anything was labeled row in the foreach needs to be changed to r
0
 

Author Comment

by:Takeoutdinner
ID: 35012436
Looks good.
Hopefully last one: No error code, but working incorrectly in C#. Please refer to VB & C# below.

VB:
        Protected Sub ddlPostsPerPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPostsPerPage.SelectedIndexChanged
            Dim gvDetails As GridView = GridView1.Rows(0).FindControl("Gridview2")
            'Dim gvdetails As GridView = CType(e.Row.FindControl("GridView2"), GridView)
            gvDetails.DataBind()
            'gvdetails.Visible = False
            gvDetails.PageSize = Integer.Parse(ddlPostsPerPage.SelectedValue)
        End Sub

C#:
        protected void ddlPostsPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView gvDetails = (GridView)GridView1.Rows[0].FindControl("Gridview2");
            //Dim gvdetails As GridView = CType(e.Row.FindControl("GridView2"), GridView)
            gvDetails.DataBind();
            //gvdetails.Visible = False
            gvDetails.PageSize = int.Parse(ddlPostsPerPage.SelectedValue);
        }
0
 
LVL 22

Expert Comment

by:p_davis
ID: 35016804
what do you expect the results to be and what are you actually getting?
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question