Specified cast is not valid.

Posts   
 
    
Posts: 97
Joined: 29-Apr-2009
# Posted on: 22-May-2009 05:53:52   

hi all,

i am getting error like this : Specified cast is not valid.

on this line :

int maxIntNodeOrder = (MaxOrder == null) ? 0 : (int)MaxOrder;

my full code is as below :


IPredicateExpression filter = new PredicateExpression(CmsContentdetailsFields.IntNodeParentId == Convert.ToInt32(0)
                                                                  & CmsContentdetailsFields.FlgDeleted == 0);

                    object MaxOrder = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeOrder,
                            null, AggregateFunction.Max, filter);

                    int maxIntNodeOrder = (MaxOrder == null) ? 0 : (int)MaxOrder;
                    IPredicateExpression MaxNodefilter = new PredicateExpression();

                    object MaxNode = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeId,
                            null, AggregateFunction.Count, MaxNodefilter);

                    int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

                    CmsContentdetailsEntity cmsTree = new CmsContentdetailsEntity();
                    cmsTree.TxtNodeName = txtLinkName.Text.Trim().Replace("'", "''");
                    cmsTree.TxtDefaultUrl = AppData.GetRedirectURL(txtLinkName.Text.Trim());
                    cmsTree.IntNodeParentId = Convert.ToInt32(drpCategory.SelectedValue);
                    cmsTree.TxtCreatedBy = "admin";
                    cmsTree.IntNodeOrder = maxIntNodeOrder + 1;
                    cmsTree.FlgDeleted = 0;
                    cmsTree.FlgPublish = 1;
                    cmsTree.FlgActive = 1;
                    cmsTree.IntNodeId = maxIntNodeID + 1;
                    string temp = txtPageName.Text;
                    temp = temp.Replace(' ', '_');
                    cmsTree.TxtPageName = temp.ToString();
                    cmsTree.Save();

can you please help me

i am using mysql database

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-May-2009 09:17:04   

What's the LLBLGen Pro runitme library version used? What's the .NET type of the following field: CmsContentdetailsFieldIndex.IntNodeOrder ?

Please post the stack trace.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 22-May-2009 11:00:49   

Walaa wrote:

What's the LLBLGen Pro runitme library version used? What's the .NET type of the following field: CmsContentdetailsFieldIndex.IntNodeOrder ?

Please post the stack trace.

Hi walaa, i am using LLBLGen 2.6

and the type of "IntNodeOrder" is integer

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 22-May-2009 16:32:27   

Please post the stacktrace. Exceptions without stacktraces are not really helpful.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 97
Joined: 29-Apr-2009
# Posted on: 23-May-2009 05:41:42   

Otis wrote:

Please post the stacktrace. Exceptions without stacktraces are not really helpful.

Hi thanks for reply, i got message like this :

stack trace like below :

 File: d:\Working Projects\Outfit Recruitment\Outfit_Web\Admin\CategoryTreeNew.ascx.cs

Line: 397

Method: Void btnInsert_Click(System.Object, System.EventArgs) File:

Line: 0

Method: Void OnClick(System.EventArgs) File:

Line: 0

Method: Void RaisePostBackEvent(System.String) File:

Line: 0

Method: Void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(System.String) File:

Line: 0

Method: Void RaisePostBackEvent(System.Web.UI.IPostBackEventHandler, System.String) File:

Line: 0

Method: Void RaisePostBackEvent(System.Collections.Specialized.NameValueCollection) File:

Line: 0

Method: Void ProcessRequestMain(Boolean, Boolean) File:

Line: 0

Method: Void ProcessRequest(Boolean, Boolean) File:

Line: 0

Method: Void ProcessRequest() File:

Line: 0

Method: Void ProcessRequestWithNoAssert(System.Web.HttpContext) File:

Line: 0

Method: Void ProcessRequest(System.Web.HttpContext) File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\outfit_web\4e9bb276\f3df3333\App_Web_1bzmr0xv.0.cs

Line: 0

Method: Void ProcessRequest(System.Web.HttpContext) File:

Line: 0

Method: Void System.Web.HttpApplication.IExecutionStep.Execute() File:

Line: 0

Method: System.Exception ExecuteStep(IExecutionStep, Boolean ByRef) File:

Line: 0

Method: Void ResumeSteps(System.Exception) File:

Line: 0

Method: System.IAsyncResult System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext, System.AsyncCallback, System.Object) File:

Line: 0

Method: Void ProcessRequestInternal(System.Web.HttpWorkerRequest) File:

Line: 0

Method: Void ProcessRequestNoDemand(System.Web.HttpWorkerRequest) File:

Line: 0

Method: Void ProcessRequest(System.Web.HttpWorkerRequest) File:

Line: 0

Method: Void Process() File:

Line: 0

Method: Void ProcessRequest(Microsoft.VisualStudio.WebHost.Connection)

Thanks again. please help me

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 23-May-2009 10:23:40   

So the stacktrace shows that the error is here: d:\Working Projects\Outfit Recruitment\Outfit_Web\Admin\CategoryTreeNew.ascx.cs Line: 397

PLease post that line.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 97
Joined: 29-Apr-2009
# Posted on: 23-May-2009 11:09:21   

Otis wrote:

So the stacktrace shows that the error is here: d:\Working Projects\Outfit Recruitment\Outfit_Web\Admin\CategoryTreeNew.ascx.cs Line: 397

PLease post that line.

my try code is as below :

IPredicateExpression PageFilter = new PredicateExpression(); PageFilter.Add(CmsContentdetailsFields.TxtPageName == txtPageName.Text); CmsContentdetailsCollection checkValue = new CmsContentdetailsCollection(); int count = (int)checkValue.GetDbCount(PageFilter); if (count > 0) { lblAddMsg.Text = "This Page Name Already Exist"; MPEManageContent.Show(); } else { IPredicateExpression filter = new PredicateExpression(CmsContentdetailsFields.IntNodeParentId == Convert.ToInt32(0) & CmsContentdetailsFields.FlgDeleted == 0);

                object MaxOrder = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeOrder,
                        null, AggregateFunction.Max, filter);

                int maxIntNodeOrder = (MaxOrder == null) ? 0 : (int)MaxOrder;
                IPredicateExpression MaxNodefilter = new PredicateExpression();

                object MaxNode = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeId,
                        null, AggregateFunction.Count, MaxNodefilter);

                int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

                CmsContentdetailsEntity cmsTree = new CmsContentdetailsEntity();
                cmsTree.TxtNodeName = txtLinkName.Text.Trim().Replace("'", "''");
                cmsTree.TxtDefaultUrl = AppData.GetRedirectURL(txtLinkName.Text.Trim());
                cmsTree.IntNodeParentId = Convert.ToInt32(drpCategory.SelectedValue);
                cmsTree.TxtCreatedBy = "admin";
                cmsTree.IntNodeOrder = maxIntNodeOrder + 1;
                cmsTree.FlgDeleted = 0;
                cmsTree.FlgPublish = 1;
                cmsTree.FlgActive = 1;
                cmsTree.IntNodeId = maxIntNodeID + 1;
                string temp = txtPageName.Text;
                temp = temp.Replace(' ', '_');
                cmsTree.TxtPageName = temp.ToString();
                cmsTree.Save();

            }

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

**in ms sql this code works fine but in MY SQL It throws error. **

Posts: 97
Joined: 29-Apr-2009
# Posted on: 25-May-2009 08:10:48   

Otis wrote:

So the stacktrace shows that the error is here: d:\Working Projects\Outfit Recruitment\Outfit_Web\Admin\CategoryTreeNew.ascx.cs Line: 397

PLease post that line.

Please help me. i need your help

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 25-May-2009 08:59:20   

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

in ms sql this code works fine but in MY SQL It throws error.

What's the dataType of the MaxNode in both databases? And to which .NET dataType it's mapped to?

Do you have separate llblgen projects for each database?

Also the exception should explain which type can not be cated to which type....do you have such info from the exception text?

Posts: 97
Joined: 29-Apr-2009
# Posted on: 25-May-2009 12:23:15   

Walaa wrote:

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

in ms sql this code works fine but in MY SQL It throws error.

What's the dataType of the MaxNode in both databases? And to which .NET dataType it's mapped to?

Do you have separate llblgen projects for each database?

Also the exception should explain which type can not be cated to which type....do you have such info from the exception text?

There is only one database in MySql.

i have only one llblgen project.

i got only this error : specific cast is not valid.

so don't understand what i have to do??

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 25-May-2009 13:31:57   

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

in ms sql this code works fine but in MY SQL It throws error.

You said...in "MS SQL this code works fine". Would you please explain what did you mean by that?

Posts: 97
Joined: 29-Apr-2009
# Posted on: 25-May-2009 14:37:07   

Walaa wrote:

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

in ms sql this code works fine but in MY SQL It throws error.

You said...in "MS SQL this code works fine". Would you please explain what did you mean by that?

hi walaa,

before this project i was using MS SQL Database, but client change his requirment, now he is saying that i need MY SQL Database. in MS SQL Everything works file. i have also googled on this error : but cannot get answer

please show me way

Thanks

Posts: 97
Joined: 29-Apr-2009
# Posted on: 26-May-2009 04:45:54   

Walaa wrote:

but i am getting error here: int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

Error message : Specified cast is not valid.

in ms sql this code works fine but in MY SQL It throws error.

You said...in "MS SQL this code works fine". Would you please explain what did you mean by that?

When i was using MS SQL

the beloe statement works fine

int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

but in MY SQL it gives error

int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

specified cast is not valid

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-May-2009 07:54:49   

Hi Sitapara,

Please post the line 397 of the CategoryTreeNew.ascx.cs file. Also, please debug your code to see what is happening (the values of the variables, the context of the error, etc).

David Elizondo | LLBLGen Support Team
Posts: 97
Joined: 29-Apr-2009
# Posted on: 26-May-2009 08:09:23   

daelmo wrote:

Hi Sitapara,

Please post the line 397 of the CategoryTreeNew.ascx.cs file. Also, please debug your code to see what is happening (the values of the variables, the context of the error, etc).

Hi daelmo

This is line no: 397

int maxIntNodeID = (MaxNode == null) ? 0 : (int)MaxNode;

The value of **MaxNode ** i am getting 9

after that it throw error

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-May-2009 08:38:04   

That's wierd... If you say MaxNode is int and you can't cast it to int. Maybe comparing it to null

(MaxNode == null)

is the part which is giving an exception. Is this a Nullable integer?

Maybe you should send us a repro solution.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 27-May-2009 05:45:43   

Walaa wrote:

That's wierd... If you say MaxNode is int and you can't cast it to int. Maybe comparing it to null

(MaxNode == null)

is the part which is giving an exception. Is this a Nullable integer?

Maybe you should send us a repro solution.

Yes you are right Walaa, i am getting exception here.

see the my below code :


protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {

                IPredicateExpression PageFilter = new PredicateExpression();
                PageFilter.Add(CmsContentdetailsFields.TxtPageName == txtPageName.Text);
                CmsContentdetailsCollection checkValue = new CmsContentdetailsCollection();
                int count = (int)checkValue.GetDbCount(PageFilter);
                if (count > 0)
                {
                    lblAddMsg.Text = "This Page Name Already Exist";
                    MPEManageContent.Show();
                }
                else
                {
                    IPredicateExpression filter = new PredicateExpression(CmsContentdetailsFields.IntNodeParentId == Convert.ToInt32(0)
                                                                  & CmsContentdetailsFields.FlgDeleted == 0);

                    object MaxOrder = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeOrder,
                            null, AggregateFunction.Max, filter);

                    int maxIntNodeOrder = (MaxOrder == null) ? 0 : (int)MaxOrder;


                    IPredicateExpression MaxNodefilter = new PredicateExpression();

                    object MaxNode = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeId,
                            null, AggregateFunction.Count, MaxNodefilter);

                    int maxIntNodeID = (MaxNode == DBNull.Value) ? 0 : (int)MaxNode;

                    CmsContentdetailsEntity cmsTree = new CmsContentdetailsEntity();
                    cmsTree.TxtNodeName = txtLinkName.Text.Trim().Replace("'", "''");
                    cmsTree.TxtDefaultUrl = AppData.GetRedirectURL(txtLinkName.Text.Trim());
                    cmsTree.IntNodeParentId = Convert.ToInt32(drpCategory.SelectedValue);
                    cmsTree.TxtCreatedBy = "admin";
                    cmsTree.IntNodeOrder = maxIntNodeOrder + 1;
                    cmsTree.FlgDeleted = 0;
                    cmsTree.FlgPublish = 1;
                    cmsTree.FlgActive = 1;
                    cmsTree.IntNodeId = maxIntNodeID + 1;
                    string temp = txtPageName.Text;
                    temp = temp.Replace(' ', '_');
                    cmsTree.TxtPageName = temp.ToString();
                    cmsTree.Save();

                }



            }

            catch (Exception ex)
            {
                AppData.Write("Admin/CategoryTreeNew.ascx- > btnInsert_Click" + ex.Message);
            
            }
            BindData();
        }
    }


on this line :

object MaxOrder = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeOrder, null, AggregateFunction.Max, filter);

i am getting value of MaxOrder is 9

but in below line :

int maxIntNodeOrder = (MaxOrder == null) ? 0 : (int)MaxOrder;

When i debug this code, and than it gives exception when it compare value

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-May-2009 09:14:43   

So is this field MaxNode/maxOrder a Nullable integer? If not then you should not compare it to null.

Posts: 97
Joined: 29-Apr-2009
# Posted on: 27-May-2009 10:29:09   

Walaa wrote:

So is this field MaxNode/maxOrder a Nullable integer? If not then you should not compare it to null.

Ok Walaa,

thanks for your reply.

i have done something like this which is works fine.



object MaxOrder = new CmsContentdetailsCollection().GetScalar(CmsContentdetailsFieldIndex.IntNodeOrder,
                            null, AggregateFunction.Max, filter);
                    string Max = MaxOrder.ToString();
                    int maxIntNodeOrder = Convert.ToInt32(Max);

Thanks Again.