我正在尝试使用SqlDataReader
验证登录表单,但出现语法错误
System.Data.SqlClient.SqlException:''='附近的语法不正确。'
我已经检查了我的代码,但似乎没有检测到上述错误。请协助。
这是我的代码:
private void FillCombobox()
{
cmbAction.SelectedIndex = 0;
cmbAttachment.SelectedIndex = 0;
cmbDocumentType.SelectedIndex = 0;
cmbFollow.SelectedIndex = 0;
cmbRead.SelectedIndex = 0;
cmbSequrity.SelectedIndex = 0;
var Query = (from TblUsers in Database.Vw_Users where TblUsers.Activity == 1 select TblUsers).ToList();
Query.Insert(0, new Vw_Users { UserID = 1, FullName = "همه" });
cmbUserCreator.DataSource = Query;
cmbUserCreator.ValueMember = "UserID";
cmbUserCreator.DisplayMember = "FullName";
}
private void frmShowAllReferencesLetters_Load(object sender, EventArgs e)
{
ShowAllReferencesLetters(SearchCondition());
pdpFromDate.Value = DateTime.Now.AddDays(-10);
FillCombobox();
}
private string SearchCondition()
{
if (cmbUserCreator.SelectedIndex != 0)
{
SearchString += " And UserID = " + cmbUserCreator.SelectedValue;
}
return SearchString;
}
当我在表单加载中添加关注代码时
MessageBox.Show(Query.ToString());
我收到此错误:等于后找不到。
也许我错了。
答案 0 :(得分:3)
查询And UserID =
中的sql代码是"Incorrect syntax near '='"
添加对空字符串的检查以解决此问题:
if (cmbUserCreator.SelectedIndex != 0 && !string.IsNullOrEmpty(cmbUserCreator.SelectedValue))
{
SearchString += " And UserID = " + cmbUserCreator.SelectedValue;
}
答案 1 :(得分:0)
看起来您的值cmbUserCreator.SelectedValue为“”,但SQL需要一个数字
答案 2 :(得分:0)
您的组合选择的值为空(cmbUserCreator.SelectedValue =“”),这会使您的查询崩溃。
请先验证您的参数,然后再按照罗马·马鲁西克(Roman Marusyk)的要求发送给他们。