Link to home
Start Free TrialLog in
Avatar of APPIREDDY
APPIREDDY

asked on

Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'.

I am developing a c# standalone appln ,in which depending on the combobox selecte value i have to display data in the datagrid view.
i am using mysql database
i'm getting a runtime error at
 dAdapter2.Fill(ds, "cl2");
in  private void clNamecomboBox_SelectedIndexChanged(object sender, EventArgs e)
pls help me to find a sol'n
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 DataBinding
{
    
 
    public partial class Form1 : Form
    {
        MySqlConnection conn;
        MySqlDataAdapter dAdapter1;
        MySqlDataAdapter dAdapter2;
        DataSet ds;
        BindingSource bSource;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
           
            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 from client_mst";
            conn = null;
 
 
            try
            {
                conn = new MySqlConnection(connString);
                conn.Open();
 
                 dAdapter1 = new MySqlDataAdapter(sql, conn);
                MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(dAdapter1);
                ds = new DataSet();
                dAdapter1.Fill(ds,"Cl");
                bSource = new BindingSource();
                bSource.DataSource = ds.Tables["Cl"];
                clNamecomboBox.DataSource = bSource;
                MessageBox.Show(conn.Database);
                
                clNamecomboBox.ValueMember = "id";
                clNamecomboBox.DisplayMember = "client";
                
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error"+ex);
            }
        }
 
        private void clNamecomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql1 = @"select notes_date,notes_message 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;
         
            dAdapter2.Fill(ds, "cl2");
            BindingSource bSource2 = new BindingSource();
            bSource2.DataSource = ds.Tables["cl2"];
            dataGridView1.DataSource = bSource2;
        }
        
    }
}

Open in new window

Avatar of tculler
tculler
Flag of United States of America image

I'm guessing the error is coming from this line:
dAdapter2.SelectCommand.Parameters["@cid"].Value = clNamecomboBox.SelectedValue;

The error is telling you that the object passed to the Value property did not implement the interface System.IConvertible. In other words, it couldn't be "converted" to a satisfactory String representation. The SelectedValue property returned a DataViewRow. Maybe use clNamecomboBox.SelectedValue.Value?
Avatar of APPIREDDY
APPIREDDY

ASKER

if i change that line to
dAdapter2.SelectCommand.Parameters["@cid"].Value = Convert.ToInt32clNamecomboBox.SelectedValue);
i'm still getting the error.
cid  is an integer in database

clNamecomboBox.SelectedValue.Value doesn't exist
ASKER CERTIFIED SOLUTION
Avatar of APPIREDDY
APPIREDDY

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial