根据下拉值

时间:2016-06-13 07:49:03

标签: c# asp.net linq gridview

我有下拉和网格视图..我从数据库填充下拉列表

有两个表tblRegtblRes都有RegionID

 protected void Page_Load(object sender, EventArgs e)
        {
            T1 tea = new T1();
            var list1 = tea.tblReg.ToList();
            var list = (from ta in list1
                        let data = ta.Region + " " + ta.StartDate ?? DateTime.Now.ToString() + " " + ta.EndDate ?? DateTime.Now.ToString()
                        select new { data, ta.RegionID }).ToList();

            if(!Page.IsPostBack)
            {
                regiondrop.DataSource = list;
                regiondrop.DataTextField = "data";
                regiondrop.DataValueField = "RegionID";
                regiondrop.DataBind();

            }
    }

现在在下拉列表中有几个值,我希望当我从下拉列表中选择值然后再次显示此值时,数据将显示

On dropdown SelectedIndexChange我做了这个

 protected void regiondrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        T1 ts = new T1();

        var dq=(from dropdwn in ts.tblRes
               where dropdwn.RegionID==Convert.ToInt32(regiondrop.SelectedValue)
               orderby dropdwn.OwnerName
               select new {
                            FFID = dropdwn.FFID,
                            OwnerName = dropdwn.OwnerName,
                            RegNo = dropdwn.RegNo,
                            RegionID = dropdwn.RegionID,

               });
        GridView1.DataSource = dq;
        GridView1.DataBind();
    }

当我从下拉列表中选择值时,它显示错误

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

on

GridView1.DataBind();

任何解决方案

1 个答案:

答案 0 :(得分:1)

好吧,正如您的错误提到的那样,您无法做到

Convert.ToInt32(regiondrop.SelectedValue)

在linq to entity查询中(它不能在sql / store表达式中翻译)

所以只需更改这些行

var dq=(from dropdwn in ts.tblRes
        where dropdwn.RegionID==Convert.ToInt32(regiondrop.SelectedValue)

var selectedValue = Convert.ToInt32(regiondrop.SelectedValue);

var dq=(from dropdwn in ts.tblRes
            where dropdwn.RegionID==selectedValue

你也可以这样做(但以前的解决方案更具可读性)

var dq=(from dropdwn in ts.tblRes
            where SqlFunctions.StringConvert((double)dropdwn.RegionID)==regiondrop.SelectedValue

SqlFunctions中有一个方法可以将数字转换为字符串,但是......不是字符串转换为数字值。