Posted by: Draško Sarić | June 25, 2007

Cannot convert from ‘ref int’ to ‘ref int?’

Recently, I kept getting above mentioned error. My code looked like this:

protected void btnUploadSubmit_Click(object sender, EventArgs e)
/* here goes code for uploading Excel file and filling DataTable called excelTable*/ BackEndTableAdapters.RegistrantsTableAdapter reg = new BackEndTableAdapters.RegistrantsTableAdapter();

foreach(DataRaow excelRow in excelTable.Rows)
int ID=0; // source of evil
string firstName = excelRow[“First Name”].ToString();
string lastName = excelRow[“Last Name”].ToString();
/* … */
reg.AddRegistrant(firstName, lastName,……, ref ID); /*this is the line that generates error; ID is output parameter of stored procedure */
/* … */

What was wrong with this code? Well, last argument of AddRegistrant function was nullable int (int?) and not int. When I changed line

int ID=0;


int? ID=0;

everything went well..

Make money with this.:)



  1. The main thing i’m enjoying while reading your blog is the way you write, you are a really charismatic person and your posts are wonderful, keep it up!

  2. This was driving me insane! Thanks for posting!

  3. hi this is vadluri sreenu,
    you should delcare ID like this.
    int? ID;
    insted of int ID;
    it solves your problem.

  4. This was driving me insane also…

    Thanks for finding this *NOT* well documented tidbit.

  5. Hello webmaster
    I would like to share with you a link to your site
    write me here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: