ASP.NET Form View IF on Updating - asp.net

I want to run an if before updating a form view;
if yes then..."message" & cancel update query
if no continue update query.
i've tried this but i'm getting a "obeject instance not set to null instance......" on the first line of the if? and the item updates regardless
Private Sub FormView2_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView2.ItemUpdating
Dim status As TextBox = FormView1.FindControl("ApprovalStatusTextBox")
If status.Text = "approved" Or "denied" Then
e.Cancel = True
lblupdaterequest.Text = "you cannot update this request as it has already been responded to"
Else
HolidayDetailsdatasource.Update()
End If
Anyone aware of a better was of achieving something like this?
exact error:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=WebApplication1
StackTrace:
at WebApplication1.HolidayApprovalDetails.DetailsView1_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) in line 32
at System.Web.UI.WebControls.DetailsView.OnItemUpdating(DetailsViewUpdateEventArgs e)
at System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation)
at System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I can at least see that you are retrieving the value of the status TextBox from 'FormView1', while your sub ItemUpdating is referring to 'FormView2'.
After changing this, try adding a message box before the if, to make sure you got the right value you wanted:
MsgBox(status.Text)
Hopefully, it helps you solve your problem.

If status IsNot Nothing AndAlso (status.Text = "approved" OrElse status.Text = "denied") Then

Related

Adwords API error: ReportDownloadError.INVALID_VERSION. c# client library

I'm trying to get the .NET client library code samples to return a criteria performance report but can't get the code to function. I'm getting an error when clicking the "Download Criteria Report button" on the AdWords.Examples.CSharp.OAuth test page.
The error is as following:
System.Net.HttpWebRequest.GetResponse() +1399
Google.Api.Ads.AdWords.Util.Reports.ReportUtilities.DownloadReport(String downloadUrl, String postBody) in C:\Development\Projects\google.adwords.api\src\AdWords\Util\Reports\New\ReportUtilities.cs:183
[AdWordsReportsException: AdWordsReportsException: One or more AdWords Report download errors have occurred.
ReportDownloadError.INVALID_VERSION. (Error: ReportDownloadError.INVALID_VERSION, FieldPath: , Trigger: )]
Google.Api.Ads.AdWords.Util.Reports.ReportUtilities.DownloadReport(String downloadUrl, String postBody) in C:\Development\Projects\google.adwords.api\src\AdWords\Util\Reports\New\ReportUtilities.cs:204
Google.Api.Ads.AdWords.Util.Reports.ReportUtilities.GetReport() in C:\Development\Projects\google.adwords.api\src\AdWords\Util\Reports\New\ReportUtilities.cs:166
Google.Api.Ads.Common.Util.Reports.AdsReportUtilities.GetResponse() in C:\Development\Projects\google.adwords.api\src\Common\Util\Reports\AdsReportUtilities.cs:162
Google.Api.Ads.AdWords.Util.Reports.ReportUtilities.GetResponse() in C:\Development\Projects\google.adwords.api\src\AdWords\Util\Reports\New\ReportUtilities.cs:310
Google.Api.Ads.AdWords.Examples.CSharp.OAuth.Default.OnDownloadReportButtonClick(Object sender, EventArgs eventArgs) in C:\Development\Projects\google.adwords.api\examples\AdWords\csharp\oauth\Default.aspx.cs:100
[ApplicationException: Failed to download report.]
Google.Api.Ads.AdWords.Examples.CSharp.OAuth.Default.OnDownloadReportButtonClick(Object sender, EventArgs eventArgs) in C:\Development\Projects\google.adwords.api\examples\AdWords\csharp\oauth\Default.aspx.cs:104
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9712662
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
What am i missing or doing wrong?
My main goal is to retrieve an XML feed that i can display on a webpage.
Any clues ?
which version are you using?
if you are using old version, update them.
Here is Deprecation Schedule.
https://developers.google.com/adwords/api/docs/sunset-dates

ASP.NET: Random.Next: NullReferenceException?

I am trying to set the text of two labels to random numbers on page load. This code
Random random = new Random();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LINE 22 Label12.Text = random.Next(99).ToString();
LINE 23 Label13.Text = random.Next(999).ToString();
}
foreach (string s in scr1.Style.Keys)
{
Response.Write(s + ",");
}
}
...
works on localhost but when run on my server throws a NullReferenceException.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
.apps..Page_Load(Object sender, EventArgs e) in C:\Users**\Documents\Visual Studio 2010\Projects****\apps**.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Two options:
Label12 could be null
random could have been set to null by some other piece of code
The first part of diagnosing the problem would be to work out which of those is the case. Simply split the assignment:
string randomText = random.Next(99).ToString();
Label12.Text = randomText;
Then see which line it fails on. Once you know which expression is null, you can try to work out why it's null.

Web Page aspx issue

I'm trying to access a the page below but I'm getting the following error:
Offending URL: http://www.mockcourt.org.uk/default.aspx
Source: App_Web_azvlqjha
Message: Object reference not set to an instance of an object. Stack
trace: at _Default.Page_Load(Object sender, EventArgs e) at
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
o, Object t, EventArgs e) at
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at
System.Web.UI.Control.LoadRecursive() at
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Does anyone know what is causing this?
Thanks
Ross
Theres not enough information to say exactly, but at a guess there is an object being referenced in your Page_Load method which does not exist. Perhaps you have something like:
Page.FindControl("ID_Here")
Where there is no control with the ID: ID_HERE

Column '' does not belong to table " even though it is present in the table and the query

Error Message is :
Exception of type 'System.Web.HttpUnhandledException' was thrown.
Friendly Message : System.ArgumentException: Column 'testfile' does
not belong to table . at System.Data.DataRow.GetDataColumn(String
columnName) at System.Data.DataRow.get_Item(String columnName) at
Registration_test.FillImage() in
c:\inetpub\wwwroot\testWeb\Events\Registration.aspx.cs:line 49 at
Registration_test.Page_PreInit(Object sender, EventArgs e) in
c:\inetpub\wwwroot\testWeb\test\Registration.aspx.cs:line 39 at
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object
o, Object t, EventArgs e) at
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) at System.Web.UI.Page.OnPreInit(EventArgs e) at
System.Web.UI.Page.PerformPreInit() at
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Target Site : Boolean HandleError(System.Exception) Source :System.Web
Error Stack Trace is : at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequest(HttpContext context) at
ASP.events_registration_aspx.ProcessRequest(HttpContext context) in
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET
Files\testWeb\761ce6e4\d915dd85\App_Web_posk8hm6.6.cs:line 0 at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,
Boolean& completedSynchronously)
I have got the above error as email when the site is hosted in IIS, but i donot get it when i am checking the code locally or even online.The error says the testfile column doesnot belong to datable even though the query returns the column.
can anyone help what could be the cause..
//////////////////////this is my code///////////////////////////////////////////////////
public partial class Registration_Registration : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
FillImage();
}
protected void FillImage()
{
string Query = "select t.header_image_file as 'HeaderImage',t.image,c.testfile from registrars c left join trade t on c.tradeid = t.tradeid and c.mainitemid = t.mainitemid ";
DataTable result = GetDataTable(Query);
if (result != null && result .Rows.Count > 0)
{
if (result .Rows[0]["testfile"] != null)
{
this.Theme = result .Rows[0]["testfile"].ToString().Trim();
this.MasterPageFile = result .Rows[0]["testfile"].ToString().ToLower().Trim() + ".master";
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}}
Ok so at a first glance we are seeing that the error is related to a column not being in a table.
To fix this I suggest you first run the query in management studio, check the names of the columns returned against the ones you are trying to access in your ASP.Net page.
If the column names are all correct backup the data then drop and recreate the table and the stored procedure that accesses this table and hopefully this will solve the issue.
It is a fairly easy to accidentally update a column etc in your database whilst developing and not push that change through to your production database.
If this doesnt solve things you will be able to get better help if you post your table structure, the query and the code which is accessing the query so we can diagnose the problem more efficiently.
UPDATE:
These are the columns you are selecting in your query
strt.header_image_file as 'HeaderImage',
t.image,
c.themefile
Yet in your code you are trying to access a column called testfile

LINQ: The query operator 'ElementAtOrDefault' is not supported

Why the following code produces the error?
The query operator 'ElementAtOrDefault' is not supported
Dim Im = (From view In Db.Views Where _
view.Pass = txtCode.Text _
Select New With {.Id = view.UniqueID.ToString}_
).Distinct
Response.Redirect("~/test.aspx?x=" & Im(0).Id)
Is there any way of fixing it without using the FirstOrDefault option?
UPDATE: And here is the StackTrace
at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.ElementAtOrDefault[TSource](IQueryable`1 source, Int32 index)
at Login.btnLogin_Click(Object sender, EventArgs e) in D:\Projects\Memoria\Login.aspx.vb:line 14
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
What you need to do is add .ToList() to the end of your query. This should work:
Dim Im = (From view In Db.Views Where _
view.Pass = txtCode.Text _
Select New With {.Id = view.UniqueID.ToString}_
).Distinct.ToList()
Response.Redirect("~/test.aspx?x=" & Im(0).Id)
Without .ToList(), the query just returns a DataQuery(Of T) instead of a List(Of T). Adding the ToList call does two things:
Forces the query to execute immediately, and
Returns a collection type that supports ElementAtOrDefault()
Hope that helps!

Resources