Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am using the following codes to save data which have been checked in a checkedlistbox
into sql db. Now i want select the saved data and checked them in the checklistbox. Please help
me out

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

            string StrQuery;

            foreach (var checkedItem in this.cblMinistries.CheckedItems)
            {
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                StrQuery = @"INSERT INTO tblMembershipministry VALUES ('" + this.txtMembershipid2.Text + "', '" + checkedItem + "')";

                comm.CommandText = StrQuery;
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
            //MessageBox.Show("Record was successfully Added");
        }


am trying to use this code but not working. help me out

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

            string StrQuery;

            foreach (var checkedItem in this.cblMinistries.CheckedItems)
            {
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                StrQuery = @"SELECT * FROM tblMembershipministry WHERE FormattedMembershipid = '" + this.txtMembershipid2.Text + "'";

                comm.CommandText = StrQuery;
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
            //MessageBox.Show("Record was successfully Added");
        }
Posted
Updated 7-May-14 13:11pm
v2
Comments
Maciej Los 7-May-14 13:08pm    
Not enough information. Please, post sample data. Use "Improve question" widget.
[no name] 7-May-14 20:20pm    
It's "not working" because you are not doing anything with the result of your query. You need to ExecuteReader not ExecuteNonQuery. And you are opening yourself up to SQL injection attacks. And why on earth are you executing your query for every checked item? You are just guessing at what you are doing.

1 solution

You need to write a SELECT statement to pull the data from the database. Then you loop through your data, probably using a DataReader, and set the appropriate checkboxes.
 
Share this answer
 
Comments
mikeoabban 7-May-14 19:13pm    
my problem is how to loop through and check them in the checklistbox.
please have a look at the improved question
ZurdoDev 7-May-14 20:02pm    
You need to think out it first.

1. Run your sql
2. Loop through the records
3. Find the matching checkbox list item and check it.
mikeoabban 7-May-14 20:20pm    
thanks for the help. am using this codes but not working. what might be problem

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

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

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

if (dr.Read())
{

foreach (int i in cblMinistries.CheckedIndices)
{
cblMinistries.SetItemCheckState(i, CheckState.Checked);
}
}



}

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