I am a newbie at ASP.net and after using sqldatasource with a listview to insert and show results from an SQL server db I want to try using the LINQ datasource since it seems to be more flexible in codebehind. My problem is this: I droped a listview control to the page and I created the Linq datasource in codebehind with vb. the issue that I am having when I ..Select d.columms name i get the error system.string does not contain a property with the name "columname".. if i ommit the column name then its works fine.. the funny part is the d.count works fine but after that i get the error.. please see my code below:
vb code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim rowsCount As Integer
Dim showSearchForm As String
showSearchForm = Request.QueryString("tab")
If showSearchForm = "1" Then
Dim db As New ASPNETDBDataContext()
Dim q = From b In db.PassengerVehiclesTables Select b.dbMake
rowsCount = q.Count
MsgBox(rowsCount)
lvMakes.DataSource = q
lvMakes.DataBind()
PnlPassengerVehiclesSearch.Visible = True
ElseIf showSearchForm = "2" Then
aspx code
<asp:Panel ID="PnlPassengerVehiclesSearch" Visible="false" runat="server">
Search Passenger Vehicles Form.....<br />
<table style="width: 100%; border-style: solid; border-width: 1px">
<tr>
<td>
<asp:ListView ID="lvMakes" runat="server">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<%#Eval("dbMake")%><br />
</ItemTemplate>
</asp:ListView>
</td>
b.dbMake needs to work so that i can use Distinct ,, ia m using asp.net version:3.5 and IIS version 7.0 .. not sure what i am missing ,, but i did try alot of approaches,,1- checked the web.config file and it seems to开发者_运维技巧 have two assemblies and two namespaces for LINQ..2- used different databinding syntaxs,,and i searched a lot for the solution.. the last one i read the person ommited the name of the column,, i thought that wasnt the best solution.. also my dbMake column is comming up in the "intellisence" .. thank you in advance for your help..
change the following
Dim q = From b In db.PassengerVehiclesTables Select b.dbMake
to
Dim q = From b In db.PassengerVehiclesTables Select New With {b.dbMake}
For further explanation see Select new keyword combination
The reason is because you're selecting dbMake
in your query so the return of this line is a collection of strings:
Dim q = From b In db.PassengerVehiclesTables Select b.dbMake
If you change it to this:
Dim q = From b In db.PassengerVehiclesTables Select b
Your Eval
will begin to function. It will query a larger data set than you need, but your binding will happen accurately.
精彩评论