1

I am writing something in VB and need to convert a SQL date (not datetime) into a string. This should be an easy convertion, however toString does not work and I cannot find anything online . The part of the code I'm working on looks like this:

    Dim incomingDate As String
    incomingDate = row.Cells(5).Text.ToString()

When the data being put in the gridview is a DateTime datatype this works fine. If it's simply Date, it gives me the following error message:

"Specified argument was out of the range of valid values. Parameter name: index"

I've also tried this work-around but it didn't work

    Dim incomingDate As String
    Dim d As New DateTime
    d = DateTime.Parse(row.Cells(5).Text)

    incomingDate = Date.Parse(row.Cells(5).Text)

Same error...

5
  • What exactly is the column type for the "date" of which you speak? Which DBMS? How are you querying for data? Commented Jul 26, 2012 at 21:30
  • The column type is 'date'. In all other tables it's datetime, however for this one they just used 'date' Commented Jul 26, 2012 at 21:37
  • 1
    Which DBMS are you using? Oracle? PostgreSQL? Commented Jul 26, 2012 at 22:01
  • 2
    Are you trying to read the value back out of a form control like a Table or Grid? The error you receive makes me thing that row.Cells(5) doesn't exist. Do you have six columns? Remember to index the cells starting with 0, not 1. Other than that, retrieving the text via the .Text property should be ok. No need to parse into a Date unless you want to change the format. Commented Jul 26, 2012 at 22:10
  • Yes, that turned out to be the problem... thanks Cory! :) Commented Jul 27, 2012 at 15:08

1 Answer 1

1

In SQL you can use convert(varchar, mySqlDate, 101) to emit a string instead of a date. Is that what you had in mind?

Sign up to request clarification or add additional context in comments.

2 Comments

I am running the code in Visual Studio, loading the query into a SqlDataAdapter object. Is this something I can add into the SQL SELECT statement?
Yes. Instead of select mySqlDate ... (which returns a SqlDate), plug this in select convert(varchar, mySqlDate, 101) ... and get back a string instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.