Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Unable to cast object of type system.data.datarowview to typ system.iconvertible:

C#
public void Bind()
{
    try
    {
        string query = @"select ProductMaster.id,ProductMaster.pName,ProductDetails.Modelno,ProductDetails.Price,ProductDetails.Quantity from ProductMaster, ProductDetails";
        sda = new SqlDataAdapter(query, conn);
        dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            dataGridView1.DataSource = dt;
        }
        dataGridView1.Columns[0].Visible = false;//here i got an error(index out of bound)
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Posted
Updated 23-Apr-15 23:16pm
v3
Comments
Leo Chapiro 24-Apr-15 5:19am    
I don't think that the error comes from here: dataGridView1.Columns[0].Visible = false;
Please check twice by debugiing!
Member 11057488 24-Apr-15 5:34am    
private void button1_Click(object sender, EventArgs e)
{

try
{
conn.Open();

cmd = new SqlCommand(@"INSERT INTO [pix].[dbo].[ProductDetails] ([ProductID],[Modelno],[Price],[Quantity]) VALUES ('" + Convert.ToInt32(comboBox1.SelectedItem) + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", conn);// here i got this error unable to cast object of type system.data.datarowview to typ system.iconvertible
int i = 0;
i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Rows inserted successfully");
}
else
{
MessageBox.Show("No rows inserted");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
conn.Close();
}
finally
{
conn.Close();
Bind();
}
cleardata();

}
Leo Chapiro 24-Apr-15 5:37am    
What is that now? Another one issue or the same one?
Member 11057488 24-Apr-15 5:43am    
same issue actually unable to cast the oject error is thown on this code and in the first code the exception thrown is index out of bound
aarif moh shaikh 24-Apr-15 6:19am    
use comboBox1.SelectedItem.text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900