Click here to Skip to main content
14,545,626 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am calling a GetSerialNo function But it showing some error like:

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

Can anyone help me to solve this issue.

Here is the code:

int slNo= GetSerailNo(keydata);



private int GetSerailNo(String keydata)
	{
        
        SqlConnection con = new SqlConnection(@"server=Servername;database=DBNAME;uid=Username;pwd=Pwd;max pool size=250;Connect Timeout=0");
        con.Open();
		cmd = new SqlCommand("select isnull(max(slno)+1,1) from d001docs where source_keydata='" + keydata + "'", con);
		dynamic no = cmd.ExecuteScalar();
		cmd.Dispose();
		con.Close();
		return no;
	}</pre>

Thanks in advance
Posted
Updated 2-Jan-20 2:44am
Rate this:
Please Sign up or sign in to vote.

Solution 2

no is decimal while your function return value is int, hence the error (decimal datatype is wider than int) you may either:
  • Change your function (and your slNo variable) data type to decimal.

or
  • Explicitely cast no to int:
    return (int) no;

    (warning: use this technique only if you are sure the values will always fit into int range).
   
Comments
Sergey Alexandrovich Kryukov 12-Jul-11 12:50pm
   
OP needs to go back to basics. My 5.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

It means what it says. Your code is passing a decimal where an int is expected. You need to explicitly cast it, with Convert.ToInt(), I guess. I assume the issue is your return type, why not just change it ? Also, buy a C# book and read it, the error messages don't get any clearer than this one.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

dynamic no = cmd.ExecuteScalar();

u need to cast explicitly the no in integer,
   

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




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