SqlDataReader......

  • Thread starter Thread starter Smith Simson
  • Start date Start date
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
 
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()
 
different datatype into Text box:::
hi,
i feel this will help
u can explicitly get whatever value u want from DB......

MyDataReader.GetDateTime();
 
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.
 
Back
Top