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

    Dotaz

  •    I am using the code below to view list of car brands below car makes but while loading i get the above error and i guess it's because the car make value is null during loading. I tried to use if (cboVehicleMake.SelectedItem == null) but still getting the same error.


    what can i do?



    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Data.SqlClient;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace Rent_A_Car

    {

        public partial class frmMain : Form

        {

            SqlConnection sql_connection = null;

            SqlCommand sql_command = null;

            SqlDataAdapter sql_adapter;

            // DataTable data_table = null;

            SqlDataReader sql_reader = null;

            DataSet data_set = null;

     

            public frmMain()

            {

                InitializeComponent();

            }

     

            private void hide_panels()

            {

                panelVehicles.Visible = false;

                panelVehicleDetails.Visible = false;

            }

     

            private void get_vehicle_makes()

            {

                try

                {

                    sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=rent_a_car_company;integrated security=true;");

                    sql_connection.Open();

     

                    sql_command = new SqlCommand("sp_get_vehicle_makes", sql_connection);

                    sql_command.CommandType = CommandType.StoredProcedure;

     

                    // Create data adapter object

                    sql_adapter = new SqlDataAdapter();

                    sql_adapter.SelectCommand = sql_command;

     

                    // Create a dataset object and fill with data using data adapter's Fill method

                    data_set = new DataSet();

                    sql_adapter.Fill(data_set, "vehicle_makes");

     

                    // Attach dataset's DefaultView to the combobox

                    cboVehicleMake.DataSource = data_set.Tables["vehicle_makes"].DefaultView;

                    cboVehicleMake.DisplayMember = "vehicle_make";

                    cboVehicleMake.ValueMember = "vehicle_make_id";

     

                    cboVehicleMake.SelectedValue = -1;

                }

                // catch (Exception oE)

                // {

                // MessageBox.Show("Problem Populating Reader Box: [" + oE.ToString() + "]");

                // }

                finally

                {

                    if (sql_reader != null) sql_reader.Close();

                    // if (sql_connection != null)

                    // {

                    // if (sql_connection.State == ConnectionState.Open)

                    // sql_connection.Close();

                    // }

                }

            }

     

            private void get_vehicle_brands_by_make()

            {

                try

                {

                    sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=rent_a_car_company;integrated security=true;");

                    sql_connection.Open();

     

                    sql_command = new SqlCommand("sp_get_vehicle_brands_by_make", sql_connection);

                    sql_command.CommandType = CommandType.StoredProcedure;

                    sql_command.Parameters.Add("@vehicle_make", SqlDbType.Int).Value = Convert.ToInt32(cboVehicleMake.SelectedValue);

     

                    // Create data adapter object

                    sql_adapter = new SqlDataAdapter();

                    sql_adapter.SelectCommand = sql_command;

     

                    // Create a dataset object and fill with data using data adapter's Fill method

                    data_set = new DataSet();

                    sql_adapter.Fill(data_set, "vehicle_brands");

     

                    // Attach dataset's DefaultView to the combobox

                    cboVehicleBrands.DataSource = data_set.Tables["vehicle_brands"].DefaultView;

                    cboVehicleBrands.DisplayMember = "vehicle_brand";

                    cboVehicleBrands.ValueMember = "vehicle_brand_id";

     

                    cboVehicleBrands.SelectedValue = -1;

                }

                // catch (Exception oE)

                // {

                // MessageBox.Show("Problem Populating Reader Box: [" + oE.ToString() + "]");

                // }

                finally

                {

                    if (sql_reader != null) sql_reader.Close();

                    // if (sql_connection != null)

                    // {

                    // if (sql_connection.State == ConnectionState.Open)

                    // sql_connection.Close();

                    // }

                }

            }

     

            private void toolExit_Click(object sender, EventArgs e)

            {

                Application.Exit();

            }

     

            private void frmMain_Load(object sender, EventArgs e)

            {

     

            }

     

            private void navNewVehicle_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)

            {

                if (panelVehicleDetails.Visible != true)

                {

                    // hide all panels;

                    hide_panels();

     

                    get_vehicle_makes();

     

                   

                    // panelVehicleDetails.Width = 635;

                    // panelVehicleDetails.Height = 435;

                    // panelVehicleDetails.Top = 200;

                    // panelVehicleDetails.Left = 52;

                    panelVehicleDetails.Location = new System.Drawing.Point(navMain.Width + 5, navMain.Top);

                    panelVehicleDetails.Size = new Size(this.Width - navMain.Width - 40, navMain.Height);

                    panelVehicleDetails.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;

                    panelVehicleDetails.Visible = true;

     

                    /*

                    Control control_name = panelVehicleDetails;

                    control_name.Name = "panelVehicleDetails";

                    control_name.Width = 635;

                    control_name.Height = 435;

                    control_name.Top = 200;

                    control_name.Left = 52;

                     * */

                }

            }

     

            private void navOurVehicles_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)

            {

                hide_panels();

            }

     

            private void cboVehicleMake_SelectedIndexChanged(object sender, EventArgs e)

            {

                if (cboVehicleMake.SelectedItem == null)

                {

                    return;

                }

                else

                {

                    get_vehicle_brands_by_make();

                }

            }

        }

    }


    Jassim Rahma
    23. srpna 2008 0:30

Odpovědi

  • ok i found the solution.

    just change the order from:

    // Attach dataset's DefaultView to the combobox

    cboVehicleMake.DataSource = data_set.Tables["vehicle_makes"].DefaultView;

    cboVehicleMake.DisplayMember = "vehicle_make";

    cboVehicleMake.ValueMember = "vehicle_make_id";



    to:

    // Attach dataset's DefaultView to the combobox

    cboVehicleMake.DisplayMember = "vehicle_make";

    cboVehicleMake.ValueMember = "vehicle_make_id";

    cboVehicleMake.DataSource = data_set.Tables["vehicle_makes"].DefaultView;


    Jassim Rahma
    27. srpna 2008 14:32

Všechny reakce

  •  

    please help..


    Jassim Rahma
    23. srpna 2008 15:14
  • Please debug the code and tell us which line you get the error.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    27. srpna 2008 10:14
  • ok i found the solution.

    just change the order from:

    // Attach dataset's DefaultView to the combobox

    cboVehicleMake.DataSource = data_set.Tables["vehicle_makes"].DefaultView;

    cboVehicleMake.DisplayMember = "vehicle_make";

    cboVehicleMake.ValueMember = "vehicle_make_id";



    to:

    // Attach dataset's DefaultView to the combobox

    cboVehicleMake.DisplayMember = "vehicle_make";

    cboVehicleMake.ValueMember = "vehicle_make_id";

    cboVehicleMake.DataSource = data_set.Tables["vehicle_makes"].DefaultView;


    Jassim Rahma
    27. srpna 2008 14:32
  • Jassim (or anyone else), why does this make a difference???  What's going on here?

    Rod
    30. července 2009 20:12