Showing posts with label errors. Show all posts
Showing posts with label errors. Show all posts

Wednesday, August 1, 2012

Why not ending URL with a slash resulted in 404 File not found error

Came accross an issue I have not encountered before - excluding trailing slash after URL.

The path to the web application I was accessing (sent to me in an e-mail) was http://ServerName/MainDirectory/Subdirectory. When I opened it in IE, I received 404 error - File not found.

The thing is that traditonally, when URL is pointing to a file, it does not include a trailing slash, but URL pointing to a directory does. So whenever the user does not put a trailing slash into the URL, the web server looks for a file with that name as opposed to a directory. leaving off a trailing slash results in a redirect if the file with that name is not found and goes to the folder with that name and looks for a default file in the directory. And that is why it takes slightly longer to load the URL without a trailing slash.

In my case I got an error and was quite puzzled as to why I get an error instead of redirect. I looked in IIS Manager and saw something strange - not only the application directory was configured as an application but all its subfolders. So that was why redirect didn't take place - web server did not see a subfolder as a subfolder but as an application.

I am going to look into it further and try to figure out why did developer configured each subdirectory as an application...

Thursday, May 3, 2012

Login failed for user [name] error

So you have been moving database from one server or instance to another, you backed up and restored the database, scripted all logins, did everything in the book, and suddently, when loging in with sql server login, that worked so well before, you get "Login failed for user [name]" error.

First thing to check is whether your SQL Server is configured to use Mixed mode or Windows only authentication. because if it is configured to use Windows only, any sql login will be rejected, no matter what type of permissions it has.

Right-click on the server name, go to Properties, select Security and make sure "SQL Server and Windows Authentication mode" is selected.

Wednesday, May 4, 2011

Strange occurrence when returning Datatable from a function

I was trying to retrieve some data from a SQL Server database using Powershel using the following function:


function GetLocationData($Name)
{
$SqlConnection = New-Object system.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=ServerName;Database=MyDB;User Id=dbuser;Password=pwd;"

$SqlCommandText = "SELECT Field1, Field2 FROM TableName WHERE [Name]='" + $Name + "'"
$SqlConnection.Open()

$SqlCmd = New-Object Data.SqlClient.SqlCommand($SqlCommandText, $SqlConnection)
$Reader = $SqlCmd.ExecuteReader()

$Datatable = New-Object System.Data.DataTable
$DataTable.Load($Reader)

# Close the database connection
$sqlConnection.Close()

return $Datatable
}


When I would call it from my script


$dt = New-Object System.Data.DataTable
$dt=GetLocationData $item.name
Write-Host "row count is " + $dt.Rows.Count


It would not return anything and if I tried accessing a field


Write-Host $dt.Rows[0].Field1


I would get an error "Cannot index into null array"


The issue is that function would be returning a child collection of the table object instead of the table itself. What helped me is using the comma operator to wrap the rows collection in an array so that when the array is unrolled leaving me with the original rows collection.

So all I had to do to fix this issue is put a comma in the return statement right before datatable object:


function GetLocationData($Name)
{
$SqlConnection = New-Object system.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=ServerName;Database=MyDB;User Id=dbuser;Password=pwd;"


$SqlCommandText = "SELECT Field1, Field2 FROM TableName WHERE [Name]='" + $Name + "'"
$SqlConnection.Open()

$SqlCmd = New-Object Data.SqlClient.SqlCommand($SqlCommandText, $SqlConnection)
$Reader = $SqlCmd.ExecuteReader()

$Datatable = New-Object System.Data.DataTable
$DataTable.Load($Reader)

# Close the database connection
$sqlConnection.Close()

return ,$Datatable
}

Wednesday, July 28, 2010

Error when exporting data to Excel

While exporting data from SQL Server 2005 to Excel, I got an error "Columns "Field1" and "F1" cannot convert between unicode and non-unicode string data types". I was exporting data returned by a particular view. In order to fix the issue I had to modify the view, utilizing CAST function to cast the columns to their existing data type. Weird, huh?

Here is my original view. Please note that Field1 and Field2 are of type varchar(5) and varchar(100) respectively:


CREATE VIEW [dbo].[myView]
AS
SELECT Field1,
Field2
FROM Table1


Here is what I modified my view to and what worked for me:


CREATE VIEW [dbo].[myView]
AS
SELECT CAST(Field1 As Varchar(50)) as Field1,
CAST (Field2 as Varchar(100)) As Field2
FROM Table1