Solution for Date Format Issue After Deploying MVC Application in IIS 8

MVC Application deployed in IIS 7 using MSSQL Server 2005 and MSSQL Server 2008 R2 didn't encountered issue on date format. But after the compatibility test conducted for deployment in Window 8 operating system using IIS 8 and MSSQL Server 2014 dates encoded were displayed in (dd/mm/yyyy) format which differs from the original format which was (mm/dd/yyyy).

After series of test conducted to determine the cause, it was found out that you need to set the value of the following in 2 different options.

  1. culture
  2. uiCulture


Option 1: Via Internet Information Services



  1. From the features view select .Net Globalization
  2. Set Culture to English (United States) (en-US)
  3. Set UI Culture to English (United States) (en-US)
  4. Select Apply Action
  5. Restart IIS


Option 2: Via Webconfig

Add the following lines before  </system.web>

<globalization culture="en-US" uiCulture="en-US" />

Hope this helps.
Continue Reading

Deploying MVC Application With System.Web.Mvc Update



Error:

Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

This occurred during browsing of MVC Application published in the IIS.

Cause:

Automatic Windows update thus, ASP.Net MVC 3 was included in the update. Web application reference was replaced by newer version.


Fixes:

Open Web config and replace the following highlighted by yellow with the values highlighted by green.

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>


  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>



Continue Reading

Display Key and Values of AppSettings To DataGridView From External Webconfig Using Vb.Net




Private Sub btnOpenFile_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFile.Click
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then

            Dim appSetDT As DataTable
            appSetDT = New DataTable("AppSettings")

            Dim KeyNameCol As DataColumn = New DataColumn("KeyNameCol")
            KeyNameCol.DataType = System.Type.GetType("System.String")
            Dim KeyValueCol As DataColumn = New DataColumn("KeyValueCol")
            KeyValueCol.DataType = System.Type.GetType("System.String")
            appSetDT.Columns.Add(KeyNameCol)
            appSetDT.Columns.Add(KeyValueCol)

            Dim FileName As String = OpenFileDialog1.FileName.ToString()
            Dim map As New ExeConfigurationFileMap()
            map.ExeConfigFilename = FileName
            Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)

            Dim appSettings = config.AppSettings
   
            Dim keyCount As Integer = appSettings.Settings.Count
            Dim keyName As String
            Dim keyValue As String

            'App Settings
            If appSettings.Settings.Count = 0 Then
                MsgBox("AppSettings is empty.", MsgBoxStyle.Information)
            Else
                For Each key In appSettings.Settings.AllKeys
                    keyName = key
                    keyValue = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None).AppSettings.Settings(keyName).Value.ToString()        

                    Dim appRw As DataRow
                    appRw = appSetDT.NewRow()
                    appRw.Item("KeyNameCol") = keyName
                    appRw.Item("KeyValueCol") = keyValue
                    appSetDT.Rows.Add(appRw)
                Next

            End If
            'Load Data Table to Data Grid View
            dgvAppSettings.DataSource = appSetDT
            dgvAppSettings.Columns(0).Width = (dgvAppSettings.Width - 25) / 2
            dgvAppSettings.Columns(1).Width = (dgvAppSettings.Width - 25) / 2
            dgvAppSettings.Columns(0).HeaderText = "Key"
            dgvAppSettings.Columns(1).HeaderText = "Value"

     End If
    End Sub
Continue Reading

SQL Script to Add Identity Column

Supposing you have 50 tables without identity column, adding identity manually in the table designer would take you so much time. Here's a script to automatically add identity to a column.

List all tables without identity column.
SELECT 
(SCHEMA_NAME(schema_id) + '.' + name) as SchemaTable
FROM sys.tables
WHERE [name] NOT IN
(
SELECT 
OBJECT_NAME(OBJECT_ID) AS TABLENAME
FROM SYS.IDENTITY_COLUMNS
)
ORDER BY SchemaTable;
GO

Add identity
DECLARE cur CURSOR FOR 
select   
a.TABLE_SCHEMA as sn, 
a.table_name as tn, 
a.column_name as cn 
from information_schema.columns as a
inner join sys.tables as b on a.table_Name = b.name
where table_schema in(schema names) and  
ordinal_position <= 1 and 
table_name NOT IN(SELECT OBJECT_NAME(OBJECT_ID) AS TABLENAME   FROM SYS.IDENTITY_COLUMNS)and 
substring(a.column_name, len(column_name)-1, 2) = 'Id' 

DECLARE @sn sysname,
@tn sysname, 
@cn sysname, 
@sql varchar(500) 
OPEN cur     
FETCH NEXT FROM cur INTO @sn, @tn, @cn 

WHILE @@FETCH_STATUS = 0     
BEGIN 
SET @sql = 'ALTER TABLE [' + @sn + '].['+ @tn +'] ADD ' + @tn +' int NOT NULL IDENTITY (1,1)'    
PRINT @sql   
exec (@sql)  
FETCH NEXT FROM cur INTO @sn, @tn, @cn     
END 
CLOSE cur     
DEALLOCATE cur

Happy coding :)
 
Continue Reading

SQL Script to Add Primary Key

Adding primary key using designer in SQL would take you so much time.
Here's a script that would automatically list all the tables without a primary key and create one. You can also filter by schema.

List all tables without Primary Key 
select   
a.table_name, 
a.column_name,
a.ordinal_position
from information_schema.columns as a
inner join sys.tables as b on a.table_Name = b.name
where ordinal_position <= 1 
and (object_id not in(SELECT object_id from sys.indexes WHERE is_primary_key=1))

Add Primary key

DECLARE cur CURSOR FOR 
select   
a.TABLE_SCHEMA as sn, 
a.table_name as tn, 
a.column_name as cn 
from information_schema.columns as a
inner join sys.tables as b on a.table_Name = b.name
where table_schema in(schema names) and  
ordinal_position <= 1 and 
(object_id not in(SELECT object_id from sys.indexes WHERE is_primary_key=1)) and  
substring(a.column_name, len(column_name)-1, 2) = 'Id' 

DECLARE @sn sysname,
@tn sysname, 
@cn sysname, 
@sql varchar(500) 
OPEN cur     
FETCH NEXT FROM cur INTO @sn, @tn, @cn 

WHILE @@FETCH_STATUS = 0     
BEGIN 
SET @sql = 'ALTER TABLE [' + @sn + '].['+ @tn +']  ADD  CONSTRAINT [PK_' +@tn +'_'+ @cn+ '] PRIMARY KEY CLUSTERED
(['+ @cn+'] ASC) ON [PRIMARY]'    
PRINT @sql   
exec (@sql)  
FETCH NEXT FROM cur INTO @sn, @tn, @cn     
END 
CLOSE cur     
DEALLOCATE cur
 
Happy coding ;)

Continue Reading

Unrecognized target framework : Fixed

During testing of the published file in IIS, I've encountered this issue regarding the target framework.


To resolve this, you need to change the target framework of the Application Pool.


  • Select the name of your application from the list.
  • Double click to Edit.
  • From the dropdownlist of the .Net Framework version, select .Net Framework v4.0.30319
  • Click OK
  • Refresh your site



Continue Reading

Tables dropped and re-created - SQL: Fixes

When you are modifying table columns in SQL example changing data type or adding a new column, after you click save you are prompted with this error: "The changes you have made require the following tables to be dropped and re-created" from SQL Server."

To solve this issue you must do the following:

  • Go to Tools, and select Options
  • Select designer in the pop-up window
  • Uncheck Prevent saving changes that requires table re-creation
  • Click OK




Continue Reading