Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please someone tell that how to solve this error my code is:
C#
while (dr.Read())
{
      adcode = dr[0];
}
dr.Close();

it is throwing object to int error, please someone tell.
Posted
Updated 23-Apr-11 3:29am
v2

Refer below link,

[^]

I think your adcode field is int so try this,

XML
while (dr.Read())
                              {
                                    adcode = (int)dr[0];
                              }
                              dr.Close();

If it is String then,
adcode = (string)dr[0];
 
Share this answer
 
v3
Hi,

As dr[0] returns a value of object type, it throws error. You need to convert it into integer. Try the following code.

adcode  = Convert.ToInt32(dr[0]);


or

int ord = dr.GetOrdinal("FieldName");
adcode = dr.GetSqlInt32(ord).Value;
 
Share this answer
 
scmd=new SqlCommand("select * from AdminUser",scon);
            scon.Open();

        sdr = scmd.ExecuteReader(CommandBehavior.KeyInfo);
        sdr.Read();
        dt = sdr.GetSchemaTable();
        scon.Close();


Hope This can help you.
 
Share this answer
 

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