How to allow null in Context.Request
:
context.Response.Write(retrieveList(context.Request["SalCode"].ToString(null), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
How to allow null in Context.Request
:
context.Response.Write(retrieveList(context.Request["SalCode"].ToString(null), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
Firstly, Request["..."]
already returns a string
, so there is no need to call ToString()
on it and thus no need to worry, at this stage, if it returns null
(i.e. if the key is not present in the request).
Thus you can call e.g.
retrieveList(
context.Request["SalCode"],
context.Request["groupKeyword"],
context.Request["text"]);
without worrying if any of the three are null.
You can then alter retrieveList
to respond correctly if any of the inputs is null. For example, you could return null:
private string /*or whatever*/ retrieveList(
string salCode, string groupKeyword, string text)
{
if (String.IsNullOrEmpty(salCode) ||
String.IsNullOrEmpty(groupKeyword) ||
String.IsNullOrEmpty(text))
{
return null;
}
...
}
Then, note that Response.Write
doesn't care if you give it a null
, it just writes nothing, so you can keep the call to Write
as above.
Alternatively, you could for example check the return value and write a message if it is null
:
var list = retrieveList(
context.Request["SalCode"],
context.Request["groupKeyword"],
context.Request["text"]));
if (!String.IsNullOrEmpty(list))
{
context.Response.Write(list);
}
else
{
context.Response.Write("Missing request parameter.");
}
TYou didn't specify anything! Please add information, you even didn't specify the environment, the classes, or the general context in which this problem occurs. nor do we know what the signature of retrieveList() is! This makes us very difficult to help you! How much would you like to answer the question, one of my colleagues faced once: 'This crap doesn't work!' ? (Yes, it is not even a question, but happened in a real life support situatiuon!)
One thing I noted is that you use *T*oString() instead of *t*oString(), I assume that is a typo. (edited, as it is clear that this is C#) Also, I don't know what .toString(null) means. Did you want to tell us that that statement causes the NullPointerException? In that case, you could have committed a bit more effort towards us to understand your question, e.g. by writing this down...
BTW, if that's the case, I'd say, this will solve your problem:
Object salCode = context.Request["SalCode"];
context.Response.Write(retrieveList(salCode==null?"":salCode.ToString(), context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));
EDIT I think (but have no means to test) that if the null is the problem, this would fix it. If however, the underlying code does not work properly with empty String specified, that should be checked in retrieveList() like this (pasted from referenced post):
private string retrieveList(string SalCode, string groupKeyword, string text)
{
SqlConnection _sqlCon = default(SqlConnection);
SqlCommand _sqlCom = default(SqlCommand);
SqlDataReader _sqlReader = default(SqlDataReader);
StringBuilder _sb = new StringBuilder();
List<TokenInputJSON> _out = null;
try
{
_sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SalesianBlastConnectionString"].ConnectionString);
_sqlCom = new SqlCommand("getTokenInput", _sqlCon);
_sqlCom.CommandType = CommandType.StoredProcedure;
_sqlCon.Open();
//This is the edited part
if(SalCode==null || SalCode.Equals("")) {
_sqlCom.Parameters.Add(new SqlParameter("@salCode", SqlDbType.VarChar, 4)).Value = SalCode;
}
//... continue with the other parts
Quick check: I just had an idea: use a constant value when calling retrieveList to find out if this is the problem:
context.Response.Write(retrieveList("enterAValidSalCodeHere", context.Request["groupKeyword"].ToString(), context.Request["text"].ToString()));