Solved

VB to C# Conversion

Posted on 2011-03-01
14
349 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
  • 8
  • 6
14 Comments
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
not a perfect tool but it will get you started

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

Author Comment

by:Takeoutdinner
Comment Utility
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
Comment Utility
what's the error.
0
 

Author Comment

by:Takeoutdinner
Comment Utility
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
Comment Utility

then

o.SelectParameters[0].DefaultValue

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

Author Comment

by:Takeoutdinner
Comment Utility
Please let me know the exact codes.
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
o.SelectParameters[0].DefaultValue = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ID")).ToString();
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Takeoutdinner
Comment Utility
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
Comment Utility
what is the error?
0
 

Author Comment

by:Takeoutdinner
Comment Utility
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
Comment Utility
if (!((Control)e.CommandSource).HasControls())


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

Accepted Solution

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

Author Comment

by:Takeoutdinner
Comment Utility
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
Comment Utility
what do you expect the results to be and what are you actually getting?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now