how to assign a value of a field to a variable?? (c#)

  • Thread starter Thread starter Fanor
  • Start date Start date
F

Fanor

Hi all,
I know this is a simple question for most of u guys.
Actually i want to assign a value of a a field to a variable.

when i do this
k= dr["myfield"]

I got an error message because dr a row object and k a variable.

TIA
 
You'll need to cast the value to myField to whatever k is.

Assuming the declaration string k;

k = dr["myField"].ToString();
k = (string)dr["myField"]; //assumes tht the avlue in the row is
convertible.

You may confront a similar problem with datareader and you can use similar
mechanisms or it's GetString[Index]; method for instance.

HTH,

Bill
--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
Actually k is integer and dr["myfield"] as well, is not there another
direct way to use the field value???? or should I do something like

k=Convert.Int64(dr["myField'].tostring())



William Ryan eMVP said:
You'll need to cast the value to myField to whatever k is.

Assuming the declaration string k;

k = dr["myField"].ToString();
k = (string)dr["myField"]; //assumes tht the avlue in the row is
convertible.

You may confront a similar problem with datareader and you can use similar
mechanisms or it's GetString[Index]; method for instance.

HTH,

Bill
--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
Fanor said:
Hi all,
I know this is a simple question for most of u guys.
Actually i want to assign a value of a a field to a variable.

when i do this
k= dr["myfield"]

I got an error message because dr a row object and k a variable.

TIA
 
Any cast to the same type will work.

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
Fanor said:
Actually k is integer and dr["myfield"] as well, is not there another
direct way to use the field value???? or should I do something like

k=Convert.Int64(dr["myField'].tostring())



William Ryan eMVP said:
You'll need to cast the value to myField to whatever k is.

Assuming the declaration string k;

k = dr["myField"].ToString();
k = (string)dr["myField"]; //assumes tht the avlue in the row is
convertible.

You may confront a similar problem with datareader and you can use similar
mechanisms or it's GetString[Index]; method for instance.

HTH,

Bill
--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
Fanor said:
Hi all,
I know this is a simple question for most of u guys.
Actually i want to assign a value of a a field to a variable.

when i do this
k= dr["myfield"]

I got an error message because dr a row object and k a variable.

TIA
 
Back
Top