user3615514
user3615514

Reputation: 1

resolving error message Unable to cast object to type 'System.String' to type 'System.Data.DataRowView'

I have a form where the system administrator enters data into a tblministries table (e.g. A B C D E F) using save button. when the form loads, A B C D E F loads into the checkedlistbox (cblMinistries). a user can check any of the data in the checkedlistbox (e.g. B C F) Which is then saved into the tblMembershipministry table with the users member ID. When a member enter his ID and click the search button, the form should load A B C D E F into the checkedlistbox and any data with the same member ID from the tblMembershipministry table (B C F) should be checked in the checklistbox. My problem is i keep getting the error “Unable to cast object to type 'System.String' to type 'System.Data.DataRowView'” on this line of code “DataRowView row = (DataRowView)this.cblMinistries.Items[i];” please help me out

code for populating the checklistbox

public void PopuMinistry()
    {
        SqlConnection conn = new SqlConnection("Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike");
        if (conn.State != ConnectionState.Open)
        {
            conn.Open();
        }
        string SqlDataPull = ("select Homecellname from tblministries order by Homecellname");

        SqlCommand cmd = new SqlCommand(SqlDataPull);
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        SqlDataReader dr = cmd.ExecuteReader();


        while (dr.Read())
        {
            SqlDataPull = dr[0].ToString();
            cblMinistries.Items.Add(SqlDataPull);
        }
        dr.Close();
    }

code for marking checklistbox items as checked

public void minsearch()
    {
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=PIWCDB;User ID=sa;Password=mike";
        conn.Open();

        string sqlQuery = "SELECT Ministryname FROM tblMembershipministry WHERE FormattedMembershipid = '" + this.txtMembershipid.Text + "'";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = sqlQuery;
        cmd.CommandType = System.Data.CommandType.Text;

        try
        {
            System.Data.SqlClient.SqlDataReader dr = null;
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                for (int i = 0; i < cblMinistries.Items.Count; i++)
                {
                    DataRowView row = (DataRowView)this.cblMinistries.Items[i];

                    if (row[cblMinistries.ValueMember].ToString() == dr["Ministryname"].ToString())
                    {
                        cblMinistries.SetItemChecked(i, true);
                    }                     
                }
            }

            dr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Upvotes: 0

Views: 1720

Answers (2)

Hawkle
Hawkle

Reputation: 11

string SqlDataPull = ("select Homecellname from tblministries order by Homecellname");
...
cblMinistries.Items.Add(SqlDataPull);

thus,

DataRowView row = (DataRowView)this.cblMinistries.Items[i] 

is trying to convert a string to DataRowView.

Perhaps the following will work:

string ministryValue = this.cblMinistries.Items[i] ;
if (ministryValue  == dr["Ministryname"].ToString())
                    {
                        cblMinistries.SetItemChecked(i, true);
                    }      

Upvotes: 1

Me.Name
Me.Name

Reputation: 12544

The checked listbox is not populated with datarows, but with strings of the first column returned by the reader (Homecellname).

That means, you can cast the item to a string directly and provided that you need that value (you use cblMinistries.ValueMember in your code, I'm assuming that it refers to Homecellname)

 string tofind = dr["Ministryname"].ToString();
 for (int i = 0; i < cblMinistries.Items.Count; i++)
 {
      string ministry = (string)this.cblMinistries.Items[i];

      if (ministry  == tofind)
      {
           cblMinistries.SetItemChecked(i, true);
      }                     
  }

Upvotes: 0

Related Questions