Windows Application Error: OleDbCommand.Prepare method requires all parameters to have an explicitly set type

Last Reply 2 months ago By dharmendr

Posted 2 months ago
Hello, i want to update record ( based on condtion in sql query ) from DataGridView. i used the update method and i tried some code but it shows me below error on update method
 
OleDbCommand.Prepare method requires all parameters to have an explicitly set type.
       OleDbDataAdapter da;  
       DataSet ds = null;  
       BindingSource bsource = new BindingSource();  
  
       public void BindData()  
       {  
  
           OleDbCommand cmd = new OleDbCommand("select srno,AccNumber,Admission_Fee,Family_fund,MonthlyCollection,MonthlyDeposit,Fund,SimpleLoanBal,SimpleInstallment,SimpleInt,UrgentLoanBal,UrgentInstallment,UrgentInt,EducationLoanBal,EducationInstallment,EducationInt,GuarantorInstallment,GuarantorInt,OtherLoanBal,OtherInstallment,OtherInt,BankLoanBal,BankInstallment,BankInt,TotalInstallment,OtherLoanCaption from transDemand where IssueDate=@IssueDate order by AccNumber Desc", con);  
           cmd.Parameters.AddWithValue("@IssueDate", datesearch.Value.ToString("dd-MM-yyyy"));  
           da = new OleDbDataAdapter(cmd);  
           ds = new DataSet();  
           OleDbCommandBuilder cmdbuild = new OleDbCommandBuilder(da);  
           da.Fill(ds, "transDemand");  
           bsource.DataSource = ds.Tables["transDemand"];  
           dataGridView1.DataSource = bsource;  
  
  
       private void btnupdate_Click(object sender, EventArgs e)  
       {  
  
           DataTable dt = ds.Tables["transDemand"];  
           this.dataGridView1.BindingContext[dt].EndCurrentEdit();  
           this.da.Update(dt);  
  
           BindData();  
       }
Posted 2 months ago Modified on 2 months ago

Hi ajitn4u,

Check the below example.

Code

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace _AccessDatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        OleDbDataAdapter da;
        DataSet ds = null;
        BindingSource bsource = new BindingSource();
        public void BindData()
        {
            string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Dharmendra\\Desktop\\Test.accdb;Persist Security Info = False; ";
            OleDbConnection con = new OleDbConnection(str);
            OleDbCommand cmd = new OleDbCommand("select * from transDemand where IssueDate=@IssueDate order by AccNumber Desc", con);
            cmd.Parameters.Add("@IssueDate", OleDbType.Date).Value = datesearch.Value.ToString("MM/dd/yyyy");
            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            OleDbCommandBuilder cmdbuild = new OleDbCommandBuilder(da);
            da.Fill(ds, "transDemand");
            bsource.DataSource = ds.Tables["transDemand"];
            dataGridView1.DataSource = bsource;
        }
        private void btnupdate_Click(object sender, EventArgs e)
        {
            DataTable dt = ds.Tables["transDemand"];
            this.dataGridView1.BindingContext[dt].EndCurrentEdit();
            this.da.Update(dt);
            BindData();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }
    }
}

Screenshot