Click here to Skip to main content
13,591,539 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Here is my code but...working.....?
 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 29-Dec-11 4:22am
Updated 29-Dec-11 4:26am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
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+
Uday P.Singh 30-Dec-11 0:46am
   
thank you Ravi :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
SqlDataAdapter da = new SqlDataAdapter("SELECT NAME, AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'", con);


And remember to add parameter to your query:
SqlDataAdapter da = new SqlDataAdapter("SELECT NAME, AC_CODE FROM AccountM where compcode=@compcode", con);
...
  Permalink  
v2
Comments
Uday P.Singh 29-Dec-11 10:46am
   
good point of SQL injection 5ed
Wendelius 29-Dec-11 10:51am
   
Thanks Uday :)
Lakhan Aanjana 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!
Wendelius 29-Dec-11 15:14pm
   
Thanks Thatraja :)
RaviRanjanKr 29-Dec-11 18:24pm
   
My 5+
Wendelius 30-Dec-11 1:38am
   
Thanks Ravi :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try
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;
            }
        }
  Permalink  
Comments
Wendelius 30-Dec-11 1:38am
   
Well formulated, 5
RaviRanjanKr 30-Dec-11 2:27am
   
Thanks Mika :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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;
}
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.180618.1 | Last Updated 2 Jan 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100