Solved

Populate checkboxlist with items from database using linq to sql?

Posted on 2014-12-29
6
142 Views
Last Modified: 2016-02-16
Hi:
My table : tblapplicationType
Fields are: Id & applicationType.
Can I Populate checkboxlist with items from tblapplicationType
My context :
dbDataContext db = new dbDataContext();
Would you please, help me writing the suitable code doing so.
Thanks in advance.
0
Comment
Question by:Mohammad Alsolaiman
  • 3
  • 2
6 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 100 total points
ID: 40523652
You should be able to use the DataSource property to set the items for the CheckBoxList, with any source that is IEnumerable.
0
 

Author Comment

by:Mohammad Alsolaiman
ID: 40523685
IEnumerable! sorry
I'm newbie to c# . I'll watch some videos on that, but acn any one help with the code step by step. please
thanks Bob Learned .
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 400 total points
ID: 40523962
Understand that this is a very rudimentry and simple example based on the provided information:
using System;
using System.Windows.Forms;

namespace EE_Q28587937
{
	public partial class Form1 : Form
	{
		private BindingSource source = null;
		private dbDataContext context = null;
		public Form1()
		{
			InitializeComponent();
			if (context == null)
				context = new dbDataContext();
			if (source == null)
				source = new BindingSource();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			source.DataSource = context.GetTable(typeof(tblApplicationType));
			checkedListBox1.DataSource = source;
			checkedListBox1.DisplayMember = "ApplicationType";
			checkedListBox1.ValueMember = "ID";
		}

		private void OnItemCheck(object sender, ItemCheckEventArgs e)
		{
			MessageBox.Show(string.Format("You have {0} {1}", e.NewValue == CheckState.Checked ? "checked" : "unchecked", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
		}
	}
}

Open in new window

Produces the following output -Capture.JPGAnd when you check an item -Capture.JPGAnd then uncheck the item -Capture.JPG-saige-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Mohammad Alsolaiman
ID: 40526721
i try it , but i get this error.
The type or namespace name 'tblapplicationTypes' could not be found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        dbDataContextDataContext dbDataContext = new dbDataContextDataContext();
        private BindingSource source = null;
        private dbDataContextDataContext context = null;
        public Form1()
        {
            InitializeComponent();
            if (context == null)
                context = new dbDataContextDataContext();
            if (source == null)
                source = new BindingSource();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            source.DataSource = context.GetTable(typeof(tblapplicationTypes));
            checkedListBox1.DataSource = source;
            checkedListBox1.DisplayMember = "ApplicationType";
            checkedListBox1.ValueMember = "ID";
        }

        private void OnItemCheck(object sender, ItemCheckEventArgs e)
        {
            MessageBox.Show(string.Format("You have {0} {1}", e.NewValue == CheckState.Checked ? "checked" : "unchecked", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
        }
    }
}

Open in new window

please help.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 400 total points
ID: 40526865
As I stated, this example was created based on the provided information.  So the types and definitions that I used in mine will be different from yours.  What you need to do is open the designer file for your dbDataContextDataContext and look at the table definition.  There are a few ways to do this:

1. In solution explorer, locate the the object that defines your data context and open the designer.

My data context object is DataClasses.dbml.I can right-click on the DBML and choose 'Open' or 'View Designer'.In the designer, locate the table that represents the table for your application types.  Here is mine.  The highlighted portion is what get's placed in the typeof method.-Or-

2. In solution explorer, locate the the object that defines your data context and open the Designer file.

My data context object is DataClasses.dbml.I right-click on the DataClasses.designer.cs file and choose 'Open' or 'View Code'.Selecting the drop down that provides the class definitions, I can find my table definition here.  Once again, this is what gets put into the typeof method.The DataContext class also provides a method that returns the table so that we would not have to worry about the line:
source.DataSource = context.GetTable(typeof(tblApplicationType));

Open in new window

Instead we could call this shortcut method on the context itself:Capture.JPGCapture.JPGSo we could change our code to -
source.DataSource = context.tblApplicationTypes;

Open in new window

For completeness here is the full *edited* code for Form1.cs -
using System;
using System.Windows.Forms;

namespace EE_Q28587937
{
	public partial class Form1 : Form
	{
		private BindingSource source = null;
		private dbDataContext context = null;
		public Form1()
		{
			InitializeComponent();
			if (context == null)
				context = new dbDataContext();
			if (source == null)
				source = new BindingSource();
		}

		private void OnLoad(object sender, EventArgs e)
		{
			source.DataSource = context.tblApplicationTypes;
			checkedListBox1.DataSource = source;
			checkedListBox1.DisplayMember = "ApplicationType";
			checkedListBox1.ValueMember = "ID";
		}

		private void OnItemCheck(object sender, ItemCheckEventArgs e)
		{
			MessageBox.Show(string.Format("You are {0} {1}", e.NewValue == CheckState.Checked ? "checking" : "unchecking", (checkedListBox1.Items[e.Index] as tblApplicationType).ApplicationType));
		}
	}
}

Open in new window

-saige-
0
 

Author Closing Comment

by:Mohammad Alsolaiman
ID: 40551208
thanks very much to all of you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now