简体   繁体   English

尝试将特定数据从数据库显示到 datagridview 时收到此错误

[英]Receving this error when trying to display specific data from database to datagridview

It works fine when i select all the data from the database and display it.当我从数据库中选择所有数据并显示它时,它工作正常。 But when i want to specifically display certain data from the database, that error would show up.但是当我想专门显示数据库中的某些数据时,就会出现该错误。 (System.Data.OleDb.OleDbException: 'No value given for one or more required parameters.' ) (System.Data.OleDb.OleDbException: '没有为一个或多个必需参数提供值。')

  private void button1_Click(object sender, EventArgs e)
  {
   string selectoledb = "Select * from items where Categories=Fitems";
   command = new OleDbCommand(selectoledb, connection);
   da = new OleDbDataAdapter(command);

    DataTable table = new DataTable();
    itemstxt.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    itemstxt.RowTemplate.Height = 120;
    itemstxt.AllowUserToAddRows = false;

    da.Fill(table);

    itemstxt.DataSource = table;

    DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
    imageColumn = (DataGridViewImageColumn)itemstxt.Columns[3];
    imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;

     DataGridViewButtonColumn button = new DataGridViewButtonColumn();
     button.HeaderText = "Buttons";
     button.Name = "button";
     button.Text = "Add to cart";
     button.UseColumnTextForButtonValue = true;
     itemstxt.Columns.Add(button);


     da.Dispose();

    }

you need to add parameter to your select您需要将参数添加到您的选择中

string selectoledb = "Select * from items where Categories=@Fitems";
command.Parameters.Add("@Fitems", SqlDbType.Varchar).Value = Fitems.Text; //or value you want to filter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM