Thursday 31 January 2013

C# VB.NET Excel.Interop - Change tab color

  oSheet.Tab.Color = = RGB(31, 73, 125)

More info:

Wednesday 30 January 2013

C# ASP.NET - How to access controls in master page from child page

Watch whole example on YouTube.

3 ways to do that
1. add text box to master page
__________________________________________________________________________
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
 
        </asp:ContentPlaceHolder>
    </div>
    </form>
2. In code behind of the master page add the following property
        public TextBox TextBox
        {
            get { return this.TextBox1; }
        }
3. In child page add the following code:
        protected void Page_Load(object sender, EventArgs e)
        {
             Site1 m = Master as Site1;
             m.TextBox.Text = "aaaaaaaaaaaaa";
        }
__________________________________________________________________________
Another - better example
__________________________________________________________________________
1. add text box to master page
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
 
        </asp:ContentPlaceHolder>
    </div>
    </form>
2. In code behind of master page add the following property
        public TextBox TextBox
        {
            get { return this.TextBox1; }
        }
3. Add the following to html of child page
<%@ MasterType virtualpath="~/Site1.master" %>
4. In child page code behind add the following code
        protected void Page_Load(object sender, EventArgs e)
        {
             Master.TextBox.Text = "BBB:";
        }
__________________________________________________________________________
By referenece
__________________________________________________________________________
1. add text box to master page
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
 
        </asp:ContentPlaceHolder>
    </div>
    </form>
2.In child page code behind add the following code
     protected void Page_Load(object sender, EventArgs e)
        {
            TextBox tb;
            tb = (TextBox)Master.FindControl("TextBox1");
            tb.Text = "BBB";
        }

Tuesday 29 January 2013

VB.NET , C# EXCEL.Interop - define thick line programmatically

 osheet.Range("O1", "O10").Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous

You can specify continuous line, but to specify thick line just assign number 7 twice

 osheet.Range("O1", "O10").Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = 7
osheet.Range("O1", "O10").Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = 7
This is not very elegant way to do it, but I don't have time to investigate further.
Probably number 7 should be replaced by something else.

More info:


and here:

Microsoft Outlook - How to fix error Cannot start Microsoft Outlook. Cannot open the Outlook window. Invalid XML, the view cannot be loaded.








To fix it, execute the following command:
<br />




go to start/run and type outlook.exe /ResetNavPane and press enter.






Sunday 27 January 2013

C# How to fix error Type and identifier are both required in a foreach statement

To fix Type and identifier are both required in a foreach statement  

Watch online
Replace
            List<string> names = new List<string>();
            names.Add("One");
            names.Add("Two");
            names.Add("Three");
            names.Add("Four");
            names.Sort();
            string n;
            foreach(n in names){

                Response.Write(n);
            }
With
            List<string> names = new List<string>();
            names.Add("One");
            names.Add("Two");
            names.Add("Three");
            names.Add("Four");
            names.Sort();
           
            foreach(string n in names){
                Response.Write(n);
            }

Wednesday 23 January 2013

C# How to fix error '' does not contain a constructor that takes 0 arguments

    public class SchoolClass
    {
        private int school_id;
        private string school_name;

        public int schoolID
        {
            get { return school_id; }
            set { school_id = value; }
        }
        public string SchoolName
        {
            get { return school_name; }
            set { school_name = value; }
        }
        public SchoolClass()
        {
            school_id = 0;
            school_name = string.Empty;
        }

        public SchoolClass(int id, string n)
        {
            school_id = id;
            school_name = n;
        }
    }


if code in red is missing then the following error will be displayed when executing the following code:
SchoolClass sc = new SchoolClass() { schoolID = 2, SchoolName = "some name" };

How to fix error Inconsistent accessibility: base class 'class name' is less accessible than class ' '

Inconsistent accessibility: base class 'class name' is less accessible than class ' '  

- it appears when trying to initialize abstract class





Replace
public class car : vehicle

With
class car : vehicle

Tuesday 22 January 2013

C# Entity - Simple tutorial with drop down list and grid view

In this example I will create simple web project that will have drop down list and grid view.
User will be able to select some value from drop down list and based on that selection grid view will be populated.

Step by step instructions can be found on this video:



1.  Create Empty Web Application
2.  Add entity to it (step by step instructions can be found in video or here http://howtodomssqlcsharpexcelaccess.blogspot.ca/2012/03/visual-studio-2010-ms-sql-2012-aspnet.html
3. Add web form then add to it drop down list and gridview
4. In code behind of the page add the following using statements
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.ModelBinding;
5. Create the following 2 methods that will load data for ddl and gridview
        private SchoolEntities db = new SchoolEntities();
        public IQueryable<Dpt> GetDepartments()
        {
            var query = this.db.Dpts;
            return query;
        }
        public IQueryable<User> GetSelectedUser([Control("DropDownList1")] int? DepartmentId)
        {
            var query = this.db.Users
                .Where(u => u.DepartmentId == DepartmentId);
            return query;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

Note that GetSelectedUser method receives value from drop down list control
6. Now ensure that both controls have all needed properties:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" SelectMethod="GetDepartments"
    ItemType="Model_Binding.Dpt" DataTextField="DepartmentName" DataValueField="DepartmentID" >
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server" ItemType="Model_Binding.User" SelectMethod="GetSelectedUser"
     DataKeyNames="UserId" AllowPaging="True" AllowSorting="True" PageSize="10" AutoGenerateColumns="False">
    <Columns>
       <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />    </Columns>
</asp:GridView>
7. Video example will show you database structure.

How to fix Error The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

Watch online
I am getting the following error
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

I have simple web app with gridview.  data is loaded through entity 
SelectMethod="GetDepartments"
            ItemType="Model_Binding.Dpt"


        private SchoolEntities db = new SchoolEntities();
        public IQueryable<Dpt> GetDepartments()
        {
            var query = this.db.Dpts
              .Include(c => c.Users);
            return query;
        }
 

Of course I allow sorting and paging - therefore that error.
To fix it add DataKeyNames

        <asp:GridView ID="GridView1" runat="server" SelectMethod="GetDepartments"
            ItemType="Model_Binding.Dpt" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataKeyNames="DepartmentId" PageSize="1">

Monday 21 January 2013

MSSQL - How to fix error Saving changes is not permitted.


Watch example online


The following error is displayed when trying to modify table using visual components:

Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.


In case you receive this error - execute the following query instead of using visual components:
Alter table UsersTable Alter Column LastName Varchar(50)

Friday 18 January 2013

VB.NET - Excel - Pivot - how to remove subtotals

Dim ptTable As Excel.PivotTable = osheet.PivotTables.Add(PivotCache:=ptCache, TableDestination:=xlRange, TableName:="SomeName")
Dim ptColumn As Excel.PivotField = ptTable.PivotFields("Columm Name")
Dim ptRow As Excel.PivotField = ptTable.PivotFields("RowName")

ptTable.ColumnGrand = True
ptTable.RowGrand = True
For Each ptRow In ptTable.PivotFields

    Try
    ptRow.Subtotals(1) = True
    ptRow.Subtotals(1) = False
    Catch ex As Exception

    End Try

Next ptRow

Thursday 17 January 2013

VB.NET - Excel - Pivot table - how to fix error Type Mismatch

Replace
Dim xlRange As Excel.Range = CType(osheet, Excel.Worksheet).Range("C2:AE70000")
Dim ptCache As Excel.PivotCache = obook.PivotCaches.Add(XlPivotTableSourceType.xlDatabase, xlRange)

With
              Dim ptCache As Excel.PivotCache = obook.PivotCaches.Add(SourceType:=Excel.XlPivotTableSourceType.xlDatabase, SourceData:="Emb!C2:AE70000")

Wednesday 16 January 2013

VB.Net Excel - how to fix error Unable to set the OutlineLevel property of the Range class

 Replace
osheet.Range("G1").OutlineLevel = 1.0
With 
osheet.Columns("G").OutlineLevel = 1.0

VB.NET Excel - How to group rows and columns

  
' Group rows
osheet.Range("A1" , "A20" ).Group(True, , , )
' Group columns
osheet.Range("D1", "G1").EntireColumn.Group(True, , , )


-- colapse groups
'osheet.Columns("G").OutlineLevel = 1.0
osheet.Outline.ShowLevels(ColumnLevels:=1)

VB.NET Excel Pivot - Move sigma values to column

                Dim ptSigmaRow As Excel.PivotField
                For Each ptSigmaRow In ptTable.RowFields
                    Dim d As String = ptSigmaRow.Name
                    If ptSigmaRow.Name = "Data" Then
                        ptSigmaRow.Orientation = XlPivotFieldOrientation.xlColumnField
                    End If
                Next

Friday 4 January 2013

MSSQL How to get numbers after the decimal



  Select Decimal, Decimal % 1 as Truncated
  FROM TestingDecimal3
  Where Decimal % 1  > 0
 
  Select Float, (Convert(decimal(5,2),Float) % 1) as Truncated
  From TestingDecimal3

Youtube example

How to fix error The data types float and int are incompatible in the modulo operator.

Replace
  Select Float, Float % 1 As Truncated
  From TestingDecimal2
with
  Select Float, (Convert(decimal(5,2), Float ) % 1) As Truncated
  From TestingDecimal2

Youtube example

Thursday 3 January 2013

How to fix error - The maximum report processing jobs limit configured by your system administrator has been reached.

In my case I generate PDF file using Crystal Reports and VB.NET
In order to get rid of this error, don't forget to call close and dispose methods once report has been generated.


            cr.Close()
            cr.Dispose()