Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding C# to a asp.net project

Posted on 2006-07-07
6
Medium Priority
?
1,080 Views
Last Modified: 2012-05-05
Can someone give me the dummy version of how to do this.  I would like to add a c# file into my .net project.  My entire solution at this point is all asp.net.  How do you integrate C# into a project.  How would you call a c# function on a vb page.  Thanks

0
Comment
Question by:Robb Hill
[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
  • 3
  • 3
6 Comments
 
LVL 11

Author Comment

by:Robb Hill
ID: 17058658
More specifically....

I have a cs file that looks like this.  I found this one the web courtesy of the code project.  I would like to access the
function DataGridExcelExporter from my vb code behind.  PLease help!!

using System;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.Util;


namespace Web.Generic.DataGridTools
{
      /// <summary>
      /// Serves as the base class that defines the methods, properties and events common
      /// to all datagrid exporters in the Web.Generic.DataGridTools
      /// </summary>
      public abstract class DataGridExporterBase
      {
            /// <summary>
            /// Holds a reference to the datagrid being exported
            /// </summary>
            protected DataGrid MyDataGrid;

            /// <summary>
            /// Holds a reference to the page where the datagrid locates
            /// </summary>
            protected Page CurrentPage;

            /// <summary>
            /// Overloaded. Initializes a new instance of the DataGridExporterBase class.
            /// </summary>
            /// <param name="dg">The datagrid to be exported</param>
            /// <param name="pg">The page to which the datagrid is to be exported</param>
            public DataGridExporterBase(DataGrid dg, Page pg)
            {
                  MyDataGrid = dg;
                  CurrentPage = pg;
            }

            /// <summary>
            /// Overloaded. Initializes a new instance of the DataGridExporterBase class.
            /// </summary>
            /// <param name="dg">The datagrid to be exported</param>
            public DataGridExporterBase(DataGrid dg):this(dg, dg.Page)
            {
            }

            /// <summary>
            /// Exports the current datagrid
            /// </summary>
            public abstract void Export();
      }

      /// <summary>
      /// Exports a datagrid to a excel file.
      /// </summary>
      /// <requirements>Microsoft Excel 97 or above should be installed on the client machine in order to make
      /// this function work
      /// </requirements>
      public class DataGridExcelExporter:DataGridExporterBase
      {

            /// <summary>
            /// CSS file for decoration, se it if any or dont use it
            /// </summary>
            private const string MY_CSS_FILE = "./css/MDF.css";

            /// <summary>
            /// Overloaded. Initializes a new instance of the DataGridExcelExporter class.
            /// </summary>
            /// <param name="dg">The datagrid to be exported</param>
            /// <param name="pg">The page to which the datagrid is to be exported</param>
            public DataGridExcelExporter(DataGrid dg, Page pg):base(dg, pg)
            {
            }

            /// <summary>
            /// Overloaded. Initializes a new instance of the DataGridExcelExporter class.
            /// </summary>
            /// <param name="dg">The datagrid to be exported</param>
            public DataGridExcelExporter(DataGrid dg):base(dg)
            {
            }

            /// <summary>
            /// Overloaded. Exports a datagrid to an excel file, the title of which is empty
            /// </summary>
            public override void Export()
            {
                  Export(String.Empty);
            }

            /// <summary>
            /// Renders the html text before the datagrid.
            /// </summary>
            /// <param name="writer">A HtmlTextWriter to write html to output stream</param>
            protected virtual void FrontDecorator(HtmlTextWriter writer)
            {
                  writer.WriteFullBeginTag("HTML");
                  writer.WriteFullBeginTag("Head");
                  writer.RenderBeginTag(HtmlTextWriterTag.Style);
                  writer.Write("<!--");
                  
                  StreamReader sr = File.OpenText(CurrentPage.MapPath(MY_CSS_FILE));
                  String input;
                  while ((input=sr.ReadLine())!=null)
                  {
                        writer.WriteLine(input);
                  }
                  sr.Close();
                  writer.Write("-->");
                  writer.RenderEndTag();
                  writer.WriteEndTag("Head");
                  writer.WriteFullBeginTag("Body");
            }

            /// <summary>
            /// Renders the html text after the datagrid.
            /// </summary>
            /// <param name="writer">A HtmlTextWriter to write html to output stream</param>
            protected virtual void RearDecorator(HtmlTextWriter writer)
            {
                  writer.WriteEndTag("Body");
                  writer.WriteEndTag("HTML");
            }

            /// <summary>
            /// Exports the datagrid to an Excel file with the name of the datasheet provided by the passed in parameter
            /// </summary>
            /// <param name="reportName">Name of the datasheet.
            /// </param>
            public virtual void Export(string reportName)
            {
                  ClearChildControls(MyDataGrid);
                  MyDataGrid.EnableViewState = false;//Gets rid of the viewstate of the control. The viewstate may make an excel file unreadable.

            
                  CurrentPage.Response.Clear();
                  CurrentPage.Response.Buffer = true;

                  //This will make the browser interpret the output as an Excel file
                  CurrentPage.Response.AddHeader( "Content-Disposition", "filename="+reportName);
                  CurrentPage.Response.ContentType="application/vnd.ms-excel";

                  //Prepares the html and write it into a StringWriter
                  StringWriter stringWriter = new StringWriter();
                  HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
                  FrontDecorator(htmlWriter);
                  MyDataGrid.RenderControl(htmlWriter);
                  RearDecorator(htmlWriter);

                  //Write the content to the web browser
                  CurrentPage.Response.Write(stringWriter.ToString());
                  CurrentPage.Response.End();
            }

            /// <summary>
            /// Iterates a control and its children controls, ensuring they are all LiteralControls
            /// <remarks>
            /// Only LiteralControl can call RenderControl(System.Web.UI.HTMLTextWriter htmlWriter) method. Otherwise
            /// a runtime error will occur. This is the reason why this method exists.
            /// </remarks>
            /// </summary>
            /// <param name="control">The control to be cleared and verified</param>
            private void RecursiveClear(Control control)
            {
                  //Clears children controls
                  for (int i=control.Controls.Count -1; i>=0; i--)
                  {
                        RecursiveClear(control.Controls[i]);
                  }

                  //
                  //If it is a LinkButton, convert it to a LiteralControl
                  //
                  if (control is LinkButton)
                  {
                        LiteralControl literal = new LiteralControl();
                        control.Parent.Controls.Add(literal);
                        literal.Text = ((LinkButton)control).Text;
                        control.Parent.Controls.Remove(control);
                  }
                  //We don't need a button in the excel sheet, so simply delete it
                  else if(control is Button)
                  {
                        control.Parent.Controls.Remove(control);
                  }

                  //If it is a ListControl, copy the text to a new LiteralControl
                  else if(control is ListControl)
                  {
                        LiteralControl literal = new LiteralControl();
                        control.Parent.Controls.Add(literal);
                        try
                        {
                              literal.Text = ((ListControl)control).SelectedItem.Text;
                        }
                        catch
                        {
                        }
                        control.Parent.Controls.Remove(control);
                                    
                  }
                  //You may add more conditions when necessary

                  return;
            }

            /// <summary>
            /// Clears the child controls of a Datagrid to make sure all controls are LiteralControls
            /// </summary>
            /// <param name="dg">Datagrid to be cleared and verified</param>
            protected void ClearChildControls(DataGrid dg)
            {
                  
                  for(int i = dg.Columns.Count -1 ; i>=0; i--)
                  {
                        DataGridColumn column = dg.Columns[i];
                        if (column is ButtonColumn)
                        {
                              dg.Columns.Remove(column);
                        }
                  }
                  
                  this.RecursiveClear(dg);
                  
            }

      }

      /// <summary>
      /// HTML Encodes an entire DataGrid.
      /// It iterates through each cell in the TableRow, ensuring that all
      /// the text being displayed is HTML Encoded, irrespective of whether
      /// they are just plain text, buttons, hyperlinks, multiple controls etc..
      /// </summary>
      public class CellFormater
      {
            /// <summary>
            /// Constructs an instance of the CellFormater class.
            /// </summary>
            public CellFormater()
            {
                  //
                  // TODO: Add constructor logic here
                  //
                  
            }

            /// <summary>
            /// Method that HTML Encodes an entire DataGrid.
            /// It iterates through each cell in the TableRow, ensuring that all
            /// the text being displayed is HTML Encoded, irrespective of whether
            /// they are just plain text, buttons, hyperlinks, multiple controls etc..
            /// <seealso cref="System.Web.UI.WebControls.DataGrid.ItemDataBound">DataGrid.ItemDataBound Event</seealso>
            /// </summary>
            /// <param name="item">
            /// The DataGridItem that is currently being bound in the calling Web
            /// Page's DataGrid.ItemDataBound Event.
            /// </param>
            /// <remarks>
            /// This method should be called from the
            /// <c>DataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)</c>
            /// event in the respective Web View Codebehind.
            /// </remarks>
            /// <example>
            ///          We want to HTMLEncode a complete DataGrid (all columns and all
            ///          rows that may/do contain characters that will require encoding
            ///          for display in HTML) called dgIssues.
            ///          Use the following code for the ItemDataBound Event:
            ///          <code>
            ///               private void dgIssues_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            ///               {
            ///                    WebMethod wm = new WebMethod();
            ///                    wm.DataGrid_ItemDataBound_HTMLEncode((DataGridItem) e.Item);
            ///               }//dgIssues_ItemDataBound
            ///          </code>
            /// </example>
            public void AdHocHTMLEncode(System.Web.UI.WebControls.DataGridItem item)
            {
                  bool doHTMLEncode = false;
                  switch (item.ItemType)
                  {                        
          #region DataBound
                              //The following case statements are in ascending TableItemStyle order.
                              //See ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclassitemstyletopic.htm for details.
                        case System.Web.UI.WebControls.ListItemType.Item:
                        {
                              doHTMLEncode = true;
                              break;
                        }//ListItemType.Item
                        case System.Web.UI.WebControls.ListItemType.AlternatingItem:
                        {                              
                              doHTMLEncode = true;
                              break;
                        }//ListItemType.AlternatingItem
                        case System.Web.UI.WebControls.ListItemType.SelectedItem:
                        {
                              doHTMLEncode = true;
                              break;
                        }//ListItemType.SelectedItem                              
                        case System.Web.UI.WebControls.ListItemType.EditItem:
                        {
                              //These should not be prone to this as TextBoxes aren't.
                              doHTMLEncode = false;
                              break;
                        }//ListItemType.EditItem
          #endregion DataBound
          #region Non-DataBound
                              //The remainder are the other ListItemTypes that are non-Data-bound.
                        case System.Web.UI.WebControls.ListItemType.Header:
                        {
                              //We might have specified Headers like "<ID>".
                              doHTMLEncode = true;
                              break;
                        }//ListItemType.Header
                        case System.Web.UI.WebControls.ListItemType.Footer:
                        {
                              //Similarly for the Footer as with the Header.
                              doHTMLEncode = true;

                              break;
                        }//ListItemType.Footer
                        case System.Web.UI.WebControls.ListItemType.Pager:
                        {
                              //With just numbers or buttons, none is required.
                              //However, for buttons, this is not strictly true as you
                              //need to specify the text on the buttons. But the Property
                              //Builder for the DataGrid hints in its defaults that these
                              //need to be HTMLencoded anyway.
                              doHTMLEncode = false;
                              break;
                        }//ListItemType.Pager
                        case System.Web.UI.WebControls.ListItemType.Separator:
                        {
                              doHTMLEncode = false;
                              break;
                        }//ListItemType.Separator
          #endregion Non-DataBound
                        default:
                        {
                              //This will never be executed as all ItemTypes are listed above.
                              break;
                        }//default
                  }//switch

                  if (doHTMLEncode)
                  {
                        //Encode the cells dependent on the type of content
                        //within (e.g. BoundColumn, Hyperlink), taking into account
                        //that there may be more than one (or even zero) control in
                        //each cell.
                        System.Web.UI.WebControls.TableCellCollection cells = (System.Web.UI.WebControls.TableCellCollection)item.Cells;
                        foreach (System.Web.UI.WebControls.TableCell cell in cells)
                        {
                              if (cell.Controls.Count != 0)
                              {
                                    foreach (System.Web.UI.Control ctrl in cell.Controls)
                                    {
                                          if (ctrl is Button)
                                          {
                                                Button btn = (Button) ctrl;
                                                btn.Text = HttpUtility.HtmlEncode(btn.Text);
                                          }//if
                                          else if (ctrl is HyperLink)
                                          {
                                                HyperLink hyp = (HyperLink) ctrl;
                                                hyp.Text = HttpUtility.HtmlEncode(hyp.Text);
                                                //hyp.NavigateUrl = HttpUtility.UrlEncode(hyp.NavigateUrl);
                                          }//else if
                                          else if (ctrl is LinkButton)
                                          {
                                                LinkButton lb = (LinkButton) ctrl;
                                                lb.Text = HttpUtility.HtmlEncode(lb.Text);
                                          }//else if
                                          // this check is for to change the forecolor of REJECTED activities to red
                                          else if(ctrl is Label)
                                          {
                                                Label objL = (Label)ctrl;
                                                if(objL.Text == "REJECTED")
                                                      objL.ForeColor = System.Drawing.Color.Red;
                                          }//else if
                                    }//foreach
                              }//if
                              else
                              {
                                    //The cell is a BoundColumn.
                                    if (cell.Text.ToLower().Trim()!="&nbsp;")
                                          cell.Text = HttpUtility.HtmlEncode(cell.Text);
                                    
                              }//else
                        }//foreach
                  }//if
            }//DataGrid_ItemDataBound_HTMLEncode
      }
}
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 17059218
I have compiled this into a dll and am trying to access the functions inside..but cannot get them to wokr as the c# indicates it should
0
 
LVL 13

Expert Comment

by:PraxisWeb
ID: 17059379
You have a few options here...

You can have C# and VB projects in a Single Solution...

So in your Solution that contains your VB ASP.NET project:
* Add a new C# project (class library)
* Remove the Class.cs default file it puts in there.
* Use add existing file to the project to add your c# file
* Build the project - not solution.

Go back to your ASP.NET project and Add a reference to your C# project  and you should be good to go and able to debug both and all that jazz.
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 13

Assisted Solution

by:PraxisWeb
PraxisWeb earned 2000 total points
ID: 17059403
If you already have the dll compiled and part of a seperate project make sure you add a reference to it and as long as you reference its namespace you should be good to go.

in either case you would likely put in a
Imports Web.Generic.DataGridTools at the top of your VB.Net code behinds
then you should be able to access your function as needed.
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 17059579
I compiled the c# page into a class dll.  I added the dll to my bin and as a reference in my vb.net project.  I have importorted the namespace into my project.

I am trying now to call the function in the C# called datagridexcel exporter.  I should be able to just pass the name of my datagrid and a title into that function and be good to go.
This isnt working.  Is there somethign wrong with the c#  or in my calling..

I am doing this to get to the function.  The function will not accept two arguements for some reason...it only promts for a name as string in  the function call.  Im confused..please help.

dim test as dgExporter.datagridExcelExporter


test.export(dgSOP,"Title")
0
 
LVL 13

Accepted Solution

by:
PraxisWeb earned 2000 total points
ID: 17060811
Ah,

Looking at your code from above the problem is that you have not initialized your Exporter...

Your exporter declares two constructors:

          public DataGridExcelExporter(DataGrid dg, Page pg):base(dg, pg)
          {
          }

          public DataGridExcelExporter(DataGrid dg):base(dg)
          {
          }

The code snippet you provided above only declares the variable for use, but does not initialize it:
    dim test as dgExporter.datagridExcelExporter

needs to be:
    dim test as dgExporter.datagridExcelExporter = new dgExporter.datagridExcelExporter(dgSOP)

Then you can use the Export virtual as defined:
          public virtual void Export(string reportName)  <--- not only a single string parameter is defined.

test.Export("Title")

0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

715 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