ASP.Net Dropdown List is not throwing an error - asp.net

I am having problems with dropdown lists NOT throwing an error with the following code in ASP.Net 4.0
the dropdown list is empty to start. The page in question is a simple test page containing no code besides the lines below in Page_Load.
ddlTest.Items.Add(new ListItem("test","test"));
ddlTest.SelectedValue = "Fail";
When I load the page, the DDL displays "test" as the only item (as expected) and no error is thrown. I thought an error would be thrown with something like "item 'Fail' does not exist"
I have tested the code both, inside an if (!IsPostBack) block and outside of it. The results are the same.
Does this make sense? I don't understand why this is not throwing an error. Any explanation would be greatly appreciated.
Thanks.

It will only throw an exception if there are no items in ddlTest, otherwise it just doesn't find the value.
You can always do this first if you are trying to find out if the item exists in the list:
if (ddlTest.Items.FindByValue("Fail") != null)
ddlTest.SelectedValue = "Fail";
else
//item doesn't exist, do something meaningful here

Related

datagrid "Sorry an error occurred" when adding a new column

I have a datagrid that is working but when I add another simple column, I the the "Sorry an error occurred" error message. I've tried it for a couple of fields I have on the view and even reverted it back to what I had before. It's still giving me this error. I'd include code but I don't know what to include. Does anyone know this issue?

Meteor - Binding to element on initial template render throws error

I am using Meteor, FlowRouter, and Parsley for validation. When I reload the app and enter into the page with the form, I get this error and parsley is not working on the form:
You must bind Parsley on an existing element.
If I leave the page and come back, it works fine. I have the following code initializing the binding:
Template.report.onRendered ->
report = Reports.findOne(_id: FlowRouter.getParam('reportId'))
if report.status == 'finalized'
Session.set('showDistributeReport', true)
else
Session.set('showDistributeReport', false)
$('#status-js').val(report.status)
$('#report-form-js').parsley()
I have been using Parsley and haven't seen this problem on other pages. Any help would be greatly appreciate.
I figured out a fix but I'm not sure why I had to do this. If anyone has any input that would be great. I had to take the portion where I set the session variable and move it to the onCreated method. This is the code I have now.
Template.report.onCreated ->
report = Reports.findOne(_id: FlowRouter.getParam('reportId'))
if report.status == 'finalized'
Session.set('showDistributeReport', true)
else
Session.set('showDistributeReport', false)
Template.report.onRendered ->
report = Reports.findOne(_id: FlowRouter.getParam('reportId'))
$('#status-js').val(report.status)
$('#report-form-js').parsley()

Telerik Reporting Hide Subreport

I'm working through the Telerik Reporting step-by-step tutorial located here:
http://www.telerik.com/documents/TelerikReporting-LearningGuide.pdf
I followed the steps precisely in the Lab: Subreports section (page 111-115). So this lab is to display a Master-Detail report. There is a Boolean Editor (User choose True or False) on the UI when the report is run. The way it is supposed to work is that, when the report is loaded, both the Master report and the Detail subreport are displayed. If the User selects False, the subreport is supposed to then be hidden.
I got the Master-Detail report to display properly. However, after I added the functionality for hiding the subreport, when I preview the report I get the following error message: "An error has occurred while processing SubReport 'subReport1': Specified cast is not valid". The issue lies in the following statement:
(sender as Telerik.Reporting.Processing.SubReport).Visible = (bool)this.ReportParameters["ShowEmployees"].Value;
Therefore, as far as I can tell, this tutorial is faulty. So the cast which I'm attempting on the right hand side of this assignment statement above is invalid. I tried the following unsuccessfully (still get an Exception), also:
(sender as Telerik.Reporting.Processing.SubReport).Visible = Convert.ToBoolean(this.ReportParameters["ShowEmployees"].Value;
I have been trying everything for a few days now to find an alternate solution to achieve my goal, which is to hide the subreport in case the User selects False in the Boolean Editor. Does anyone know either
how to resolve the cast issue in the assignment statement above, or
an alternate solution to hide the subreport in case the User selects False in the Boolean Editor?
That tutorial is around since v.3 of Telerik Reporting and I don't think it is updated since.
You can get reference to the main report and use the processing report parameter instead of the definition, try the following code to hide the subreport based on a report parameter (dummy code, not tested so u get an idea):
private void subReport1_ItemDataBound(object sender, System.EventArgs e)
{
Processing.SubReport subReport = (Processing.SubReport)sender;
Processing.Report mainRep = (Processing.Report)subReport.Parent;
subReport.Visible = (bool)mainRep.Parameters["ShowEmployees"].Value;
}
See here for more info: http://www.telerik.com/help/reporting/designing-reports-parameters-programmatic-control.html

ASP.Net: How do I find this error?

I'm getting the following error when I try to run an ASP.Net page. It just popped up during development, and I don't know what the cause is.
"OverflowException Occurred. Value was either too large or too small for an Int32."
The line the debugger breaks on is of absolutely no help at all
Dim ApproxDate As TextBox = FormView1.FindControl("CurrentResultDateTextBox")
The error is occurring on this line because calling findControl on FormView1 causes it to databind (executes a SQL query to return the dataset). I assume the error lies somewhere in one of the object data sources or possibly one of the grid views on the page, but the debugger won't tell me where. Most of the page data sources are bound declaratively (specified in ASP tags in the source)
Pressing F11 to run past the exception displays the following:
"13712696100373320501120906 is not a valid value for Int32."
The value being shown here is the primary key for the main SQL table used by the page. There are about 10 data sources which use this field. Again, there's no indication of where exactly the error is occurring.
So how can I debug an error like this? Are there commands I can run? IIS error logs? 3rd party software to capture the error? I'll take anything I can get
Managed to find the problem. The trick is to first disable "just my code" in the IDE/Debug settings. Then you can see the system functions on the call stack, and while there is no source code, you can click on these functions to unwind the stack to one of the web controls (in this case the ListControl.PerformSelect function)
If you check the auto variables in the debug window, it will show the control involved in the error. From there I added a watch on the ID property to see which control it was, looked up the data source that feeds it, and found the error.
You can also unwind the stack to the ObjectDataSourceView, look at the dataTable property, and call GetErrors() to see the issues. This returns an array of data rows which are flagging errors; The property RowError will show the error message for any given row.

"Failed to enable constraints" Error keeps cropping up (seemingly randomly) in my ASP.Net project

I have a weird problem where the "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error sometimes pops up when I am trying to build my project.
The line in question throwing the error was auto-generated code in the designer file for the dataset. To find out the offending table, I used the following code:
Try
Me.Adapter.Fill(dataTable) <--Breakpoint here on the offending line
Catch ex As Exception
For Each TRow As DataRow In dataTable.Rows
If TRow.HasErrors Then
Trace.Write(TRow.RowError)
End If
Next
End Try
Funnily enough, as soon as I run the project after putting in the above code and breakpoint, the error disappears. I assume this has something to do with the code being regenerated. All data is presented successfully, and the project compiles without errors.
However, this has happened often enough for me to frustrate me. Anybody know what might be causing the error and how I can isolate it?
What unique constraint are you placing on the datatable and have you verified that the data your passing in doesn't have any duplicates? If this is coming from a shared database perhaps someone else is modifying the data which could be causing the randomness.
Edit
If that code is exactly as what you have (And forgive me my vb.net is not as strong as my C#). Wouldn't it swallow the exception? In C# we need to rethrow the error.
Add a Trace.Write() right before you go into your for loop.
You are probably working with a typed dataset, or have some code configuration on it. Also you might be reading from one table, and then adding data from another.
In any of those cases, you really want to look into any inconsistencies between the schema. If it is a typed dataset / or configured in code, check its definition, and look for anything different from the sql table. If you are reading from 2 different tables, check the definition of both tables.
Figured it out finally!
The reason the breakpoint wasn't being hit was because the code in question was auto-generated, and had the attribute "DebuggerNonUserCodeAttribute". In addition, in my VS settings, I had set the debugger to "Just my code".
When I ran the code again, and the breakpoint wasn't hit, I noticed the breakpoint icon had changed to a warning. Hovering showed this:
"The breakpoint will currently not be hit. Breakpoints cannot be set in a method or class with the 'DebuggerNonUserCode' attribute when the debugger option 'Just My Code' is enabled."
Ouch!
Once I had removed the "Just My Code" debugging option, the breakpoint was hit, and I discovered that one of the rows had a NULL value in one of the columns which had a non-null constraint. Stupid ole' me!
You should use this code before you fill DataTable :
var dt = new System.DataTable.DataTable();
dt.Clear();
dt.Rows.Clear();
dt.Columns.Clear();

Resources