How to get the key(id) of the binded checkListBox - c#

I am have a Windows Forms application and also using SQL to get the data from my db. I have a binded checkListBox in my Windows Forms application and I would like to get the primary key (the value) of all the checked Items in my checkListBox. What I realize is in combo box, they have a build in method 'selectedValue' I am wondering if we can do the same thing in checkListBox by looping through the checkedItems in checkListBox and get its value
Let say in my db table, I have:
EmployeeId Employee Name
1 Jimmy
2 Pam
3 Bob
4 Kobe
My binded checkListBox showed something like
[x]Jimmy
[x]Pam
[]Bob
[]Kobe
Can I get the checkedItem item's id by using sql query. Now let say Jimmy and Pam is checked and I want to get their employeeId. I am not sure how to do it. Help will be appreciated.
select * from employee_table where employeeId = '"+checkListBox.SelectedValue+"'
or
foreach(var item = item.CheckedItems{
select * from employee_table where employeeId = '"items"';
}

You don't need to go to database again to retrieve Id of your items.
You can create a class for items that contains an Id and a Name property and override ToString method to return the string that you want to show in CheckedListBox:
public class ItemModel
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
Then when loading data, you can select the output and shape it to ItemModel and then add these ItemModels to CheckedListBox:
private void Form_Load(object sender, EventArgs e)
{
var db = new TestDBEntities();
//Select items that you need and shape it to ItemModel
var list = db.Categories.Select(x => new ItemModel
{
Id = x.Id,
Name = x.Name
})
.ToList();
//We cast the list to object[] because AddRange method accept object[]
this.checkedListBox1.Items.AddRange(list.Cast<object>().ToArray());
}
Then when you need to know Id of checked items, you can simply cast each checked item to ItemModel an use its Id property:
private void button1_Click(object sender, EventArgs e)
{
this.checkedListBox1.CheckedItems.Cast<ItemModel>()
.ToList()
.ForEach(item =>
{
MessageBox.Show(string.Format("Id:{0}, Name:{1}", item.Id, item.Name));
});
}
Note:
If you use another method to connect to database, you can simply change this code to satisfy your requirements, for example this code uses ADO.Net objects to shape data to ItemModel:
private void CheckedListBoxSample_Load(object sender, EventArgs e)
{
var connection = #"data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;MultipleActiveResultSets=True;";
var command = "SELECT Id, Name From Categories";
var dataAdapter = new System.Data.SqlClient.SqlDataAdapter(command, connection);
var table = new DataTable();
dataAdapter.Fill(table);
var list = table.Rows.Cast<DataRow>()
.Select(row => new ItemModel
{
Id = row.Field<int>("Id"),
Name = row.Field<string>("Name")
})
.ToList();
this.checkedListBox1.Items.AddRange(list.Cast<object>().ToArray());
}

Something along these lines might help you.
Basically what it does, is that it builds a parameterized query, and then adds all the selected items using SqlParameters. Then using the reader, you are able to parse each returned record. The code below will probably need some modifications for you to use it, but should help you getting started.
Although I would strongly suggest using an ORM like Entity Framework. Using an ORM you don't have to build your own queries, and allows you to use strong typed classes to "talk" to your database.
var query = "SELECT * FROM employee_table WHERE imployeeId IN (#ids)";
var ids = String.Join(","
MyCheckBoxList.Items
.Cast<ListItem>()
.Where(x => x.Selected)
.Select(x => x.Value);
using (var connection = new SqlConnection(myConnectionString))
{
connection.Open();
using(var command = new SqlCommand(query, connection)
{
command.Parameters.Add("ids", ids);
var reader = command.ExecuteReader();
while(reader.Read())
{
//get all the needed data from the reader
}
}
}

Related

Linq into the array then find values in the array and list to combobox

I would like to return the result of the LINQ query to the array, the list then look in this array for example with the letter "A". And return the list to the combobox containing all the letters "A".
My linq query:
var collectionName = (
from row in repos.GetTable<Table_Names>()
select row.Name
).Distinct().ToArray();
And I do not know now how to search the array so that it will find all of them containing the letter "A".
Ultimately, I would like to search in the array instead of sending queries to the database. That would make the list in the combobox narrow down dynamically.
I think it's better to get the filtered collection, instead for perform search after getting the collection. So I suggest you to use a Where clause like the following to get filtered items only:
string searchString ="A";
from row in repos.GetTable<Table_Names>()
where row.Name.Contains(searchString)
select row.Name;
Instead for Contains you can also try StartsWith if you want to get the collection of strings that starts with the given search text.
If you don't want to filter in the database, you can use linq to objects to further filter the collection in memory:
var filtered = collectionName.Where(item => item.Contains("A")).ToArray();
You can use SqlMethods.Like in LINQ query. Check the code below
private void comboBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.DataSource = getItems(comboBox1.Text);
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ID";
}
public static List<ComboboxItem> getItems(string text)
{
DataClasses1DataContext context = new DataClasses1DataContext();
try
{
List<ComboboxItem> Ilist = new List<ComboboxItem>();
var query = from x in context.testComboBoxes where SqlMethods.Like(x.name, '%' + text +'%') select x;
foreach (var q in query)
{
ComboboxItem item = new ComboboxItem();
item.ID = q.id;
item.Name = q.name;
Ilist.Add(item);
}
return Ilist;
}
catch (Exception ex)
{
return null;
}
}
public class ComboboxItem
{
public object ID { get; set; }
public string Name { get; set; }
}

filling combo box using LINQ query (distinct)

I have a combocox the name is "cmbModel" I want to fill the database with two different values in a table.
This is what I have done:
private void Form1_Load(object sender, EventArgs e)
{
using (LINQSQLDataContext db = new LINQSQLDataContext())
{
cmbModel.DisplayMember = "szModel";
cmbModel.DataSource = db.VehEcus.ToList<VehEcu>();
}
}
this will fill my cmbModel with szModel column of my table but I want to avoid repeating , how can I use "distinct" in query to achieve my goal?
and also I want to show 2 items of my table like "modelID-szModel" in my combobox
Thanks
If you just want to show a single column anyway you could select that column and use Distinct:
cmbModel.DataSource = db.InfoProg_VehEcus.Select(x => x.szModel).Distinct();
You can apply Distinct() at any point after your query. I recommend doing it before you enumerate.
To create the custom modelID-szModel field, you can enumerate the query using AsEnumerable(), then create an anonymous type and use String.Format to concatenate your values into your new field.
using (LINQSQLDataContext c = new LINQSQLDataContext ())
{
var items = c.VehEcus.Select(t => new
{
a = t.szModel,
b = t.modelID
}).Distinct()
.AsEnumerable().Select(t => new
{
displayMember = String.Format("{0}-{1}", t.a, t.b)
});
cmbModel.DisplayMember = "displayMember";
cmbModel.DataSource = items.ToList();
}

Creating a dictionary or ienumerable object

So I have a two columns from a database that will return all the products that are in my store and the department id's that are associated with the product.
What I want to do is create something using a list/dictionary/ienumerable set so that if I give a function a product id, it will spit out the department id. At the moment I am having some troubles getting the declarations correct and need some help in that department.
First I have the base for the relationship between the Product and Category. Then I want ProductCategoryCollection to return the collection of all the mappings for each product and category/department. I am stuck in the second part and not really sure where to go from where I am at.
helper.GetProductToCategoryMatching() returns the rows from the database.
public class ProductAndCategoryID
{
public ProductAndCategoryID(int product, int category)
{
this.productID = product;
this.categoryID = category;
}
public int productID;
public int categoryID;
}
public class ProductCategoryCollection : IEnumerable<ProductAndCategoryID>
{
public ProductCategoryCollection()
{
}
public List<ProductCategoryCollection> populate()
{
ShippingClassHelper helper = new ShippingClassHelper();
DataSet ds = new DataSet();
List<ProductCategoryCollection> list = new List<ProductCategoryCollection>();
ds = helper.GetProductToCategoryMatching();
foreach (DataRow row in ds.Tables[0].Rows)
{
}
return new List<ProductCategoryCollection>();
}
}
All you need to do now is create a ProductCategoryCollection object inside of your loop and add it to your list.
public List<ProductAndCategoryID> populate()
{
ShippingClassHelper helper = new ShippingClassHelper();
DataSet ds = new DataSet();
List<ProductAndCategoryID> list = new List<ProductAndCategoryID>();
ds = helper.GetProductToCategoryMatching();
foreach (DataRow row in ds.Tables[0].Rows)
{
var pc = new ProductAndCategoryID();
pc.ProductID = row[0];
pc.CategoryID = row[1];
list.Add(pc);
}
return list;
}
If I understand your question and your requirements correctly, you would like to get a dictionary that maps a ProductID to a CategoryID, so that a lookup can be performed for the CategoryID of a given ProductID.
If that is a good translation of your problem, this is what you can do:
var productMap = new ShippingClassHelper()
.GetProductToCategoryMatching()
.Tables[0].Rows
.ToDictionary(row => (int)row[0], row => (int)row[1]);
It makes the following assumptions:
The "ProductID" field is an integer and the first field in a row.
The "CategoryID" field is an integer and the second field in a row.
Your dataset does not contain duplicate "ProductID" values.
Now you can use this dictionary to perform lookups. If you want to check if a given product id exists, you can do:
var containsProduct660 = productMap.ContainsKey(660);
If you want to retrieve the category id for a given product id you can do:
var categoryIdForProduct660 = productMap[660];

Read row values with entities

I am creating a Pie Chart and its name-value pair are being retrieved from the database.
how to read the row details in order to get the values and store it one property ?
public override void OnApplyTemplate()
{
Ram.DataContext = new List<UsageRAM>()
{ new UsageRAM() { name = "Available" , value =/*how to get the value here*/ },
new UsageRAM() { name = "Used" , value =/*how to get the value here*/ }};
base.OnApplyTemplate();
}
public class UsageRAM
{
public string name { get; set; }
public double value { get; set; }
}
EDIT
--Answer which worked for me--
using (DB db = new DB())
{
var row = (from c in db.UsageRAMs
orderby c.UsageRamID descending
select c).First();
Ram.DataContext = new List<UsageRAM>()
{ new UsageRAM() { name = "Available" , value = row.Available},
new UsageRAM() { name = "Used" , value = row.Used }};
If you're using EF, simply add a new model to your project and include the requried table(s) in this model. Then you can use the newly created Entities object to read your db values as follows:
var Rows = context.YourTableName.Where(YourCondition).ToArray();
You can then iterate over the Rows collection using foreach or something.
If you need to read values from a single row, you may want to use First instead of Where above. That will return a single object whose properties will map to your db fields and can directly be assigned in the example code you posted in the question.
EXAMPLE
Say your model's Entity class is named MyEntities and it contains the table UsageRam, which has two fields Available and Used. Then it will take the following code:
using(MyEntities e = new MyEntities)
{
var Row = e.MyTable.First(x => x.UsageRamID = **[ID]**);
MessageBox.Show("Available=" + Row.Available.ToString() + ",Used=" + Row.Used.ToString();
}
I have just shown values in message box, you can assign them to anything you want.
Another Example
using(MyEntities e = new MyEntities)
{
var Rows = e.MyTable.Where(x => x.UsageRamID > 10 && x.UsageRamID < 20);
foreach(var Row in Rows)
MessageBox.Show("Available=" + Row.Available.ToString() + ",Used=" + Row.Used.ToString();
}
EXAMPLE 3
The code in your updated post appears fine to me, though I do have some doubts about the db design, but given your table is indexed on RamUsageID column, this should give you correct results without much performance impact. I generally prefer lambada expressions over query, so I'd rather write it like:
db.RamUsage.OrderByDescending(x => x.RamUsageID).First()

i want to convert a dataset of a SDF database to obseravable collection in WPF

i am currently working with SQL CE & WPF . in the middle of coding i struck with no idea of converting the dataset of a Database to my observablecollection which is binds to the UI Controllers like Listbox and listview.
plz guide me , if possible with code !!
Let's say your DataSet contains a Table named Person and that Person has columns Id, Name, and Age.
You first create a Person class to hold your Person data:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Then you use LINQ to populate your ObservableCollection:
var people = new ObservableCollection<Person>(
dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
Id = p.Field<int>("Id"),
Name = p.Field<string>("Name"),
Age = p.Field<int>("Age")
}));
You will need to add the following assembly reference to your project in order to use the AsEnumerable<T>() and Field<T>() extension methods:
System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)
Edit
In response to your comment, you would normally process each change you make to the data immediately rather than try to convert the whole ObservableCollection back to the DataSet. So, for example, if you add a new Person to your ObservableCollection, you would also want to add that Person to the DataSet.
Add a person:
var table = dataSet.Tables["Person"];
var row = table.NewRow();
row["Id"] = person.Id;
row["Name"] = person.Name;
row["Age"] = person.Age;
table.Rows.Add(row);
Delete a person:
var table = dataSet.Tables["Person"];
var row = table.AsEnumerable().Where(p => p.Id == person.Id);
row.Delete();
Update a person (say you want to change the age to 37):
var table = dataSet.Tables["Person"];
var row = table.AsEnumerable().Where(p => p.Id == person.Id);
var row["Age"] = 37;
You might also want to look into LINQ to SQL because it automates a lot of this stuff.

Categories