Solved

VB to C# Conversion

Posted on 2011-03-01
14
356 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What can cause the styling on a .NET site to not be found? 17 24
MediaHelp 4 21
InputLanguage 1 25
Release Dynamically Allocated Memory in C# 3 19
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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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