【问题标题】:Search Function Textbox not working properly c#搜索功能文本框无法正常工作c#
【发布时间】:2022-01-08 03:54:29
【问题描述】:

我的代码:

private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                DGViewListItems.Rows.Clear();
                populateTable();
            }
            else
            {
                if (byItemcode.Checked == true)
                {
                    DGViewListItems.Rows.Clear();
                    using (SqlConnection con = db.Connect())
                    {
                        try
                        {
                            //these Messageboxes is just for testing. to test if the data is correct
                            MessageBox.Show('%' + STEntry.whseFr.Text.Trim() + '%');
                            MessageBox.Show('%' + txtSearch.Text.Trim() + '%');
                            SqlDataReader rd;
                            SqlCommand cmd = new SqlCommand("sp_WhseItemsList", db.Connect());
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@Action", "I");
                            switch (activeform.formname)
                            {
                                case "Issuance List":
                                    //cmd.Parameters.AddWithValue("@WHSE", STEntry.whseFr.Text);
                                    break;

                                case "Stocks Transfer List":
                                    cmd.Parameters.AddWithValue("@WHSE", STEntry.whseFr.Text.Trim());
                                    break;

                                case "Stocks Adjustment List":
                                    cmd.Parameters.AddWithValue("@WHSE", SADJEntry.txtWhse.Text.Trim());
                                    break;
                            }
                            cmd.Parameters.AddWithValue("@Desc", "");
                            cmd.Parameters.AddWithValue("@Itemcode", '%' + txtSearch.Text.Trim() + '%');
                            rd = cmd.ExecuteReader();
                            int i = 0;
                            if (rd.HasRows)
                            {
                                while (rd.Read())
                                {
                                    DGViewListItems.Rows.Add();
                                    DGViewListItems.Rows[i].Cells["itemcode"].Value = rd["itemcode"].ToString();
                                    DGViewListItems.Rows[i].Cells["whsecode"].Value = rd["whsecode"].ToString();
                                    DGViewListItems.Rows[i].Cells["description"].Value = rd["description"].ToString();
                                    DGViewListItems.Rows[i].Cells["uom"].Value = rd["uom"].ToString();
                                    DGViewListItems.Rows[i].Cells["quantity"].Value = rd["quantity"].ToString();
                                    i++;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                else if (byDescription.Checked == true)
                {

                }
            }
        }

这对我不起作用,因为它没有正确填充 dgv。我不认为查询是存储过程内部的问题,因为我手动尝试了查询并且它工作正常

我尝试的查询:

SELECT DISTINCT A.*, B.description, B.uom 
    FROM inventoryTable A  
    LEFT OUTER JOIN Items B 
    ON A.itemcode = B.itemcode WHERE (A.whsecode = 'WHSE1' AND A.itemcode LIKE '%S%');

输出:

这是 textchanged 事件中代码的输出:

这里是更多示例输出:

这是存储过程内容供参考:

ALTER PROCEDURE [dbo].[sp_WhseItemsList] 
    @Action char(5) = '',
    @WHSE char(15) = '',
    @Desc varchar(50) = '',
    @Itemcode char(15) = ''
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    IF @Action = 'A'
        BEGIN
            SELECT DISTINCT A.*, B.description, B.uom 
            FROM inventoryTable A  
            LEFT OUTER JOIN Items B 
            ON A.itemcode = B.itemcode WHERE A.whsecode = @WHSE;
        END

    IF @Action = 'I'
        BEGIN
            SELECT DISTINCT A.*, B.description, B.uom 
            FROM inventoryTable A  
            LEFT OUTER JOIN Items B 
            ON A.itemcode = B.itemcode WHERE (A.whsecode = @WHSE) AND (A.itemcode LIKE @Itemcode);
        END

    IF @Action = 'D'
        BEGIN
            SELECT DISTINCT A.*, B.description, B.uom 
            FROM inventoryTable A  
            LEFT OUTER JOIN Items B 
            ON A.itemcode = B.itemcode WHERE (A.whsecode = @WHSE) AND (B.description LIKE @Desc);
        END
END

【问题讨论】:

    标签: c# sql select stored-procedures datagridview


    【解决方案1】:

    您的 @Itemcode 是一个 char(15),这意味着它始终有 15 个位置。所以这就变成了:

    A.itemcode LIKE '%S% ').

    并且LIKE 不会像= 那样忽略尾随空格。所以它只匹配一个包含“S”并以12个空格结尾的值。

    【讨论】:

    • 我添加了更多示例,那些怎么样? LIKE 忽略了空格。 nchar 是您在填写剩余长度时所说的,不是吗?
    • @RichardJohnson 表中itemcode 的数据类型是什么?
    • itemcode的数据类型是nc​​har。但是我用了很多次都没有问题,直到出现这个问题,这就是为什么我不知道是什么问题
    • 现在可以了,我在存储过程中将其更改为varchar,现在它可以工作了。直到现在我一直在使用 char 并且发生了这种情况,所以我不知道发生了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2019-08-04
    • 2017-01-28
    相关资源
    最近更新 更多