Click here to Skip to main content
14,825,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :
C#
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 9-Oct-19 11:42am
v4

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
C#
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.
C#
public object verifyAllocDest(string destination)

needs to be
C#
public string verifyAllocDest(string destination)

or
C#
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
Why return an object at all, when the return is always going to be a string?

Something more along the lines of...

C#
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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900