Click here to Skip to main content
14,175,754 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
Rate this: bad
 
good
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: bad
 
good
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: bad
 
good
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190525.1 | Last Updated 12 Jul 2011
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