I have this method:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
//some logic
return results.ToArray();
}
I need to see contextKey variable.
So I tried to use this rows inside the method above:
Response.Write(contextKey);
Response.End();
When I try to use row above, I get this error:
Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Response.get'
The problem is that my method is static and I can't use Response class.
Any idea if there is some alternative way to see the prefixText value?
The project doesn't have a solution. I open it in notepad++.
Related
My code is giving me this error i am not getting why and to do
"Error 13 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'"
I am already using sessions to get a parameter at another page and method and works fine, now i want to use the same paramenter in a Schedualed Job i created for my app but is no working.
public static bool UpdateActivationResult(string recordId, string resultPart, string failureMessage, int reconciliation_count)
{
OracleConnection conn = new OracleConnection();
OracleTransaction transaction = null;
OracleCommand cmd;
string updateQuery = "";
int rowsUpdated = 0;
string notes = "";
string branch = clsUtility.GetHO;
clsUtility.CDFOperations operation = clsUtility.CDFOperations.CanNotActivateCard;
string userLoggedBranch = "";
try
{
userLoggedBranch = Session["userDegaS"].ToString(); //this row is giving me error
clsUtility.WriteLog4Net(recordId + " - " + resultPart + " - " + failureMessage, null, clsUtility.LogType.Debug, "");
using (conn = new OracleConnection(clsUtility.ConnectionString))
{ etc
i want to use this parameter in calling this method
if (reconciliation_count == 5 && !resultPart.Equals("SUCCESS"))
{
Utility.clsUtility.SendNotificationAsHTML(recordId, branch, operation, userLoggedBranch);
}
Any idea where am i doing wrong?
The Session property is a non-static property that is defined on the base Controller in .NET (msnd).
The the code listed with your question, you are trying to access this non-static method from without a static method. That is not possible.
You can solve it three ways:
If the method is within your controller, you can simply make it non-static in order to access the Session property.
You can also pass along the property you need (userLoggedBranch) as a parameter into your method UpdateActivationResult. Whatever action is calling that method will simply have to pass this value.
So something along the lines of:
public static bool UpdateActivationResult(
string recordId,
string resultPart,
string failureMessage,
int reconciliation_count,
string userLoggedBranch) {
// Your method code
Pass along the session itself, or something from which you can access the session (such as HttpContext, or the HttpRequest).
Declare userLoggedBranch as static variable or better is to simply access session in the respective method instead of passing it as a parameter
In my application, different canvases are stored as "pages", the contents of each canvas is stored as "cells".
Now when I want to load all cells that occupy / make up one canvas, I retrieve them like this:
public Task<List<Cell>> GetCellsAsync(string uPageGUID)
{
return database.QueryAsync<Cell>("SELECT * FROM cells WHERE cellpageguid = ?", uPageGUID);
}
This works great.
Now I would like to find out the "pageguid" of the page that has the value "pageisstartpage" set to true.
Therefore I'm trying the following:
public Task<string>GetStartPageGUID()
{
nPages<List<Page>>=database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
return nPages.First.GUID;
}
The compiler tells me:
nPages doesn't exist in the current context.
I don't see where I made a mistake.
nPages doesn't exist in the current context....I don't see where I made a mistake.
The first thing to mention is that the declaration of the List<Page> seems backwards.
nPages<List<Page>>=database....
The type has to be written first followed by the variable name.
List<Page> nPagesTask = database...
Another interpretation could be that you have a generic type variable nPages in which you want to specify the generic type. So the compiler looks whether this variable has already been declared. And apparently it cannot find any.
The second thing If you have an async method that returns a Task<string> you could do the following:
public async Task<string>GetStartPageGUID()
{
Task<List<Page>> nPagesTask = database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
List<Page> npages = await nPagesTask;
return nPages.First().GUID;
}
Here is the source of the QueryAsync method. this is the signature:
public Task<List<T>> QueryAsync<T> (string query, params object[] args)
so it returns a Task<List<T>>. Since your method specifies a different return type the usual pattern is to await it in a async method as described in the MSDN example and then return the type that you specified in you method.
You have to declare nPages correctly:
List<Page> nPages = database.QueryAsync<Page>("SELECT * FROM pages WHERE pageisstartpage=?", true);
Maybe this is silly, but I'm trying to shorten the calling of the method StreamWriter.WriteLine becasue I have to call it many times throughout my code. So, instead of calling myFile.WriteLine() I would like to write just myFile.WL().
Is this possible?
After searching for a solution I wrote this:
private static void WL(this StreamWriter myFile, params string myString)
{
return (void)myFile.WriteLine(myString);
}
but I get the error
Cannot convert type 'void' to 'void'
Any ideas?
There is no need for the extension method, but just for the sake of completeness...
Remove the return statement from your method, as it doesn't have to return anything.
private static void WL(this StreamWriter myFile, params string myString)
{
myFile.WriteLine(myString);
}
BUT, Reconsider what you are trying to do. There is no point in shortening WriteLine to WL, it doesn't serve any purpose, it will make the code less readable, also you will not be able to cover up all the overloads.
Your Extension method should only be
private static void WL(this StreamWriter myFile, params string myString)
{
myFile.WriteLine(myString);
}
Reasons:
WriteLinemethod of StreamWriter does not return anything i.e. void. It only and I quote Writes a string followed by a line terminator to the text string or stream. So you should remove return keyword from the method. Read here.
BTW, Extension method should probably be public if you want to use it outside of the class you define it in.
I have the following code (removed unrelated)
//top of class declaration
private delegate void UpdateFormElements(string status, bool addEmptyRow);
//inside a function in my class
if(lboImages.InvokeRequired)
{
lboImages.Invoke((UpdateFormElements)delegate { UpdateListBox("some text", true); });
}
private void UpdateListBox(string line, bool addEmptyRow)
{
lboImages.Items.Add(line);
if (addEmptyRow)
{
lboImages.Items.Add("");
}
}
Basically I'm trying to pass two parameters to the UpdateListBox function to test whether to add an empty line or not to my listbox, but I am getting the error in the title. I have tried putting the two values in an object[] but it doesn't seem to change anything as I still get the error.
I'm still new to using threads so not really sure where I'm going wrong here.
It's not clear why you're trying to use an anonymous method here. The problem is that you're creating a delegate type with two parameters, but you're not passing arguments (values for those parameters) into Invoke.
I suspect you just want:
lboImages.Invoke((UpdateFormElements) UpdateListBox, "some text", true));
That uses a method group conversion to create an UpdateFormElements delegate, and provides it the two arguments it needs.
Alternatively, you could just use a lambda expression:
MethodInvoker invoker = () => UpdateListBox(line, addEmptyRow);
lboImages.Invoke(invoker);
I am trying to assign a variable to an extension method, but getting the error when I hover over the line.
cannot assign 'void' to an implicitly-typed local variable
I am checking for empty fields and calling my extension method and wanted to check more than one field and if they were all failing I wanted them to appear in one error box instead of them piling up.
if (drp_SelectGroup.SelectedValue == "0")
{
var message = this.ShowMessage("Select an Ethnic Group", "ERROR", ErrorType.error);
}
Edit*
public static void ShowMessage(this Page page, string message, string title, ErrorType err)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "toastr",
String.Format("toastr.{0}('{1}','{2}');", err, message, title), addScriptTags: true);
}
The problem isn't that it's an extension method - it's that the extension method has a void return type. Presumably that method shows the message, rather than just creating it. You'd get exactly the same error message if you tried to call any other void method and assign the result to a variable.
You probably need to change your extension method to something like this:
public static string GetMessage(this Whatever foo, string message, string type,
ErrorType errorType)
From the error it looks like ShowMessage has a void return type.
That means that it isn't returning anything.
You are trying to assign that nothing type to a variable.
Solution:
Change the signature of ShowMessage to return what you need it to return.
The return type of this method is: void. You can not assign a type void to a variable.
this.ShowMessage("Select an Ethnic Group", "ERROR", ErrorType.error);
appears to have a return type of void. You cannot then assign to a local variable (var message)
You can change the return type of Show.Message() and this should work for you.