473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SqlDataReader......

I am reading the data from database using SqlDataReader.
Here is my code.....

txtCustomerName =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerName"))
txtCustomerDOB =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerDOB"))

I am getting the "cast" error at the second line.
the reason for this is "CustomerDOB" is a datetime field in SQL server
dataabase.
how to solve this ? How to read different datatype into Text box. I have
smallint, tinyint in my database.
Thanks for your answer.
Smith


Nov 18 '05 #1
3 2091
You could also say MyDataReader("CustomerName").ToString() to put the data in the TextBox.

For items that are DateTime, and you want them in a specific format, you can use Convert.ToDateTime(MyDataReader("CustomerDOB").ToS hortDateString()
Nov 18 '05 #2


different datatype into Text box:::
hi,
i feel this will help
u can explicitly get whatever value u want from DB......

MyDataReader.GetDateTime();
--
Thanks and Regards,

Amit Agarwal
Software Programmer(.NET)
"Smith Simson" <Sm*******@yahoo.com> wrote in message
news:Oo**************@TK2MSFTNGP12.phx.gbl...
I am reading the data from database using SqlDataReader.
Here is my code.....

txtCustomerName =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerName"))
txtCustomerDOB =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerDOB"))

I am getting the "cast" error at the second line.
the reason for this is "CustomerDOB" is a datetime field in SQL server
dataabase.
how to solve this ? How to read different datatype into Text box. I have
smallint, tinyint in my database.
Thanks for your answer.
Smith

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.580 / Virus Database: 367 - Release Date: 2/6/2004
Nov 18 '05 #3
You need to separate out some things here. The textbox needs a string, and
it needs it assigned not to the textbox itself, but to its Text property.
The database has a date. So, to fetch the date from the database, you need
to use the GetDateTime() method of the DataReader. Then you need to convert
the data returned to a string, using ToString() or Convert.ToString():

txtCustomerName.Text =
MyDataReader.GetDateTime(MyDataReader.GetOrdinal(" CustomerName")).ToString()

txtCustomerName.Text =
Convert.ToString(MyDataReader.GetDateTime(MyDataRe ader.GetOrdinal("CustomerN
ame")))

Hint: Turn ON Option Strict!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Smith Simson" <Sm*******@yahoo.com> wrote in message
news:Oo**************@TK2MSFTNGP12.phx.gbl...
I am reading the data from database using SqlDataReader.
Here is my code.....

txtCustomerName =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerName"))
txtCustomerDOB =
MyDataReader.GetString(MyDataReader.GetOrdinal("Cu stomerDOB"))

I am getting the "cast" error at the second line.
the reason for this is "CustomerDOB" is a datetime field in SQL server
dataabase.
how to solve this ? How to read different datatype into Text box. I have
smallint, tinyint in my database.
Thanks for your answer.
Smith

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
1
by: me | last post by:
Why is SqlDataReader not present in visual studios Toolbox->Data where I find components like DataSet? Most examples in books and in vs.net are based on datasets but using disconnected tables seems...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
7
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.