Click here to Skip to main content
14,882,706 members
Please Sign up or sign in to vote.
1.00/5 (1 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

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
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.
   
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900