C# 查询字符串不返回值的方法

C# 查询字符串不返回值的方法,c#,asp.net,linq-to-sql,C#,Asp.net,Linq To Sql,我通过查询字符串将一些值从一个页面传递到另一个页面,它正确地传递了值,然后成功地传递到存储过程,但一次又一次地键入请求。QueryString[“something”]非常烦人,所以我为它创建了一个方法,但该方法不返回/传递任何值,并引发nullreference异常 protected void Page_Load(object sender, EventArgs e) { try { using (Property_dbDataContext context

我通过查询字符串将一些值从一个页面传递到另一个页面,它正确地传递了值,然后成功地传递到存储过程,但一次又一次地键入
请求。QueryString[“something”]
非常烦人,所以我为它创建了一个方法,但该方法不返回/传递任何值,并引发nullreference异常

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        using (Property_dbDataContext context = new Property_dbDataContext())
        {
            _errMsg.Enabled = false;
            _errMsg.Visible = false;                    

            var find_prop = context.find_property(val("city"), val("type"), val("subtype"), val("bedrooms"), val("size_unit"), Convert.ToInt32(val("area_from")), Convert.ToInt32(val("areato")), Convert.ToInt32(val("pricefrom")), Convert.ToInt32(val("priceto"))).ToList();
            //code above does not return any value

            //var find_prop = context.find_property(Request.QueryString["city"], Request.QueryString["type"], Request.QueryString["subtype"], Request.QueryString["bedrooms"], Request.QueryString["size_unit"], Convert.ToInt32(Request.QueryString["area_from"]), Convert.ToInt32(Request.QueryString["areato"]), Convert.ToInt32(Request.QueryString["pricefrom"]), Convert.ToInt32(Request.QueryString["priceto"])).ToList();  
           //code above return value from the database but its a long procedure

            rptr_properties.DataSource = find_prop;
            rptr_properties.DataBind();
        }
    }
    catch(Exception ex)
    {
        _errMsg.Enabled = true;
        _errMsg.Visible = true;
        _errMsg.Text = "Sorry! Property not found." + ex;
    }
}

public string val(string a)
{
    return Request.QueryString["" + a + ""].ToString();
}

在尝试访问该项目之前,您需要确保该项目存在:

public string val(string a)
{
    if(Request.QueryString[a] != null)
       return Request.QueryString[a].ToString();

    return string.Empty;
}
关于你的评论;这尤其是:

Convert.ToInt32(val("area_from"))
将导致问题,因为默认值是空字符串

不过,像这样的东西现在应该可以用了:

public string ValToString(string a)
{
    if(Request.QueryString[a] != null)
       return Request.QueryString[a].ToString();

    return string.Empty;
}

public int ValToInt32(string a)
{
    if(Request.QueryString[a] != null)
       return Convert.ToInt32(Request.QueryString[a]);

    return 0;
}
然后修改查找代码:

var find_prop = context.find_property(ValToString("city"), ValToString("type"), ValToString("subtype"), ValToString("bedrooms"), ValToString("size_unit"), Convert.ToInt32(val("area_from")), Convert.ToInt32(val("areato")), ValToInt32("pricefrom"), ValToInt32("priceto")).ToList();

将querystring值转换为int(32)的更好方法:


如果TryParse无法转换该值,则该方法将返回“int.MinValue”,然后可以检查返回的值是否不等于“int.MinValue”,并从此处开始。

System.FormatException:输入字符串的格式不正确。在System.Number.ParseInt32(String s,NumberStyles Styles,NumberBuffer&Number,NumberFormatInfo信息,Boolean parseDecimal)在System.Number.ParseInt32(String s,NumberStyles Styles,NumberFormatInfo信息)在System.Convert.ToInt32(String值)在find_properties.Page_Load(对象发送者,EventArgs e)在c:\Users\haide\u 000\Documents\Visual Studio 2013\WebSites\Property\u site\find\u properties.aspx.cs中:第23行感谢您的建议…非常感谢…:-)
    This will work
    public string val(string a)
    {
        return Request.QueryString[a];
    }
public int GetIntFromQueryString(string key)
{
    int result = int.MinValue;
    if(Request.QueryString[key] != null)
    {
        int.TryParse(Request.QueryString[key].ToString(), out result);
    }
    return result;
}