Parameter not found in the collection.

Hi Experts,

I am developing a standalone appln.I got a datagridview which contains two columns from a table client_notes which contains fields(notesid 'auto incr field',clid,date,notes)

i want to insert data back to the db. i' want to take the clid field value from combobox.

when i'am inserting data i'am getting the error
Parameter '@pId' not found in the collection

pls help me to fix the prob

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
 
namespace Easypeasy_Office_Management
{
    public partial class easypeasyForm : Form
    {
        MySqlConnection conn;
        MySqlDataAdapter dAdapter1;
        MySqlDataAdapter dAdapter2;
        DataSet ds, ds1;
        BindingSource bSource;
        MySqlCommandBuilder cBuilder;
        public easypeasyForm()
        {
            InitializeComponent();
            
        }
 
 
        private void btnNotes_Click(object sender, EventArgs e)
        {
            notesPanel.Enabled = true;
            notesPanel.Visible = true;
            string connString = @"server=localhost;database=abc;user id=root;password=kodanda;";
            //string sql = @"select client_mst.client_id as id,Concat(client_no,'   ' ,client_name) as client,notes_message as Notes,notes_date as `Date` from client_mst,client_notes  where client_mst.client_id=client_notes.client_id";
            string sql = @"select client_mst.client_id as id,Concat(client_no,'   ' ,client_name) as client from client_mst ";
            conn = null;
 
 
            try
            {
                conn = new MySqlConnection(connString);
                conn.Open();
 
                dAdapter1 = new MySqlDataAdapter(sql, conn);
                cBuilder = new MySqlCommandBuilder(dAdapter1);
                ds = new DataSet();
                dAdapter1.Fill(ds, "Cl");
                bSource = new BindingSource();
                bSource.DataSource = ds.Tables["Cl"];
                clNamecomboBox.DisplayMember = "client";
                clNamecomboBox.ValueMember = "id";
                clNamecomboBox.DataSource = bSource;
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
        }
        private void clNamecomboBox_SelectedValueChanged(object sender, EventArgs e)
        {
 
            string sql1 = @"select client_id,notes_date as Date,notes_message as Notes from client_notes where client_id=?cid";
            dAdapter2 = new MySqlDataAdapter(sql1, conn);
 
            dAdapter2.SelectCommand.Parameters.Add("cid", MySqlDbType.Int32);
            dAdapter2.SelectCommand.Parameters["cid"].Value = clNamecomboBox.SelectedValue;
            cBuilder = new MySqlCommandBuilder(dAdapter2);
            ds1 = new DataSet();
            dAdapter2.Fill(ds1, "cl2");
            BindingSource bSource2 = new BindingSource();
            bSource2.DataSource = ds1.Tables["cl2"];
            dataGridViewNotes.AutoGenerateColumns = false;
            dataGridViewNotes.DataSource = bSource2;
      //      MessageBox.Show(cBuilder.GetInsertCommand().CommandText);
 
 
 
 
        }
 
        private void btnAdd_Click(object sender, EventArgs e)
        {
           // this.dataGridViewNotes.Rows.Add();
            MySqlCommand inCommand = new MySqlCommand("insert into client_notes(`notes_date`,`notes_message`,`client_id`) values(@pDate,@pMessage,@pId)",dAdapter2.SelectCommand.Connection);
            inCommand.Parameters.Add("@pDate", MySqlDbType.Date);
            inCommand.Parameters["@pDate"].SourceVersion = DataRowVersion.Current;
            inCommand.Parameters["@pDate"].SourceColumn = "Date";
 
            inCommand.Parameters.Add("@pMessage", MySqlDbType.VarChar);
            inCommand.Parameters["@pDate"].SourceVersion = DataRowVersion.Current;
            inCommand.Parameters["@pDate"].SourceColumn = "Notes";
            inCommand.Parameters["@pId"].Value = this.clNamecomboBox.SelectedValue;
            dAdapter2.UpdateCommand = inCommand;
            dAdapter2.Update(ds1, "cl2");
            //dAdapter2.Update(ds1.Tables["cl2"]);
        }
 
        
        
    }
}

Open in new window

APPIREDDYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

APPIREDDYAuthor Commented:
sorry that's not my question.
When i am inserting data i'm getting error
Field 'client_id' doesn't have a default value
0
nkhelashviliCommented:
// this.dataGridViewNotes.Rows.Add();
            MySqlCommand inCommand = new MySqlCommand("insert into client_notes(`notes_date`,`notes_message`,`client_id`) values(@pDate,@pMessage,@pId)",dAdapter2.SelectCommand.Connection);
            inCommand.Parameters.Add("@pDate", MySqlDbType.Date);
            inCommand.Parameters["@pDate"].SourceVersion = DataRowVersion.Current;
            inCommand.Parameters["@pDate"].SourceColumn = "Date";
 
 5:        inCommand.Parameters.Add("@pMessage", MySqlDbType.VarChar);
            inCommand.Parameters["@pDate"].SourceVersion = DataRowVersion.Current;
            inCommand.Parameters["@pDate"].SourceColumn = "Notes";
            inCommand.Parameters["@pId"].Value = this.clNamecomboBox.SelectedValue;
            dAdapter2.UpdateCommand = inCommand;
            dAdapter2.Update(ds1, "cl2");
            //dAdapter2.Update(ds1.Tables["cl2"]);



here is an error in your code, after the line 5
0
APPIREDDYAuthor Commented:
i HAVE XHANGED THE CODE
 private void btnAdd_Click(object sender, EventArgs e)
        {
        this.dataGridViewNotes.AllowUserToAddRows = true;
            try
            {
                 conn = new MySqlConnection(connString);
                  conn.Open();

                MySqlCommand inCommand = new MySqlCommand("insert into client_notes(`notes_date`,`notes_message`,`client_id`) values(@pDate,@pMessage,@pId)", conn);
               

               
                inCommand.Parameters.Add("@pDate", MySqlDbType.Date);
                inCommand.Parameters["@pDate"].SourceVersion = DataRowVersion.Current;
               inCommand.Parameters["@pDate"].SourceColumn = "Date";
               

                inCommand.Parameters.Add("@pMessage", MySqlDbType.VarChar);
                inCommand.Parameters["@pMessage"].SourceVersion = DataRowVersion.Current;
                inCommand.Parameters["@pMessage"].SourceColumn = "Notes";

                inCommand.Parameters.Add("@pId", MySqlDbType.Int32);
                inCommand.Parameters["@pId"].Value = this.clNamecomboBox.SelectedValue;

                inCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("er" + ex);
            }
            finally
            {
                conn.Close();
            }
           
STILL I'M GETTING THE ERROR
I'M NOT SURE HOW TO ACCESS THE CURRENT ROW VALUES OF DATAGRIDVIEW CONTROL
I AM ATTACHING THE ERROR MESSAGE
0
nkhelashviliCommented:
datagridview1.CurrentRow.Cells["columnname"].Value    is value of field "columnname" in the current row
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.