Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C#
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
Edited 29-Dec-11 4:26am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Mika Wendelius at 29-Dec-11 10:38am
   
Good links, 5'ed
Uday P.Singh at 29-Dec-11 10:45am
   
thank you Mika :)
thatraja at 29-Dec-11 14:49pm
   
5!
Uday P.Singh at 30-Dec-11 0:46am
   
thank you raja :)
RaviRanjankr at 29-Dec-11 18:23pm
   
Nice Link, My 5+
Uday P.Singh at 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 at 29-Dec-11 10:46am
   
good point of SQL injection 5ed
Mika Wendelius at 29-Dec-11 10:51am
   
Thanks Uday :)
LAKHANP22 at 29-Dec-11 10:57am
   
can any buddy can tell me exactly what i have to do.....?
Mika Wendelius at 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 at 29-Dec-11 14:50pm
   
5!
Mika Wendelius at 29-Dec-11 15:14pm
   
Thanks Thatraja :)
RaviRanjankr at 29-Dec-11 18:24pm
   
My 5+
Mika Wendelius at 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
Mika Wendelius at 30-Dec-11 1:38am
   
Well formulated, 5
RaviRanjankr at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 2 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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