Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code but...working.....?
C#
 SqlDataAdapter da = new SqlDataAdapter("SELECT NAME AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'", con);



                DataSet ds = new DataSet();
                da.Fill(ds, "AccountM ");
                checkedListBox1.DataSource = ds;

                checkedListBox1.SelectedValue = "AC_CODE";
                checkedListBox1.SelectedItem = "NAME";
}

I want show display member (Name);
and value member (AC_COde);

Can any buddy tell ....how to bind data in checkedlistbox from database?

Thanks in advanced

lakhan
Posted
Updated 29-Dec-11 4:26am
v2

private void FillCheckedListBox (CheckedListBox chkListBox, string sSql, string displayMember, string valueMember)
{
try {
SqlConnection con = new SqlConnection(DaoCodeGCommon.GetConstring());
con.Open();
DataTable accTable = new DataTable();
SqlCommand cmd = new SqlCommand(sSql, con);
SqlDataAdapter adpObj = new SqlDataAdapter(cmd);
accTable.TableName = "tbl";
adpObj.Fill(accTable);
con.Close();
chkListBox.DataSource = accTable;
chkListBox.ValueMember = valueMember;
chkListBox.DisplayMember = displayMember;
}
catch (Exception ex) {

throw ex;
}
}
 
Share this answer
 
Try
C#
private void FillCheckedListBox (CheckedListBox chkListBox, string sSql, string displayMember, string valueMember)
        {
            try {
                SqlConnection con = new SqlConnection(DaoCodeGCommon.GetConstring());
                con.Open();
                DataTable accTable = new DataTable();
                SqlCommand cmd = new SqlCommand(sSql, con);
                SqlDataAdapter adpObj = new SqlDataAdapter(cmd);
                accTable.TableName = "tbl";
                adpObj.Fill(accTable);
                con.Close();
                chkListBox.DataSource = accTable;
                chkListBox.ValueMember = valueMember;
                chkListBox.DisplayMember = displayMember;
            }
            catch (Exception ex) {

                throw ex;
            }
        }
 
Share this answer
 
Comments
Wendelius 30-Dec-11 1:38am    
Well formulated, 5
RaviRanjanKr 30-Dec-11 2:27am    
Thanks Mika :)
 
Share this answer
 
First, do not concatenate values directly to an SQL statement. Use parameters instead, see: SqlParameter[^].

About the binding use the DisplayMember[^] and ValueMember[^] properties to define the fields your binding should use.

Addition:

I think you should have a comma in your SQL statement. Try:
C#
SqlDataAdapter da = new SqlDataAdapter("SELECT NAME, AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'", con);


And remember to add parameter to your query:
C#
SqlDataAdapter da = new SqlDataAdapter("SELECT NAME, AC_CODE FROM AccountM where compcode=@compcode", con);
...
 
Share this answer
 
v2
Comments
Uday P.Singh 29-Dec-11 10:46am    
good point of SQL injection 5ed
Wendelius 29-Dec-11 10:51am    
Thanks Uday :)
[no name] 29-Dec-11 10:57am    
can any buddy can tell me exactly what i have to do.....?
Wendelius 29-Dec-11 13:37pm    
At a glance your code seems fine. What is the exact problem. Getting errors or something else.

One thing I noticed is that you're missing a comma in your query. See the updated answer.
thatraja 29-Dec-11 14:50pm    
5!
 
Share this answer
 
Comments
Wendelius 29-Dec-11 10:38am    
Good links, 5'ed
Uday P.Singh 29-Dec-11 10:45am    
thank you Mika :)
thatraja 29-Dec-11 14:49pm    
5!
Uday P.Singh 30-Dec-11 0:46am    
thank you raja :)
RaviRanjanKr 29-Dec-11 18:23pm    
Nice Link, My 5+

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