Gridview Error :A call to Bind was not well formatted - c#

I have following code for gridview :
<asp:TemplateField HeaderText="N/A">
<ItemTemplate>
<asp:Label ID="lblENGNA" runat="server" Text='<%# Bind("N/A") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
When i run this its giving me error for above section that:
Parser Error Message: A call to Bind was not well formatted. Please
refer to documentation for the correct parameters to Bind.
Error Image:
Note: my query(which binds grid) contains field N/A

I added [N/A] in bind and it worked.
Its as follows:
<asp:Label ID="lblENGNA" runat="server" Text='<%# Bind("[N/A]") %>'>

Related

TextBox with TextMode="Date" does not show data

I have a Gridview, with a DateTime column defined like this:
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="txtDate" Text='<%# Item.Date.ToShortDateString() %>' runat="server" TextMode="Date"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Item.Date.ToShortDateString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The date gets displayed in the grid, and it gets saved correctly when I edit a row from the grid. The only problem is that "txtDate" doesn't display the current value when entering in "edit mode" of a row. It displays "mm/dd/yyyy" instead of the actual value coming from the bind. And I don't know why. Nonetheless, when I remove the property TextMode="Date", the actual value displays correctly.
Any ideas? Thank you!
This occurs in Google Chrome when the browser can't parse the given date. You should be getting a warning about that in the browser's console as well. Google Chrome expects the format to be yyyy-MM-dd, so e.g.:
Item.Date.ToString("yyyy-MM-dd");

aspx bind data with '#' symbol to gridview

Been trying to fix this for hours and I know that the problem is with the '#' but I could not find any solution to this problem. My field name in the database is 'HRTRN#'.
<asp:TemplateField HeaderText="Transaction#">
<EditItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("HRTRN#") %>' Width="50px" Height="17px" MaxLength="14"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("HRTRN#") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Does anyone know the solution for this to make the field name containing # symbol accessible?
You should change your code from :
Bind("HRTRN#")
To
Bind("[HRTRN#]")
Since it contains special charcters. ( you would do that also for columns with spaces).

can I have a return value from Eval() method in a databound Gridview?

Please refer to the code below. The template field is a part of a gridview. I have a requirement where I want to pass the string from Boundfield "TriggerEvent" to a method "Alert()" that should do some operation on the string and display it back in the grid. I have encountered error in this which is explainable. How do I achieve this functionality?
<asp:TemplateField HeaderText="TriggerEvent" SortExpression="TriggerEvent" ItemStyle- Wrap="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Alert(Eval("TriggerEvent")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
As Eval is returning object and Alert is expecting string, you can cast is with as:
Text='<%# Alert(Eval("TriggerEvent") as string) %>'

Binding binary field to ASP.NET DataGrid Causes Error

I have a dataset with one of it's fields named "Reviewed".
This dataset is popualted and below picture show it's data:
Now Im trying to show this dataset inside a DataGrid :
<ASP:TemplateColumn HeaderText="Reviewed" HeaderStyle-HorizontalAlign="center" HeaderStyle-Wrap="True">
<ItemStyle Wrap="false" HorizontalAlign="center" />
<ItemTemplate>
<ASP:Checkbox Checked='<%# DataBinder.Eval(Container.DataItem, "Reviewed") %>' runat="server" ID="Label22" />
</ItemTemplate>
<EditItemTemplate>
<Asp:Checkbox id="Textbox5" width="40" Checked='<%# DataBinder.Eval(Container.DataItem, "Reviewed") %>' runat="server" />
</EditItemTemplate>
</ASP:TemplateColumn>
I get the following Error:
Exception Details: System.InvalidCastException: Cast from type
'DBNull' to type 'Boolean' is not valid.
What I am doing wrong? Question is where DBNull comes from?
================================
Update:
Thanks for the nice answers. My main point of confusion was "STUPID" XML visualizer was wrongly showing Reviewed field as checked(as you see in the picture above). I checked the values in the dataset itself and realized all are indeed DBNUll.
It means that some of your data coming from database is null. You need to check for that before assigning.You might want to assign a default value Like.
iif((DataBinder.Eval(Container.DataItem, "Reviewed") is DbNull.Value),false, DataBinder.Eval(Container.DataItem, "Reviewed")))
You can also create a method that checks for both DBNULL and null values and returns you the appropriate value. like (it may have syntax error that you can correct)
<Asp:Checkbox id="Textbox5" width="40" Checked='<%# YourMethod(Eval("Reviewed")) %>' runat="server" />
Try to change your markup code to this code.
<asp:Checkbox
Checked='<%# (Eval("reviewed")==DBNull.Value ? false : Eval("reviewed"))%>'
runat="server" ID="Label22" />
So instead of binding a nullValue to boolean property you assign false if there is DBNull. Credits go to limno

Send value of gridview row using hyperlink within it

I am not able to sent the value of the MachineID to another page using the hyperlink in gridview.
<!-- <asp:TemplateField HeaderText="FailedFiles"
SortExpression="NumFailedFilesOverSLA">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%#Bind("NumFailedFilesOverSLA") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I have tried putting
DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}"
but don't know why this is not working??
Please suggest...
thanks
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Inventory_ID", "/default.aspx?ID={0}") %>'
Text="Details"></asp:HyperLink>
</ItemTemplate>
This should solve your problem. This is exactly how I used it.
If this doesn't work, then check that you are actually getting a value back from the DB for MachineID:
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("NumFailedFilesOverSLA") %>'
runat="server" DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}">
</asp:HyperLink>
First, try to put the default gridview in the page and attach it to the data source, so you can test if there is data to display.
If you are assigning the data source from code behind don't forget to call the DataBind() method after that.

Categories

Resources