change text on a hyperlink in a datalist - asp.net

I'm trying to bind a column from a SQL code that i have written to all hyperlinks in a datalist. This should be really simple but I'm getting the error
DataBinding: 'System.Data.Common.DataRecordInternal' does not contain
a property with the name 'NumberOfComments'.
Well I'm pretty sure the column exist but in this case it's created by a function maybe that has something to do with it. When I run the SQL code i get the values I should.
The hyperlink
<asp:HyperLink ID="lnkComment" runat="server"
NavigateUrl='<%# Eval("ID", "~/Default.aspx?ID={0}") %>'
Text='<%# Eval("NumberOfComments") %>'></asp:HyperLink>
The SQLDataSource
<asp:SqlDataSource ID="sdsNews" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" SelectCommand="SELECT News.ID, News.Topic, News.Text, News.PostTime, aspnet_Users.UserName, "NumberOfComments" = dbo.fnNumberOfCommentOnNews(News.ID)
FROM News INNER JOIN
aspnet_Users ON News.UserId = aspnet_Users.UserId
WHERE (News.ID = ISNULL(#ID, News.ID))
ORDER BY News.PostTime DESC ">
<SelectParameters>
<asp:QueryStringParameter DbType="Guid" Name="ID" QueryStringField="ID" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>

Why do you have """ surrounding NumberOfComments FieldName?
Try pasting the select command into SSMS (SQL Server Mgmt studio) and you should most definitely get an incorrect syntax error. If you drop those quot html code you will still be able to reference and bind to that column.

Related

Gridview's UPDATE does not like the space in my parameter

Unfortunately, the database I'm dealing with has a space in the column name. I have a DropDownList in a GridView and I'm trying to update the column with whatever the user selects in the DropDownList. Here's how I have the DropDownList:
<asp:TemplateField HeaderText="Phase (edit)" SortExpression="EditedPhase">
<EditItemTemplate>
<asp:DropDownList ID="PhaseDropDownList" runat="server" DataSourceID="PhaseDropDown" DataTextField="Current Project Phase" DataValueField="Current Project Phase" SelectedValue='<%# Bind("Phase") %>'>
</asp:DropDownList>
</EditItemTemplate>
<asp:TemplateField>
Here's the data source:
<asp:SqlDataSource ID="PhaseDropDown" runat="server" ConnectionString="<%$ ConnectionStrings:ODSConnectionString %>" SelectCommand="select distinct [Current Project Phase] from [Phase_Table]">
</asp:SqlDataSource>
Here's how I have my update command and the parameter:
UpdateCommand="UPDATE [Pipeline] SET EditedPhase = #[Current Project Phase]"
<UpdateParameters>
<asp:Parameter Name="[Current Project Phase]" Type="String"/>
</UpdateParameters>
After doing a little bit of research, I discovered that you can not have spaces in the parameter, but most of the solutions used the code behind. I have no code behind because introducing code behind could potentially break it (needing to deal with page loads and such). How do I fix my current issue?
If there's a typo, sorry.
Yes, do not use spaces in your parameter name; the value passed to the parameter can have spaces though, so I'm not sure if that is a point of confusion. For instance, update your SQL query like:
UpdateCommand="UPDATE [Pipeline] SET EditedPhase = #CurrentProjectPhase">
<UpdateParameters>
<asp:Parameter Name="CurrentProjectPhase" Type="String" DefaultValue="Current Project Phase"/>
</UpdateParameters>
Here the value (the DefaultValue is a default to supply to the update when no value is provided) can have spaces just fine. But the name cannot. You can set the DefaultValue of the parameter in code or use a more capable parameter (like session, control parameters) to grab values from something...

asp:sqldatasource SelectCommandType="Text" Dropdown not populating ONLY when parameters are passed to SQL

I'm trying to use a asp:SqlDataSource driven by a SQL Select with Arguments. It works as long as I don't have any arguments. I can run the parameterized query via studio and it works and returns rows. If I any arguments, then the asp:View doesn't render.
First, the code returns 2 rows when I execute the SQL via Studio.
Second, I don't want it as a stored proc. Deployment issue. Smile, let it go. :-)
<asp:DropDownList ID="lstUsers" runat="server" DataSourceID="sqlGetCSGUsersOnClaim" DataTextField="username" DataValueField="userID" />
<asp:SqlDataSource ID="sqlGetCSGUsersOnClaim" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer%>" SelectCommandType="Text" SelectCommand="
SELECT aspnet_Users.UserId as userID, aspnet_users.username as username
FROM claims, aspnet_Users
WHERE claims.claimid = #ClaimID and
(Claims.AdjusterID = aspnet_Users.UserId or Claims.SupervisorID = aspnet_Users.UserId )">
<SelectParameters>
<asp:SessionParameter Name="ClaimID" SessionField="ClaimID" Type="Int32" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the #ClaimID and SelectParameters arguments, the dropdown populates. It only fails once I add the parameter.
What am I doing wrong?
Thanks in advance,
Jason
Well it turned that if I removed the Type and DBType attributes of the SelectParameter, the code worked. sigh....
I have no idea, why though.

ASP.NET Filter Expression

Hi I'm trying to get a filter expression working on my content column located in BLOG table on my gridview.
It displays the content column fine with 50 characters but when i try click my asp button to run the filter expression i get an error saying content column not found.
Any idea why this is?
Here's my code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Assignment2ConnectionString %>" SelectCommand="SELECT [blogid], [myfriendid], [inputdate],
Left(content,50) FROM [BLOG]" filterexpression="[content] LIKE '%{0}%' or url LIKE '%{0}%'">
<filterparameters>
<asp:controlparameter controlid="TextBox1" propertyname="Text" />
</filterparameters>
</asp:SqlDataSource>
When you do Left(content,50) that the column no longer has a name
Change the to this
ConnectionString = "<%$ ConnectionStrings:Assignment2ConnectionString %>"
selectCommand "SELECT [blogid], [myfriendid], [inputdate],
Left(content,50) AS ShortContent FROM [BLOG]"
filterexpression = "[ShortContent] LIKE '%{0}%' or url LIKE '%{0}%'">

Accessing query string variable in the sql query in aspx page

I am quite new to asp.net,I am building an application where I need to show the in the grid view,Now the query I am generating for fetching the data from database containing one parameter which comes from the query string.I am using this code
<asp:SqlDataSource runat="server" ID="MySQLData"
ConnectionString="server=localhost;port=3309; User Id=xxxxx;password=xxxxx;database=xxxxx"
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT contenthead.lastmodifieddate,contenthead.heading,lk_technology.technology FROM contenthead JOIN lk_technology WHERE contenthead.techID=lk_technology.techID AND contenthead.authorid='<%=Request.QueryString["uid"]%>'" />
Now when I am using <%..%> tag I am getting parser error that says: Server tags cannot contain <% ... %> constructs.
Now I want to use this variable from query string.Please tell me how I can access this variable in this context.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." ProviderName="System.Data.SqlClient" SelectCommand="SELECT [UserId], [LastUpdatedDate] FROM [vw_aspnet_Profiles] WHERE ([UserId] = #UserId)">
<SelectParameters>
<asp:QueryString ParameterDefaultValue="0" Name="UserId" QueryStringField="Id" Type="Object"/>
</SelectParameters>
</asp:SqlDataSource>
You have to add a parameter to your SqlDataSource

Pull one record from the database and bind to an HTML tag

In ASP.NET, what is the definitive way to pull one record from the database and bind it and HTML tag? Brevity and style points count.
For brevity, you'll want to use the SqlDataSource.
<asp:SqlDataSource ID="sql" runat="server" ConnectionString='<%$ ConnectionStrings:MyConnectionString %>'
SelectCommandType="Text"
SelectCommand="select MyField FROM MyTable WHERE ID = #id"
>
<SelectParameters>
<asp:ControlParameter ControlID="txtUserName" PropertyName="Text" Name="id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:BulletedList runat="server" DataTextField="MyField" DataSourceID="sql">
</asp:BulletedList>
It's not clear what you want but, detailsview control is designed for displaying only one record from data source.
DetailsView is a data-bound user
interface control that renders a
single record at a time from its
associated data source, optionally
providing paging buttons to navigate
between records.

Resources