您好,当我单击按用户名搜索时,我试图添加一个搜索按钮,只显示我搜索过的那个
private void Update_Click(object sender, EventArgs e)
{
conect.Open();
OleDbCommand command = conect.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "select * from Sign_Up where UserName='"+Username.Text+"'";
command.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
da.Fill(dt);
dataGridView1.DataSource = dt;
conect.Close();
}
这是我的程序图片Pic 这是我的错误`System.InvalidOperationException:'在调用'Fill'之前,尚未初始化SelectCommand属性。'
使用此代码da.Fill(dt);
答案 0 :(得分:1)
您需要将SelectCommand
的{{1}}属性设置为
OleDbDataAdapter
或者您可以通过构造函数传递命令
conect.Open();
OleDbCommand command = conect.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "select * from Sign_Up where UserName='"+Username.Text+"'";
//command.ExecuteNonQuery(); no need to execute command manually
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = command; //add this line
da.Fill(dt);
dataGridView1.DataSource = dt;
conect.Close();