Hi I'm currently trying to perform a search using my SQLDataSource but want to use the WHERE statement to find the GUID that I have already assigned to a Label, however I'm getting the error "Conversion failed when converting from a character string to uniqueidentifier".
Ive been reading through forums just like this for days now and I'm just getting myself into all sorts of mess with code, Ive tried using C# in the codebehind based on SqlSelecting and buttons to populate the ListView that Im working with, ListView_Selecting etc etc
I'm pretty new to asp.net and C# so maybe I've not understood some of the forums Ive been on, either way I'd appreciate any and all help anyone can give, I've included the code below.
Thanks in advance.
(NESTED WITHIN ANOTHER LISTVIEW)
<asp:Label ID="CommId" Text='<%# Eval("CommentId") %>' runat="server" Visible="false"></asp:Label>
<asp:ListView ID="viewComments1" DataSourceID="AddCommentsSource" runat="server">
<LayoutTemplate> <span ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("Comment") %>/>
<asp:Label ID="CommentDateLabel" runat="server" Text='<%# Eval("CommentDate") %>' />
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="AddCommentsSource" runat="server"
ConnectionString="<%$ ConnectionStrings:SecurityTutorialsConnectionString %>"
SelectCommand="SELECT myComment.Comment, myComment.CommentDate, aspnet_Users.UserName FROM aspnet_Users, myComment, GuestbookComments
WHERE myComment.CommentId = 'CommId' AND aspnet_Users.UserId = myComment.UserId
ORDER BY myComment.CommentDate DESC" >
</asp:SqlDataSource>
I'd suggest using code-behind to dynamically issue your query based on your parameters, also use named parameters for convenience and safety. As for converting a string to a GUID, then it's fairly simple:
Guid yourGuid = new Guid(yourString);
Related
I am trying to access a session variable in aspx but I think there is an issue with my syntax somewhere.
Under page load:
Session["UserName"]= username.Substring(8).ToString(); // This is ok as far as I know.
Issue is with the asp bit:
<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" />
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" />
I have also tried the above (one at a time) with .ToString() at the end too. But I keep getting errors:
Either The server tag is not well formed or the server tag contains % %
One thing that might make a difference is the Textbox is inside a GridView, (it is inside a ContentTemplate)but not a boundfield.
Try this:
<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' />
Not sure if it would help..
I have a ASP Gridview that has a column with a button that opens a popover. Inside the popover I want to populate an ASP Table or ASP Gridview to show hidden related data. I am able to open the popover and it displays hard coded data. But when I want to make the data result dynamic using the Eval() tag the syntax is not able to read the text="<%# Eval('Label1') %>" tags within the string within the object.
The purpose is to load the entire data set and the popover data on one page load. I am wanting to avoid reload if possible. It seems that it cannot handle the <% within the string, is there a way to handle for this?
Any advice is welcome. Thank you in advance.
Sample code:
<asp:Button ID="Button3"
runat="server"
class="btn btn-info btn-xs"
Text="+"
OnClientClick="return false;"
data-toggle="popover"
data-trigger="focus"
TabIndex="0"
data-placement="right"
title="Owner Change"
UseSubmitBehavior="true"
data-content='<asp:Gridview runat="server" ID="gdvTest">
<Columns>
<asp:TemplateField HeaderText="Label One">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label1') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Label Two">
<asp:ItemTemplate>
<asp:Label runat="server" text="<%# Eval('Label2') %>" />
</asp:ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>
/>
Update: I also attempted to use ASP Table, but my class is not recognizing the ID im trying to use to dynamically populate the Label.
...data-content="<asp:Table><asp:TableRow><asp:TableHeaderCell text='TestHeader'><asp:Label runat='server' ID='lblTest' style='color:Red'></asp:TableHeaderCell></asp:TableRow></asp:Table></asp:Gridview>"
Your syntax is a bit off indeed. Remember that everything inside <%# %> should be a valid C#, so 'Label2' is an invalid literal. To fix that, swap quotes over:
text='<%# Eval("Label1") %>'
Update.
Missed the fact that GridView is itself surrounded by single quotes of data-content attribute. Even though this markup looks quite weird, there is a way to make it work, using double quotes everywhere and escaping appropriately:
text="<%# Eval("Label1") %>"
I'm developing a website. I have an Access database that I pull image names from that i want to use as parameters to post images from my folder using C# code in VS 2010. I want to know how to access the image name as a string and insert it into my asp image element. I'm guessing I need to store the results of my "Select" query in a variable in order to pass it through the "Eval" function, but I'm just guessing and nor have I figured out how to do it.
The following section of code is where I established a connection with the database, used the Datalist control to list the information on my webpage that this code is for. And so what I'm trying to ultimately do is use the file name that's in the database as a parameter to grab the correct image from my files and post it to my web page. See below in code where I say FILENAME GOES HERE????? to see where I need to put my filename for my picture.
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT * FROM [Table2]"></asp:AccessDataSource>
<asp:DataList ID="DataList1" DataSourceID="AccessDataSource2"
ItemStyle-VerticalAlign="Middle" runat="server" DataKeyField="ID">
<ItemStyle VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Image:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PlaceImages/" Eval(*FILENAME GOES HERE?????*) %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
try this
<asp:Image ID="Image2" runat="server" ImageUrl='<%# "PlaceImages/" + Eval("Your_ImageName_Column")%>' />
Morning all I have frequently used the old
<asp:Label ID="lblWas" runat="server" Text='<%# XPath("FACEVALUE") %>'></asp:Label>
This type of thing. when I first came across it I loved it, i'm using it again today but not quite so simply.
I have a number of extra things I would like to achieve.
Apply formatting to the value. Like Text='<%# string.Format(XPath("FACEVALUE"), "{0:c}") %>'>
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/' + XPath("FACEVALUE")>Book</asp:LinkButton>
For option number 2 the URL is not as I would expect, and for number 1 I cannot get the syntax correct if it's even possible.
I have not been able to find something suitable in google. Hopefully what I am trying to achieve is obvious from the example :)
You can use the TemplateControl.XPath(string xPathExpression, string format) override:
<asp:Label Text='<%# XPath("FACEVALUE", "{0:c}") %>' />
<asp:LinkButton Text="..." PostBackUrl='<%# XPath("FACEVALUE", "/THEATRE/{0}") %>' />
As you can see, you do not need to use string.Format because you can pass the format directly into the XPath method!
I believe for #1, you have messed up the syntax, you want to use
Text='<%# string.Format("{0:c}", XPath("FACEVALUE")) %>'
or Text='<%# XPath("FACEVALUE", "{0:c}") %>'
For #2, you need to use data binding expressions
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='<%# "/THEATRE/" + XPath("FACEVALUE")%>'>Book</asp:LinkButton>
For the first thing it must be Text='<%# string.Format( "{0:c}",XPath("FACEVALUE")) %>'>
and for the second one it should be
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/ + <%# XPath("FACEVALUE") %>'>Book</asp:LinkButton>
i have a webpage built in asp.net c#. it always a user to create a new record in a db table. there are there are two input fields, text and score. text cannot be a null value so if the user doesn't input text onsubmit, the page errors out. i want to throw in some simple error handling code in the code behind page. i've tried including an if/else on_inserted method but ran into some java script errors. any help would be apprieciated. thanks.
aspx page -----------------
<EditItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<customEditors:EditorWithCustomButtons_1 runat="server" ID="Editor1" Content='<%# Bind("userText") %>' />
</InsertItemTemplate>
why not use RequiredFieldValidator validator? it works inside the grid
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox1"
Display="Static"
ErrorMessage="*"
runat="server"/>
Also don't forget to use page is valid before saving to the database.
if (page.isValid){
//send to db
}