C#筛选字段不起作用,但代码看起来是正确的。

huangapple go评论55阅读模式
英文:

C# filter fields are not working, but the code looks correct

问题

My database has 3 filters and I am trying to have them work together for a better search, but the code I am using says it is correct, but it is not working and often crashing the database with the error:

System.Data.SyntaxErrorException: 'Syntax error: Missing operand before '=' operator.'

here is my code

private void textBox3_TextChanged(object sender, EventArgs e)
{
    DataView DV = new DataView(this.datasetocean.Water_Level);

    if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        locationfilter.Text != null && locationfilter.Text.Length > 0 &&
        textBox1.Text != null && textBox1.Text.Length > 0)
    {
        if (yearfilter.Text.Length > 0 && int.TryParse(yearfilter.Text, out _))
        {
            DV.RowFilter = string.Format("location LIKE '%{0}%' AND waterlevel LIKE '%{1}%' AND year = '{2}'", yearfilter.Text, locationfilter.Text, textBox1.Text);
        };
    }
    else if (locationfilter.Text != null && locationfilter.Text.Length > 0 &&
            textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("location LIKE '%{0}%' AND waterlevel LIKE '%{1}%'", locationfilter.Text, textBox1.Text);
    }
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        locationfilter.Text != null && locationfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("location LIKE '%{0}%' AND year = '{1}'", yearfilter.Text, locationfilter.Text);
    }
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("year = '{0}' AND waterlevel LIKE '{1}'", yearfilter.Text, textBox1.Text);
    }
    // Otherwise only include the name filter if the name filter is not empty
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("year = '{0}'", yearfilter.Text);
    }
    else if (locationfilter.Text != null && locationfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("location = '{0}'", locationfilter.Text);
    }
    else if (textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("waterlevel = '{0}'", textBox1.Text);
    }

    water_LevelDataGridView.DataSource = DV;
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
    DataView DV = new DataView(this.datasetocean.Water_Level);

    if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        locationfilter.Text != null && locationfilter.Text.Length > 0 &&
        textBox1.Text != null && textBox1.Text.Length > 0)
    {
        if (yearfilter.Text.Length > 0 && int.TryParse(yearfilter.Text, out _))
        {
            DV.RowFilter = string.Format("location LIKE '%{0}%' AND waterlevel LIKE '%{1}%' AND year = '{2}'", yearfilter.Text, locationfilter.Text, textBox1.Text);
        };
    }
    else if (locationfilter.Text != null && locationfilter.Text.Length > 0 &&
            textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("Location LIKE '%{0}%' AND WaterLevel LIKE '%{1}%'", locationfilter.Text, textBox1.Text);
    }
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        locationfilter.Text != null && locationfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("location LIKE '%{0}%' AND year = '{1}'", yearfilter.Text, locationfilter.Text);
    }
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
        textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("year = '{0}' AND waterlevel LIKE '{1}'", yearfilter.Text, textBox1.Text);
    }
    // Otherwise only include the name filter if the name filter is not empty
    else if (yearfilter.Text != null && yearfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("year = '{0}'", yearfilter.Text);
    }
    else if (locationfilter.Text != null && locationfilter.Text.Length > 0)
    {
        DV.RowFilter = string.Format("location = '{0}'", locationfilter.Text);
    }
    else if (textBox1.Text != null && textBox1.Text.Length > 0)
    {
        DV.RowFilter = string.Format("waterlevel = '{0}'", textBox1.Text);
    }

    this.water_LevelDataGridView.DataSource = DV;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text.Length > 0 && int.TryParse(textBox1.Text, out _))
    {
        DataView DV = new DataView(this.datasetocean.Water_Level);

        if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
            locationfilter.Text != null && locationfilter.Text.Length > 0 &&
            textBox1.Text != null && textBox1.Text.Length > 0)
        {
            if (yearfilter.Text.Length > 0 && int.TryParse(yearfilter.Text, out _))
            {
                DV.RowFilter = string.Format("location LIKE '%{0}%' AND waterlevel LIKE '%{1}%' AND year = '{2}'", yearfilter.Text, locationfilter.Text, textBox1.Text);
            };
        }
        else if (locationfilter.Text != null && locationfilter.Text.Length > 0 &&
                textBox1.Text != null && textBox1.Text.Length > 0)
        {
            DV.RowFilter = string.Format("location LIKE '%{0}%' AND waterlevel LIKE '%{1}%'", locationfilter.Text, textBox1.Text);
        }
        else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
            locationfilter.Text != null && locationfilter.Text.Length > 0)
        {
            DV.RowFilter = string.Format("location LIKE '%{0}%' AND year = '{1}'", yearfilter.Text, locationfilter.Text);
        }
        else if (yearfilter.Text != null && yearfilter.Text.Length > 0 &&
            textBox1.Text != null && textBox1.Text.Length > 0)
        {
            DV.RowFilter = string.Format("year = '{0}' AND waterlevel LIKE '{1}'", yearfilter.Text, textBox1.Text);
        }
        // Otherwise only include the name filter if the name filter is not empty
        else if (yearfilter.Text != null && yearfilter.Text.Length > 0)
        {
            DV.RowFilter = string.Format("year = '{0}'", yearfilter.Text);
        }
        else if (locationfilter.Text != null && locationfilter.Text.Length > 0)
        {
            DV.RowFilter = string.Format("location = '{0}'", locationfilter.Text);
        }
        else if (textBox1.Text != null && textBox1.Text.Length > 0)
        {
            DV.RowFilter = string.Format("waterlevel = '{0}'", textBox

<details>
<summary>英文:</summary>

My database has 3 filters  and I am trying to have them work together for a better search, but the code I am using says it is correct, but it is not working and often crashing the database with the error 

&gt; System.Data.SyntaxErrorException: &#39;Syntax error: Missing operand
&gt; before &#39;=&#39; operator.&#39;

here is my code
 
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
           

                DataView DV = new DataView(this.datasetocean.Water_Level);



            if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                if (yearfilter.Text.Length &gt; 0 &amp;&amp; int.TryParse(yearfilter.Text, out _))
                {

                    DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND waterlevel LIKE &#39;%{1}%&#39; AND year = &#39;{2}&#39;&quot;, yearfilter.Text, locationfilter.Text, textBox1.Text);
                };
            }
            else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                    textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND waterlevel LIKE &#39;%{1}%&#39;&quot;, locationfilter.Text, textBox1.Text);
            }
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND year = &#39;{1}&#39;&quot;, yearfilter.Text, locationfilter.Text);
            }
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39; AND waterlevel LIKE = &#39;{1}&#39;&quot;,
                yearfilter.Text, textBox1.Text);
            }
            // Otherwise only include the name filter if the name filter is not empty
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39;&quot;,
                yearfilter.Text);

            }
            else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;location = &#39;{0}&#39;&quot;,
                locationfilter.Text);
            }
            else if (textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;waterlevel = &#39;{0}&#39;&quot;,
                textBox1.Text);
            };

            water_LevelDataGridView.DataSource = DV;


        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
            DataView DV = new DataView(this.datasetocean.Water_Level);

            if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                if (yearfilter.Text.Length &gt; 0 &amp;&amp; int.TryParse(yearfilter.Text, out _))
                {

                    DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND waterlevel LIKE &#39;%{1}%&#39; AND year = &#39;{2}&#39;&quot;, yearfilter.Text, locationfilter.Text, textBox1.Text);
                };
            }
            else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                    textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;Location LIKE &#39;%{0}%&#39; AND WaterLevel LIKE &#39;%{1}%&#39;&quot;, locationfilter.Text, textBox1.Text);
            }
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND year = &#39;{1}&#39;&quot;, yearfilter.Text, locationfilter.Text);
            }
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39; AND waterlevel LIKE = &#39;{1}&#39;&quot;,
                yearfilter.Text, textBox1.Text);
            }
            // Otherwise only include the name filter if the name filter is not empty
            else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39;&quot;,
                yearfilter.Text);

            }
            else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;location = &#39;{0}&#39;&quot;,
                locationfilter.Text);
            }
            else if (textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
            {
                DV.RowFilter = string.Format(&quot;waterlevel = &#39;{0}&#39;&quot;,
                textBox1.Text);
            };

            this.water_LevelDataGridView.DataSource = DV;


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length &gt; 0 &amp;&amp; int.TryParse(textBox1.Text, out _))
            {
            DataView DV = new DataView(this.datasetocean.Water_Level);

                if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                    locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                    textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
                {
                    if (yearfilter.Text.Length &gt; 0 &amp;&amp; int.TryParse(yearfilter.Text, out _))
                    {

                        DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND waterlevel LIKE &#39;%{1}%&#39; AND year = &#39;{2}&#39;&quot;, yearfilter.Text, locationfilter.Text, textBox1.Text);
                    };
                }
                else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0 &amp;&amp;
                        textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND waterlevel LIKE &#39;%{1}%&#39;&quot;, locationfilter.Text, textBox1.Text);
                }
                else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                    locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;location LIKE &#39;%{0}%&#39; AND year = &#39;{1}&#39;&quot;, yearfilter.Text, locationfilter.Text);
                }
                else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0 &amp;&amp;
                    textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39; AND waterlevel LIKE = &#39;{1}&#39;&quot;,
                    yearfilter.Text, textBox1.Text);
                }
                // Otherwise only include the name filter if the name filter is not empty
                else if (yearfilter.Text != null &amp;&amp; yearfilter.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39;&quot;,
                    yearfilter.Text);

                }
                else if (locationfilter.Text != null &amp;&amp; locationfilter.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;location = &#39;{0}&#39;&quot;,
                    locationfilter.Text);
                }
                else if (textBox1.Text != null &amp;&amp; textBox1.Text.Length &gt; 0)
                {
                    DV.RowFilter = string.Format(&quot;waterlevel = &#39;{0}&#39;&quot;,
                    textBox1.Text);
                };

                this.water_LevelDataGridView.DataSource = DV;
            }
        }

</details>


# 答案1
**得分**: 2

你有一些代码行如下:

```csharp
DV.RowFilter = string.Format("year = '{0}' AND waterlevel LIKE = '{1}'",

LIKE 的语法不使用等号运算符,这很可能是你的问题所在。

这是更友好的建议,但如果你尝试简化一些条件并用函数替换重复的代码,调试代码会更容易。

英文:

You have a few lines that have

         DV.RowFilter = string.Format(&quot;year = &#39;{0}&#39; AND waterlevel LIKE = &#39;{1}&#39;&quot;,

The syntax for LIKE doesn't use an equals operator, and this is likely your issue.

This is more friendly advice, but you'll have a much easier time debugging your code if you try to simplify some of your conditions and replace repeating code with functions

huangapple
  • 本文由 发表于 2023年5月11日 06:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定