Question

I'm working on an ASP.NET site and have stumbled across an issue. I've been converting an ASP.NET Forum into ASP.NET 4.5. I've fixed everything and the site runs fantastic on my local dev machine. Once I upload it to my web host I get the following error.

Server Error in '/' Application.

Specified cast is not valid.

I've been trying to figure it out and I am stumped. Here is the code, SQL table and the stored procedure.

    public static int GetWebIDAndFolder(string host, out string folder)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CrzyForumConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("CWF_GetWebIDAndFolder", conn);

        host = host.Replace("www.", "");

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@WebDomain", SqlDbType.NVarChar, 50);
        cmd.Parameters.Add("@WebID", SqlDbType.Int, 4);
        cmd.Parameters.Add("@Folder", SqlDbType.NVarChar, 50);
        cmd.Parameters[0].Value = host;
        cmd.Parameters[1].Direction = ParameterDirection.Output;
        cmd.Parameters[2].Direction = ParameterDirection.Output;

        conn.Open();
        cmd.ExecuteNonQuery();


        int webID = (int)cmd.Parameters[1].Value;

        if (Convert.IsDBNull(cmd.Parameters[2].Value))
            folder = string.Empty;
        else
            folder = (string)cmd.Parameters[2].Value;

        conn.Close();

        return webID;
    }

SQL Table:

CREATE TABLE [dbo].[CWF_Webs] (
[WebDomain] NVARCHAR (50) COLLATE Latin1_General_CI_AS NOT NULL,
[WebID]     INT           NOT NULL,
[Folder]    NVARCHAR (50) COLLATE Latin1_General_CI_AS NULL
);

Stored Procedure

CREATE PROCEDURE [indie_world].CWF_GetWebIDAndFolder
(
@WebDomain  nvarchar(50),
@WebID      int OUTPUT,
@Folder     nvarchar(50) OUTPUT
)
AS

SELECT @WebID = (SELECT WebID FROM CWF_Webs WHERE WebDomain = @WebDomain)
SELECT @Folder = (SELECT Folder FROM CWF_Webs WHERE WebDomain = @WebDomain)

Any and all help will be appreciated. The code, table, and procedure are all from The project is from the following url. http://www.codeproject.com/Articles/4291/Riverside-Internet-Forums. It is the application im converting to base a simple forum off of and start expanding it.

Was it helpful?

Solution 3

I figured it out after a day of messing around with it. Thanks guys for all the great feed back. The issue was the host in the database table. I had added http:// to it were it was just looking for url.com not http://url.com.

OTHER TIPS

I would change the parameters assigment for using keys instead indexes

cmd.Parameters["@WebDomain"].Value = host;
cmd.Parameters["@WebID"].Direction = ParameterDirection.Output;
cmd.Parameters["@Folder"].Direction = ParameterDirection.Output;

and the use the key to get the value

int webID = cmd.Parameters["@WebID"].Value as int;
if(webID==0){
// something is wrong with your query
}

I see now that the params were output, I see that you are SELECTING the values into the parameters. Whenever I have used OUTPUT parameters that also have a RESULTSET (i.e. SELECT statement) I have to close the reader first.

Have you tried one of the following?

Change the SPROC:

SET @WebID = (SELECT WebID FROM CWF_Webs WHERE WebDomain = @WebDomain)
SET @Folder = (SELECT Folder FROM CWF_Webs WHERE WebDomain = @WebDomain)

Or don't change the SQL and use a DataReader and then close it after you finish Reading():

using( System.Data.Common.DbDataReader rdr = cmd.ExecuteReader() )
{
   while(rdr.Read())
   {
   }
}
int webID = (int)cmd.Parameters[1].Value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top