Click here to Skip to main content
14,106,748 members
Rate this:
 
Please Sign up or sign in to vote.
hi all,

i got the given error, please do favour for me...

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

Source Error:

Line 256: string verifydest_result = null;
Line 257: verifydest_result = "";
Line 258: verifydest_result = verifyAllocDest(TextBoxDest.Text.Trim());
Line 259: if(verifydest_result=="1")
Line 260: {

my code is :
string verifydest_result = null;
            verifydest_result = "";
            verifydest_result = verifyAllocDest(TextBoxDest.Text.Trim()).ToString();
            if(verifydest_result=="1")
            {
                int verifycount_result=0






public object verifyAllocDest(string destination)
    {
        string result = null;
        try
        {
            SqlConnection con = new SqlConnection(sqlconn_cmsstr);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand("bms_destinationvalidate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Accountnumber", Session["User_id"]);
            cmd.Parameters.AddWithValue("@DestNo", destination);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                result = "0";
                return result;
            }
            else
            {
                result = "1";
                return result;
            }
        }
        catch (Exception ex)
        {
            result = "-1";
            return result;
        }
    };
Posted
Updated 28-Sep-12 4:49am
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The problem is here:
public object verifyAllocDest(string destination)
For a quick fix (very dirty!) replace object by string.
A better solution involves an enum: you return one of three possible values.
Hence define
public enum AllocDestResult
{
    HasRows,
    HasNoRows,
    Exception
}

and use that enum instead of a string.
   
Comments
fjdiewornncalwe 28-Sep-12 10:48am
   
You got it too. +5. Same answer I gave.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

public object verifyAllocDest(string destination)

needs to be
public string verifyAllocDest(string destination)

or
verifydest_result = verifyAllocDest(TextBoxDest.Text.Trim()).ToString();
   
v2
Comments
stellus 28-Sep-12 11:02am
   
thank you now its working fine...
ridoy 28-Sep-12 12:14pm
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Why return an object at all, when the return is always going to be a string?

Something more along the lines of...

public string VerifyAllocDest(string destination)
{
    string result = null;
    try
    {
        using (var con = new SqlConnection(sqlconn_cmsstr))
        {
            con.Open();
            var cmd = new SqlCommand("bms_destinationvalidate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Accountnumber", Session["User_id"]);
            cmd.Parameters.AddWithValue("@DestNo", destination);

            using (var dr = cmd.ExecuteReader())
            {
                if (dr.HasRows)
                {
                    result = "0";
                }
                else
                {
                    result = "1";
                }
            }
        }
    }
    catch (Exception ex)
    {
        result = "-1";
    }
    return result;
};
   
Comments
fjdiewornncalwe 28-Sep-12 10:48am
   
Same answer I have for him. +5

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