Click here to Skip to main content
14,093,107 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi all,

am new in c#, i got the given error,

Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

Source Error:

Line 262: int verifycount_result=0;
Line 263:
Line 264: verifycount_result = Destinations_Alloc_Count();
Line 265:
Line 266: if(verifycount_result=="1")

my code is:

int verifycount_result=0;

    verifycount_result = Destinations_Alloc_Count();



    public object Destinations_Alloc_Count()
        string result = null;

        result = "";
            SqlConnection con = new SqlConnection(sqlconn_cmsstr);
            if (con.State == ConnectionState.Closed)
            SqlCommand cmd = new SqlCommand("select count(divertedto) from Hotdial where accountnumber='" + Session["user_id"] + "'", con);
            int i = 0;
            i = 0;
            i = int.Parse(cmd.ExecuteScalar());
            if (i > 200)
                result = "0";
                return result;
                result = "1";
                return result;

        catch (Exception ex)
            result = "-1";
            return result;
Updated 28-Sep-12 5:37am
ridoy 28-Sep-12 12:11pm
your function must need a return type to catch it's value

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is similar to your other question

Your function is returning an object. Within the function, you are returning a string. When you are calling the function, you are assigning it to an int!

1) Change your function to return type string.

public string Destinations_Alloc_Count()

2) Change your variable to be a string type

string verifycount_result = string.Empty;
verifycount_result = Destinations_Alloc_Count();

Try and keep your types the same, or you will have issues like this
stellus 28-Sep-12 11:24am
thank you so much
Stephen Hewison 28-Sep-12 11:38am
fjdiewornncalwe 28-Sep-12 12:08pm
+5. You'd think he would have put 2 and 2 together to get 4 after his last question, but alas it appears he just copied/pasted the solutions from that question without really thinking about what he was changing and why. We could probably use this guy as a perfect example of why people don't tend to learn anything if we just give code answers for homework and such.
ridoy 28-Sep-12 12:10pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100