Generating the Maximum Value of a Column in a database C#

Last post 09-26-2008 5:56 AM by abhishek_damle. 12 replies.

Sort Posts:

  • Generating the Maximum Value of a Column in a database C#

    09-25-2008, 6:36 AM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     i am having this problem. i want to get the maximum value of a column's record in my database but when i use this code, it gives me an error.  saying

    "The null value cannot be assigned to a member with type System.Int16 which is a non-nullable value type. ".

     

    How can i solve this problem?  i dont want to use sql to get it. 

    This is the code i wrote to get the maximum ID from the Oracleorganization database, 

     int x = synergy.OracleOrganizatations.Max(c=> c.OracleOrganizationID);

     

    help needed urgently. 

    Thanks

    Kingsley

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-25-2008, 6:55 AM

     it seems to be you are getting null value as there are no records in that table.

     

     int x =  (int?) synergy.OracleOrganizatations.Max(c=> c.OracleOrganizationID) ?? 0;

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Generating the Maximum Value of a Column in a database C#

    09-25-2008, 7:05 AM
    • Member
      634 point Member
    • abhishek_damle
    • Member since 09-17-2008, 3:23 PM
    • Mumbai, India
    • Posts 98

    Hi friend,

    First check what values you are getting. I think one of the value is null. Hence it is breaking. So before assigning it to int variable please check wheather it is convertable to int or not by using int.Tryparse() method as follows:

     

    int tempX = 0;

    int x = if(int.TryParse(synergy.OracleOrganizatations.Max(c=> c.OracleOrganizationID).ToString() , out tempX))? tempX : 0;

     

     

    This should solve your error, If it does help you PLEASE MARK IT AS ANSWER.

    thank you

    Mark it as answer if it is useful.

    Thanks,
    Abhishek Damle.

    MCTS - WEB 2.0
  • Re: Generating the Maximum Value of a Column in a database C#

    09-25-2008, 11:29 PM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     hey dude,

    ur code is giving me some errors, "invalid expression term if"  and i cant figure out how to solve it.

     

    could you please help me with your code?

     

    thanks

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-25-2008, 11:43 PM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     hey bro im still getting the same error when i use your code. Still need help. Thanks

     

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 12:34 AM

     r u tried my suggestion?

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 1:57 AM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     yes i did and i still got the same error that i had before.

    still need help

    Thanks 

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 2:31 AM

    try like this.

       var Id = (from v in db.TestTables select (int?)v.Id).Max() ?? 0;

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 4:27 AM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     i cannot write LINQ code in a class library project. That is why i need a  C# version.

     

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 4:48 AM
    • Member
      634 point Member
    • abhishek_damle
    • Member since 09-17-2008, 3:23 PM
    • Mumbai, India
    • Posts 98

    Hi,

    Please try the following

     

    int tempX = 0;

    int x = (int.TryParse(synergy.OracleOrganizatations.Max(c=> c.OracleOrganizationID).ToString() , out tempX))? tempX : 0;

    I have removed 'If'

     

    This should solve your error, If it does help you PLEASE MARK IT AS ANSWER.

    thank you

    Mark it as answer if it is useful.

    Thanks,
    Abhishek Damle.

    MCTS - WEB 2.0
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 5:31 AM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     still got the same error as before.

    do u have any other idea on how to solve this?

    Thanks 

    Kingsley

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 5:37 AM
    • Member
      14 point Member
    • mesmerize
    • Member since 07-23-2008, 5:03 AM
    • Posts 19

     i meant to say  i got my initial error which is :

    "The null value cannot be assigned to a member with type System.Int16 which is a non-nullable value type."

     i need serious help.

    Thanks 

    Kingsley

    Mark As Answer If this answer solves ur problem.
    Thanks
    Kingsley
  • Re: Generating the Maximum Value of a Column in a database C#

    09-26-2008, 5:56 AM
    Answer
    • Member
      634 point Member
    • abhishek_damle
    • Member since 09-17-2008, 3:23 PM
    • Mumbai, India
    • Posts 98

    Hi,

    I think you should post your code here because we dont know whats heppening into that MAX method. I will also require how are you taking column values.

    I think we need to implement some loop logic here to get max value from it. So provid us input (i.e Column values) and output (i.e. Max() method). So we can provide exact solution

     

    Thanks

    Mark it as answer if it is useful.

    Thanks,
    Abhishek Damle.

    MCTS - WEB 2.0
Page 1 of 1 (13 items)