Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am having difficulty Checking Items in a CheckedBoxList based on Database Data for some weeks now.
i tried this code but am getting the error " Cannot apply indexing with [] to an expression of type 'System.Windows.Forms.CheckedListBox' " from this line of code "DataRowView row = (DataRowView)this.cblMinistries[i];" please help me out
codes
C#
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[i];
                if (row[cblMinistries.ValueMember].ToString() == dr["FormattedMembershipid"].ToString())
                {
                    cblMinistries.SetItemChecked(i, true);
                }
            }
        }
        dr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Posted
Updated 16-May-14 6:22am
v2
Comments
[no name] 16-May-14 12:21pm    
I believe that you want "cblMinistries.Items[i];"
mikeoabban 16-May-14 12:25pm    
yes. that is where the error is coming from
mikeoabban 16-May-14 12:26pm    
can you help me out
[no name] 16-May-14 12:32pm    
Did you change your code yet?

change
C#
DataRowView row = (DataRowView)this.cblMinistries[i];
to
C#
DataRowView row = (DataRowView)this.cblMinistries.Items[i];


And also in next line you are check the value member of the item with dr["FormattedMembershipid"] but you are not selecting such column in your select statement. since you have used this column value in where condition you can directly check as below

C#
if (row["ValueMembercolumnName"].ToString() == this.txtMembershipid.Text)
{

}
 
Share this answer
 
v2
Comments
mikeoabban 16-May-14 12:46pm    
i tried it but got the error "Unable to cast object to type 'System.String' to type 'System.Data.DataRowView'." what might be the problem
DamithSL 16-May-14 12:49pm    
how you bind cblMinistries? i need to see the code
mikeoabban 16-May-14 15:29pm    
this is the code i used to bind cblMinistries
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();
}
DamithSL 16-May-14 22:33pm    
please explain your requirement, you have two tables tblministries and tblMembershipministry, what is the relationship between those two tables?
when you binding cblMinistries, you selected Homecellname column , is it same as Ministryname in tblMembershipministry table?
mikeoabban 17-May-14 0:50am    
no my form the system administrator enters data into the 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 saved into the tblMembershipministry table with the 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 member ID from the tblMembershipministry table (B C F) should be checked.
please how do i resolve the problem. thank you

CREATE TABLE tblMinistries
(Homecellid int IDENTITY constraint pkMinid primary key clustered,
Homecellname varchar(300)not null)

CREATE TABLE tblMembershipministry
(FormattedMembershipid varchar(300),
Ministryname varchar(300)not null)
You can compare the database value with checkboxlist items as following:

C#
while (dr.Read())
 {
    foreach (ListItem li in cblMinistries.Items)
    {
       if (li.Value == dr["FormattedMembershipid"].ToString())
          li.Selected = true;
    }
 }
 
Share this answer
 
Comments
mikeoabban 17-May-14 0:54am    
i am using a windows form in c# and ListItem is generating an error

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



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