im getting null exception . while im directly exceciting this page. i want to handle null exception
C#
string json = "";
if (Request.QueryString["data"] !="")
{
json = Request.QueryString["data"];
var req = JsonConvert.DeserializeObject<Request>(json);//getting error in this line
string requestid = req.requestId;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MYSTRING"].ConnectionString);
SqlCommand cmd = new SqlCommand();
connection.Open();
}
error
Value cannot be null. Parameter name: value
Well presumably Request.QueryString["data"]
is null. You're currently checking whether it's a reference to an empty string, but not whether it's a null reference. I suspect you want to use string.IsNullOrEmpty
to check that:
string json = Request.QueryString["data"];
if (!string.IsNullOrEmpty(json))
{
var req = JsonConvert.DeserializeObject<Request>(json);
...
}
See more on this question at Stackoverflow