Windows Develop Bookmark and Share   
 index > Windows Forms General > To get values of Reader
 

To get values of Reader

Hi,

I have an event and on that, I put Reader column values to the TextField but I do have the problem - 'Specified cast if not valid' when running it!

Here are part of my codes:

...

try

{

conn.Open();

OleDbCommand comm = new OleDbCommand("SELECT * FROM bk_info where id in(select max(id) from bk_info)", conn);

OleDbDataReader reader = comm.ExecuteReader();

reader.Read();

this.txtroom_no.Text = reader.GetString(1);

this.txtuser_nm.Text = reader.GetString(2);

this.txtchoose_dt.Text = reader.GetString(3);

...

HuaMin Chen  Wednesday, April 02, 2008 6:42 AM

Hi,

I think, there is some data mismatch when you retrieve data by using reader.GetString();

reader.GetString() can only be used if original data type in database is string related.

For other data types, you canuse GetBoolean(), GetChars(), etcor simply use GetValue().ToString()

Hope this may help.

wirol  Wednesday, April 02, 2008 8:20 AM

try following codes

this.txtroom_no.Text = Convert.ToString(reader[1]) ;

this.txtuser_nm.Text = Convert.ToString(reader[2]) ;

this.txtchoose_dt.Text = Convert.ToString(reader[3]) ;

remember that return fields start from zero reader[0], reader[1], ...

Hamid.M  Friday, April 04, 2008 7:55 PM

Hi,

I think, there is some data mismatch when you retrieve data by using reader.GetString();

reader.GetString() can only be used if original data type in database is string related.

For other data types, you canuse GetBoolean(), GetChars(), etcor simply use GetValue().ToString()

Hope this may help.

wirol  Wednesday, April 02, 2008 8:20 AM

try following codes

this.txtroom_no.Text = Convert.ToString(reader[1]) ;

this.txtuser_nm.Text = Convert.ToString(reader[2]) ;

this.txtchoose_dt.Text = Convert.ToString(reader[3]) ;

remember that return fields start from zero reader[0], reader[1], ...

Hamid.M  Friday, April 04, 2008 7:55 PM

You can use google to search for other answers

Custom Search

More Threads

• GDI difference between Windonws XP and Windows Server 2003
• keeping focus on the main window
• How to make a message box button (Y/N) to do something?
• Updating StatusStrip from Usercontrol
• Individually Align DataGridView Column Headers?
• Help with setting color in ListView.SubItem
• Multithreading and ddatabase question
• TreeView display width
• any way of multiple file operations???
• Scrollbar locks with styles enabled, but is fine in classic mode