0

I try to get year from a column UPDATE_DATE in a SQL Server database. And I want to display it in a dropdownlist. But, I get this error:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'UPDATE_DATE'

Here is my source code: If i remove YEAR from that, it will not show the error. how actually the correct way to do this using YEAR

strsql = "SELECT DISTINCT year(UPDATE_DATE) FROM [FixedAssetMaster_old]  "

da = New SqlDataAdapter(strsql, Conn)
Dim ds2 As New DataSet()
da.Fill(ds2)
da.Dispose()

ddlFinYear.DataSource = ds2
ddlFinYear.DataTextField = "UPDATE_DATE"
ddlFinYear.DataValueField = "UPDATE_DATE"
ddlFinYear.DataBind()

ddlDept.Items.Insert(0, "")
ds2.Dispose()

Can anyone give me some advice? Thanks in advance

1
  • I think I would add an order by clause in the Select statement, so the years would be in some sensible order for the user to choose from. Commented Jul 16, 2018 at 7:22

2 Answers 2

2

A DataSet can have many tables in it. This code only creates one, but there was no way for the DataSource property on a DropdownList to know that. Use ds2.Tables(0) for the data source so it will know which table to look at.

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

1 Comment

Or use a DataTable instead of a DataSet from the beginning, if only one DataTable is being fetched anyway .....
0
strsql = "SELECT DISTINCT year(UPDATE_DATE) as ABC FROM [FixedAssetMaster_old]  "

da = New SqlDataAdapter(strsql, Conn)
Dim ds2 As New DataSet()
da.Fill(ds2)
da.Dispose()

ddlFinYear.DataSource = ds2
ddlFinYear.DataTextField = "ABC"
ddlFinYear.DataValueField = "ABC"
ddlFinYear.DataBind()

ddlDept.Items.Insert(0, "")
ds2.Dispose()

Comments

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.