SqlDataReader......

S

Smith Simson

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

txtCustomerName =
MyDataReader.GetString(MyDataReader.GetOrdinal("CustomerName"))
txtCustomerDOB =
MyDataReader.GetString(MyDataReader.GetOrdinal("CustomerDOB"))

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
 
G

Guest

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").ToShortDateString()
 
N

.NET Follower

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

MyDataReader.GetDateTime();
 
K

Kevin Spencer

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(MyDataReader.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top