Solved

VB to C# Conversion

Posted on 2011-03-01
14
357 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
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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