Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用DropDownList按价格对数据进行排序_C#_Asp.net_Sql - Fatal编程技术网

C# 无法使用DropDownList按价格对数据进行排序

C# 无法使用DropDownList按价格对数据进行排序,c#,asp.net,sql,C#,Asp.net,Sql,我试图按价格对数据(在数据列表中)进行排序。这意味着,如果用户希望价格从最贵到最便宜,用户可以选择DropDownList,反之亦然。根据我的catID,我的数据库中存储的价格是随机序列的。我已经按照如下方式编写了代码,但它没有按照我编写的代码进行排序。我做错了什么?请告诉我 protected void Page_Load(object sender, EventArgs e) { } protected void DropDownList1_SelectedIndexChanged(ob

我试图按价格对数据(在数据列表中)进行排序。这意味着,如果用户希望价格从最贵到最便宜,用户可以选择DropDownList,反之亦然。根据我的catID,我的数据库中存储的价格是随机序列的。我已经按照如下方式编写了代码,但它没有按照我编写的代码进行排序。我做错了什么?请告诉我

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    bindDropDownList();
}

private void bindDropDownList()
{
    DropDownList1.DataTextField = "price";
    DataList1.DataSourceID = null;
    DataList1.DataSource = getReader();
    DropDownList1.DataBind();

}

private SqlDataReader getReader()
{
    SqlDataReader reader = null;

    if(DropDownList1.Text == "-Select-")
    {
      string strConnectionString =
            ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText ="SELECT * FROM [Category ] WHERE catID<= 20";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
    {
        string strConnectionString =
            ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price desc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }

    else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
    {
        /string strConnectionString =
            ConfigurationManager.ConnectionStrings["ProBizConnection"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT catID, packageName, price, description1, description2, image1, image2 FROM Category WHERE catID <= 20 ORDER BY price";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        myConnect.Open();

   reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
    return reader;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
bindDropDownList();
}
私有void bindDropDownList()
{
DropDownList1.DataTextField=“价格”;
DataList1.DataSourceID=null;
DataList1.DataSource=getReader();
DropDownList1.DataBind();
}
私有SqlDataReader getReader()
{
SqlDataReader=null;
如果(DropDownList1.Text==“-选择-”)
{
字符串strConnectionString=
ConfigurationManager.ConnectionString[“ProBizConnection”]。ConnectionString;
SqlConnection myConnect=新的SqlConnection(strConnectionString);

string strCommandText=“SELECT*FROM[Category],其中catID排序未正确处理Price列可能是因为Price的数据类型不是十进制的,可能是Varchar


因此,请将价格数据类型更改为Decimal以正确排序

我可以看到三个错误

1) 您的
getReader
代码有一个返回null的路径。您可能应该确保在其他检查为false的情况下,您的默认值始终为
else

2) 您正在读取
DropDownList1的值。SelectedValue
同时试图让读卡器数据绑定到它。我觉得这是错误的-我假设有两个下拉列表,一个包含排序信息,另一个是您想要设置并数据绑定到的内容

3) 在getReader的elseif中,您正在比较
DropDownList 1.DataTextField
,该字段应包含您要绑定的字段的名称(您在
bindDropDownList
方法的前面代码中将其设置为
“price”

如果这些错误中的任何一个是您遇到的问题(如果现在不是,将来可能会是),那么您应该能够通过在调试模式下单步执行来发现这一点。这应该告诉您代码的运行路径等等

如果以上任何一项都没有导致您当前的问题,那么请提供更多详细信息。我必须强调三个单独的问题,因为您的描述没有充分说明您到底出了什么问题


是一个非常有用的链接,指向一个页面,该页面旨在帮助解释是什么让问题变得好和容易回答。

我认为这句话是错误的,可能永远不会正确

 else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
应该是

 else if (DropDownList1.SelectedValue == "Price - Lowest to Highest")
甚至更好

 else if (DropDownList1.Text == "Price - Lowest to Highest")
更新:

AppendDataBoundItems=“true”
可能会干扰您的下拉列表项。最低和最高的结果正在合并,这可能就是您无法看到差异的原因

删除默认列表项并将它们放在代码中

private void bindDropDownList()
{
   DropDownList1.DataTextField = "price";
   DropDownList1.DataSource = getReader();
   DropDownList1.DataBind();

   DropDownList1.Items.Insert(0, new ListItem("-Select-"));
   DropDownList1.Items.Insert(1, new ListItem("Price - Highest to Lowest"));
   DropDownList1.Items.Insert(2, new ListItem("Price - Lowest to Highest"));
}

对于另一种方法,请检查

您可能想提供有关错误的更多详细信息…它根本没有排序吗?它总是从低到高或从高到低排序吗?它没有返回任何内容吗?下拉列表的名称是否正确?您似乎正在检查
DropDownList1
以获取排序信息,但这也是您正在尝试的g绑定…我已经将数据类型更改为十进制,但价格仍然没有相应排序。它仍然作为默认布局,根据catID显示。当然!我在上面的问题中添加了。出错的部分是,无论我在dropdownlist中选择了什么(1.价格从高到低2.价格从低到高)输出仍然与默认值相同(即当用户看到页面时,数据根据我存储在数据库中的catID显示)。那么我提出的其他要点呢?您是否真的将具有价格选项的dropdownlist绑定到select语句的结果?从您第一次运行该代码时我可以看到,最有可能选择的项目是“-select-”(因为默认情况下没有指定选择它们,所以我希望第一个是选中的)所以我希望
getReader()
返回`null(因为两个if语句都不匹配).因此,我希望它尝试使用空数据源进行数据绑定…当您单步执行时会发生什么?这真的是正确的代码吗?您是对的!它返回一个空值,因为第一项是-Select-。但是,我根据codingbiz的说法更改了代码,我删除了列表项并将它们插入到.cs代码中。然而,新的p问题出现了。现在价格确实按照从高到低的顺序排序,反之亦然,但它在thr dropdownlist中排序,而不是在我的数据列表中排序。正如我多次说过的,您正在将该记录集数据绑定到您的dropdownlist-
DropDownList1.DataSource=getReader()
。将其更改为调用datalist的任何名称,这应该很好…我有时觉得没有人读我写的东西…;-)我将其更改为DataList1.DataSource=getReader();但我遇到以下错误:“DataSource和DataSourceID都是在“DataList1”上定义的。删除一个定义。"在你的代码中放一个断点,看看发生了什么-在if和else部分现在我知道错误在哪里了。结果是,他们将我的价格从最高到最低,从最低到最高排序,并将它们放在dropdownlist中,而不是在我使用数据列表显示产品时将其排序在我的数据列表中。那么,我该怎么做呢t从下拉列表中选择并在我的数据列表中显示价格,而不是从下拉列表中选择并在我的数据列表中显示价格
private void bindDropDownList()
{
   DropDownList1.DataTextField = "price";
   DropDownList1.DataSource = getReader();
   DropDownList1.DataBind();

   DropDownList1.Items.Insert(0, new ListItem("-Select-"));
   DropDownList1.Items.Insert(1, new ListItem("Price - Highest to Lowest"));
   DropDownList1.Items.Insert(2, new ListItem("Price - Lowest to Highest"));
}