英文:
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
> 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}'",
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("year = '{0}' AND waterlevel LIKE = '{1}'",
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论