Solved

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

Posted on 2009-05-20
4
1,492 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:APPIREDDY
  • 3
4 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24432265
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?
0
 

Author Comment

by:APPIREDDY
ID: 24432354
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

0
 

Author Comment

by:APPIREDDY
ID: 24432382
clNamecomboBox.SelectedValue.Value doesn't exist
0
 

Accepted Solution

by:
APPIREDDY earned 0 total points
ID: 24449555
I have changed the order to
clNamecomboBox.DisplayMember = "client";
                clNamecomboBox.ValueMember = "id";
                clNamecomboBox.DataSource = bSource;
then I got it right
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Security 2 29
application restarter  - for an exe that hangs 9 31
What are the simplest ways to create a Merge Sort? 3 29
Input parameteres to DragOver 2 20
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question