Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB to C# Conversion

Posted on 2011-03-01
14
Medium Priority
?
363 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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