Wednesday, December 28, 2011

ActiveX controls at asp.net


As web application developers, we want to give our users a highly functional application. We want to provide our users with functionality like printing ,scanning images etc., but it is difficult due to the security and the disconnected architecture of the Internet .

1. create an activeX control
   a. create interface
     public interface ITest
    {
        string Name { get; }
        string SayHello(string str);
    }
    b.create class
     [Guid("a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4")]
    [ComVisible(true)]
    [ProgId("Test.Test")]
    public class TestClass:ITest
    {
         public string Name
        {
            get  { return "My Test Name"; }
        }
        public string SayHello(string str)
        {
            return "Hello " + str;
        }

    }
   c.register the activeX control:
      at the project properties window => build => check => "Register for COM interop"
2.Host control at wweb application
    a. add object tag to the html page
      <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />
    b. access the object and the functions/properties from javascript
      var myAx = new ActiveXObject("Test.Test");    => the name of ProgId
            alert(myAx.Name);  

   an example for the html:
     <script type="text/javascript">
        function getProperty() {
            var myAx = new ActiveXObject("Test.Test");
            alert(myAx.Name);
        }
        function getName() {
            var myAx = new ActiveXObject("Test.Test");
            var text = document.getElementById("myName").value;
            alert(myAx.GetMe(text));
        }
    </script>
    <table>
        <tr>
            <td>
                <input type="text" id="myName" />
            </td>
            <td>
                <input type="button" value="Property" onclick="getProperty()" />
            </td>
            <td>
                <input type="button" value="Function" onclick="getName()" />
            </td>
        </tr>
    </table>
    <object classid="CLSID:a991dd50-ff67-47b0-a4c3-f2b3e9fbd0f4" id="myCtrl" />

       

Sunday, December 25, 2011

Microsoft Reportviewer deployment


Dlls required for Microsoft reportviewer deployment
1 .Microsoft.ReportViewer.WebForms.dll or Microsoft.ReportViewer.WinForms.dll
2. Microsoft.ReportViewer.Common.dll


Aslo setup the Reporting Redistributeable version that corresponding to the version that used at the development , for example if you use reportViewer version 10 then you have to set up the  

Thursday, December 22, 2011

RDLC reports at MVC project


RDLC reports cannot have connection string and can't directly connected to the database , instead we use the "Object" datasource to provide data to the reports.

The "Object" datasource provides, at the design time , the report by the structure of the data that  will be passed to the report at the runtime

The datasources cannot be added to the MVC project so we need a class library that the datasources will be built upon

Steps

  1. create a class library project 
  2. add the "ViewModel" class -- that will have the structure of the data 
  3. for example
public class PatientViewModel
{
public int Id{get;set;}
public string Name{get;set;}
}
    4. at the solution explorer , select the ViewModelClassLibrary project
    5. at the menu bar => Data => select =>Add New DataSource 
    6. Data Source configuration wizard open => select "Object" Data source =>Next
    7.  select "PatientViewModel " class => finish
    8.create the MVC project 
   9. add new folder called "forms" at the project main folder
  10. add new rdlc report to the folder 
  11 . open the report => at the view menu => press " report data"
12 .create new datasets  => in the list , you will find the created data source at point  (7)
 and give the dataset a specific name => for example => PatientDataSet
13 . at the controller  , create an action for the report
 public ActionResult GetReport()
        {
            LocalReport rep = new LocalReport();
            rep.ReportPath = Server.MapPath("~/Forms/MyReport.rdlc");
            List<PatientViewModel> patients = GetPatients();
            rep.DataSources.Add(new ReportDataSource("PatientDataSet", patients));
 
            //Render the report
            _renderedBytes = rep.Render(
                _reportType,
                _deviceInfo,
                out _mimeType,
                out _encoding,
                out _fileNameExtension,
                out _streams,
                out _warnings);
 
            return File(_renderedBytes, _mimeType);
 
        }





Multiple validation summary at single page

The problem:
at razor , if we have 3 partial views each contains a validation summary control
when the validation works, and the validation summary supposed to give the usual red text , all the validations summary controls will view the error , even if the element that have the problem is not it its own view
example 

The solution:
the solution is to draw the validation summary only when you validate your form for example
  1. at the controller of "RegisterForm" , set the  Session["ValidationSummary"] = "RegisterForm"
  2. at the controller of "LoginForm" , set the  Session["ValidationSummary"] =  "LoginForm"
  3. at the   RegisterForm view
                    if ( Session["ValidationSummary"] == "RegisterForm")
            { 
   @Html.ValidationSummary(); 
            } 

    4. at the    LoginForm  view 
       if ( Session["ValidationSummary"] == " LoginForm ")
            { 
                   @Html.ValidationSummary(); 
            } 

       



you can find nother solution here NamedValidationSummary 



Error with MVC telerik


when using Telerik components , it may give you the erorr:
 "Unable to set value of the property '__MVC_FormValidation': object is null or undefined"
 solution :
 let the style sheets registeration to be:
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css") ) )

 instead of :
 @(Html.Telerik().StyleSheetRegistrar() .DefaultGroup(group => group
                                                                                   .Add("telerik.common.css")
                                                                                    .Add("telerik.Vista.css")
                                                                                   .Add("telerik.rtl.css")
                                                                                  .Combined(true)
                                                                                   .Compress(true) ) )