0

我尝试了几种方法来做到这一点,但由于某种原因,我无法正确实现 If 语句。

我正在尝试获取SelectedItem.TextfromDrop Down List然后在数据库中搜索该行并将其存储在其中DataSet,然后将其显示在GridView. 但是,数据库有 19 个带有单词的固定列,因此如果用户在 if 语句中选择一个下拉列表,则DataSet应该显示在 a 上,GridView但如果在数据库中找不到数据,否则会弹出一条错误消息,显示“数据未找到”

我尝试将 19 固定columns在 an 中Array并使用以下内容进行物理输入:

protected void SearchProductButton_Click1(object sender, EventArgs e)
    {
        string Producttext = SearchProductDropDownList.SelectedItem.Text; 
        string[] Producttext2 = 
        {
            "Chem",
            "Drill",
            "Elect",
            "Products",
            "Environmental",
            "Instrument",
            "Lab",
            "Lift",
            "Tools",
            "Repair",
            "Pipes",
            "Portable",
            "Power",
            "Steel",
            "Testing",
            "Tooling",
            "Tubes",
            "Valves",
            "Systems",
            "Consumables",
        };
        using (var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
        using (var daProduct = new SqlDataAdapter("SELECT [Serial_No], [Material_No], [Product_Line], [Product_Description], [Size], [UOM], [Supplier_Price], [Price_Date], [Selling_Price] FROM [ProductDB] WHERE [Product_Line] = @Product_Line", con))
        {
            foreach (string x in Producttext2)
            {
                if (x.Contains(Producttext))                              
                {
                    daProduct.SelectCommand.Parameters.Add("@Product_Line", SqlDbType.VarChar, 50).Value = Producttext;
                    DataSet dsProduct = new DataSet();
                    daProduct.Fill(dsProduct, "Product_Line");
                    SearchProductGridView.DataSource = dsProduct;
                    SearchProductGridView.DataBind();                        
                }
                else
                {
                    MessageBox.Show("Data Not Found");
                }
            }

        }
    }

在 SQL 数据中,我只有为Chem&填充的信息,所以所有其他的都应该显示一个错误,Elect但由于value` 没有清除并保持在第一个运行值,所以它最终会跳过,直到它遍历整个列表。Foreachx

我在阅读了关于字符串到字符串数组的信息后修改了代码,然后检查index它是否greater then -1继续或else显示error.

这是代码:

 protected void SearchProductButton_Click1(object sender, EventArgs e)
    {
        string Producttext = SearchProductDropDownList.SelectedItem.Text; 
        string[] Productarray = new string[20]; 
        Productarray[0] = "Chem";
        Productarray[1] = "Drill";
        Productarray[2] = "Elect";
        Productarray[3] = "Products";
        Productarray[4] = "Environmental";
        Productarray[5] = "Instrument";
        Productarray[6] = "Lab";
        Productarray[7] = "Lift";
        Productarray[8] = "Tools";
        Productarray[9] = "Repair";
        Productarray[10] = "Pipes";
        Productarray[11] = "Portable";
        Productarray[12] = "Power";
        Productarray[13] = "Steel";
        Productarray[14] = "Testing";
        Productarray[15] = "Tooling";
        Productarray[16] = "Tubes";
        Productarray[17] = "Valves";
        Productarray[18] = "Systems";
        Productarray[19] = "Consumables";

        using (var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
        using (var daProduct = new SqlDataAdapter("SELECT [Serial_No], [Material_No], [Product_Line], [Product_Description], [Size], [UOM], [Supplier_Price], [Price_Date], [Selling_Price] FROM [ProductDB] WHERE [Product_Line] = @Product_Line", con))
        {
            //foreach (string x in Producttext2)
            //{
            int index1 = Array.IndexOf(Productarray, Producttext);
                if (index1 !=-1)                             
                {
                    daProduct.SelectCommand.Parameters.Add("@Product_Line", SqlDbType.VarChar, 50).Value = Producttext;
                    DataSet dsProduct = new DataSet();
                    daProduct.Fill(dsProduct, "Product_Line");
                    SearchProductGridView.DataSource = dsProduct;
                    SearchProductGridView.DataBind();                        

                }
                else
                {
                    MessageBox.Show("Data Not Found");
                }
            //}

        }
    }

第二种方法的问题在于,无论我尝试什么,它都不会进入 else 语句。

编辑:

我决定以不同的方式来满足我的要求,而不是使用Textbox我已经排序为使用ComboBoxfrom AJAX ToolKit

这是HTML

<ajaxToolkit:ComboBox ID="SearchMaterialComboBox" runat="server" 
        AutoCompleteMode="Append" AutoPostBack="false" CaseSensitive="false" 
        DataSourceID="ProductDBMaterialSqlDataSource" DataTextField="Material_No" 
        DataValueField="Material_No" DropDownStyle="DropDownList" Height="20px" 
        MaxLength="10" RenderMode="Block" style="display: inline-block;" Width="285px">
    </ajaxToolkit:ComboBox>**strong text**

代码

protected void SearchMaterialButton_Click(object sender, EventArgs e)
    {
        //long Materialtextbox = Convert.ToInt64(SearchMaterialComboBox.Text);
        string Materialstr = SearchMaterialComboBox.Text;
        using (var con = new SqlConnection("Data Source=localhost;Initial Catalog=ROG;Integrated Security=True"))
        using (var daMaterial = new SqlDataAdapter("SELECT [Serial_No], [Material_No], [Product_Line], [Product_Description], [Size], [UOM], [Supplier_Price], [Price_Date], [Selling_Price] FROM [ProductDB] WHERE [Material_No] = @Material_No", con))
        {
            if (Materialstr != "")
            {
                daMaterial.SelectCommand.Parameters.Add("@Material_No", SqlDbType.BigInt).Value = Materialstr;
                DataSet dsMaterial = new DataSet();
                daMaterial.Fill(dsMaterial, "Material_No");
                SearchProductGridView.DataSource = dsMaterial;
                SearchProductGridView.DataBind();
            }
            else
            {

            }

        }
    }       

我相信@Patashu 提到的最后一篇文章通过插入HasRowsa valueof有一个有效点0,如果不是,0则 continue 否则显示错误消息。但是,与传统方法相比,当前方法具有AutoComplete简洁的选项TextBox

4

1 回答 1

0
using System.Linq;

string match = Producttext2.FirstOrDefault((x) => x.Contains(Producttext));
if (match != null)
{
 // do stuff
}
else
{
 // error message
}

Method from http://msdn.microsoft.com/en-us/library/bb549039.aspx

于 2013-01-31T01:14:44.307 回答