Arraylist and list issue in C# - asp.net

I have a arraylist of employees from the gridview in my ASP.net Page as shown below:
ArrayList tempempList = new ArrayList();
List<Employee> emps = new List<Employee>();
for (int i = 0; i < gvemp.Rows.Count; i++)
{
if (((CheckBox)gvemp.Rows[i].FindControl("cbAssign")).Checked)
{
tempempList.Add(((Label)gvemp.Rows[i].FindControl("lblempSeqNo")).Text);
}
}
Now I have List and Employee object consists of the following properties
empID, empName,empAge,empSalary
Now for each empID from tempempList I need to populate the List
I am not sure how I can populate emps.
Appreciate your help

The List<T> object is just the strongly-typed version (generic) of ArrayList. ArrayList is preserved for traditional purposes, and List<T> should be utilized when necessary.
You can call the List<T>.Add() method, just like you did for the ArrayList.Add() method. So something like:
emps.Add(yourValue);
That should suffice.

Related

How to Insert DataTable with values into Database in Linq to sql

I want to save all record exist in a DataTable into my database. I am using Linq-to-SQL in C#.
I am using this code:
DataTable dt = null;
dt = getTable(fl.ToString()); //getting DataTable
using (DataClassesDataContext dc = new DataClassesDataContext())
{
foreach (var mt in dc.Mapping.GetTables())
{
if (dt.TableName == mt.TableName)
{
//How to insert this table values in DB??
}
}
}
Please suggest me better way to do that.
Thanks in advance.
Well, the whole point of using something like Linq-to-SQL is the fact that it gives you proper .NET objects to work with, instead of the clumsy, untyped DataTable.
So if you use Linq-to-SQL properly, you should retrieve your data as .NET objects using Linq-to-SQL, then you should modify your .NET objects, and use Linq-to-SQL again to store the data.
Something like this:
// set up your Linq-to-SQL DataContext
using (AdventureWorksDataContext ctx = new AdventureWorksDataContext())
{
// get some data - use the *OBJECTS* returned to you! Not a DataTable!
List<Customer> customers = ctx.Customers.Take(10).ToList();
// modify data
foreach (Customer c in customers)
{
c.ModifiedDate = DateTime.Now;
}
// save the changes
ctx.SubmitChanges();
}
So if you have a DataTable, what I'd suggest is converting those DataRows into some kind of an object type that's stored in your database, and then storing that information into the database using Linq-To-SQL - something like this:
public void SaveData(DataTable table)
{
// set up your Linq-to-SQL DataContext
using (AdventureWorksDataContext ctx = new AdventureWorksDataContext())
{
// iterate over the data rows
foreach (DataRow row in table.Rows)
{
// Convert the DataRow to an object
YourObjectType myData = ConvertDataRowToObject(row);
// add this data to the Linq-to-SQL data context
ctx.YourObjectTypes.InsertOnSubmit(myData);
}
// save the changes
ctx.SubmitChanges();
}
}

How to bind an entity framework object to a listview

This is the first time to ask here, so please help me convey what I mean.
First I used an entity framework and I want to make a query that retrieve data from 3 tables, and then bind the result to a list view, the problem here is that there are nothing appear on the page. That's what I made:
ITIEntities MyContext = new ITIEntities();
var Courses = MyContext.Courses;
var Instructors = MyContext.Instructors;
var Ins_Courses = MyContext.Ins_Course;
var Query= from Crs in Courses
from Ints in Instructors
from Instructor_courses in Ins_Courses
where Crs.Crs_Id==Instructor_courses.Crs_Id
&& Instructor_courses.Ins_Id== Ints.Ins_Id
select new
{
Name= Ints.Ins_Name,
Salary=Ints.Salary,
ListOfCourses=Crs.Crs_Name,
};
MyListView.DataSource = Query;
MyListView.DataBind();
MyContext.SaveChanges();
here problem is enumerable type variable Query so if you want to bind result to listview you have to define entity public class with getter and setter properties for each column which you want to show.
public Class ClassNameEntity
{
public string Name {get; set;}
}
use in linq query with following
select new ClassNameEntity
{
Name= Ints.Ins_Name
}.ToList();
that will return list of your defined class object list so directly bind result of upper query.
ITIEntities MyContext = new ITIEntities();
var Courses = MyContext.Courses;
var Instructors = MyContext.Instructors;
var Ins_Courses = MyContext.Ins_Course;
List<ClassNameEntity> Query= from Crs in Courses
from Ints in Instructors
from Instructor_courses in Ins_Courses
where Crs.Crs_Id==Instructor_courses.Crs_Id
&& Instructor_courses.Ins_Id== Ints.Ins_Id
select new ClassNameEntity
{
Name= Ints.Ins_Name,
Salary=Ints.Salary,
ListOfCourses=Crs.Crs_Name,
}.ToList();
MyListView.DataSource = Query;
MyListView.DataBind();
MyContext.SaveChanges();

datagrid rows to save in collection and show on another page asp.net

I have grid in my page which have four columns with multiple rows
Userid username stateid and statename
which collection i should use to send data to another page using session
my code is
string[] strArray = new string[] {};
foreach (GridViewRow gr in gvCompany.Rows)
{
strArray =new string[4] {new[] {gr.Cells[0].Text}.ToString(), new[] {gr.Cells[1].Text}.ToString(), new[] {gr.Cells[2].Text}.ToString(),new[] {gr.Cells[3].Text}.ToString()};
}
Session["List"] = strArray;
Response.Redirect("Tid.aspx?Mode=List");
on tid page my code is
string[] ls = new string[] { };
ls =(string[]) Session["List"];
foreach (string st in ls )
{
//get each cell
}
but value of st is system.string rather than value
Using an string[] is not recommended since the more fields will add in future the difficult it will get to keep the field and index relationship bug free..
You can create a proper class object eg. Company and pass a List to other class, List is in System.Collections.Generic namespace.

Iterating DataView for Update

I have a DataView which filters
inputView.RowFilter = "isnull(" + ID1 + ",'')<>'' and isnull(" + reason + ",0)=0";
after this filter i have to update "IsVerified" column of inputView to "1"
Is there something in LINQ to execute the following?
inputView.RowFilter.ForEach(x=>x.Field<IsVerified> =1);
If you want to use LINQ on your DataView, you'll have to create your own extension method on the DataView. The DataView does not natively have a .ToList() method, which is where the .ForEach() is defined.
Create your Extension Method for the DataView.
public static class DataViewExtensions
{
public static List<DataRowView> ToList(this DataView v)
{
List<DataRowView> foo = new List<DataRowView>();
for (int i = 0; i < v.Count; i++)
{
foo.Add(v[i]);
}
return foo;
}
}
Your DataView can now be called this like:
inputView.ToList().ForEach(x=>x["IsVerified"]=1);
Some developers may prefer the full .NET foreach (var x in y) statement, but this will do the same.
A full console application proo-of-concept: http://pastebin.com/y9z5n6VH

Is there an ASP.NET collection for selected items in ListBox?

On my Asp.NET website, I have a listbox that allows multiple selections. I'd like to be able to ask something like:
blah = myListbox.selectedItems;
and be given a collection of the items that were selected in the listbox. It looks like there is a method for this in the Windows Forms world, but not for asp.NET. Is there a simpler way to do this than just iterating over the Items collection looking for selected values?
Something like this should get you the selected items:
List<ListItem> selectedItems = new List<ListItem>();
int[] selectedItemsIndexes = myListbox.GetSelectedIndices();
foreach (int selectedItem in selectedItemsIndexes)
{
selectedItems.Add(myListbox.Items[selectedItem]);
}
As an extension method:
public static class ListBoxExtensions
{
public static List<ListItem> GetSelectedItems(this ListBox listbox)
{
List<ListItem> selectedItems = new List<ListItem>();
int[] selectedItemsIndexes = listbox.GetSelectedIndices();
foreach (int selectedItem in selectedItemsIndexes)
{
selectedItems.Add(listbox.Items[selectedItem]);
}
return selectedItems;
}
}
so now you can just call:
List<ListItem> selectedItems = myListBox.GetSelectedItems();
As olle suggested the Extension method could be Linq-ified and thu shrunk down even further to:
public static class ListBoxExtensions
{
public static IEnumerable<ListItem> GetSelectedItems(this ListBox listbox)
{
var selectedItems = from ListItem i in myListbox.Items where i.Selected select i
return selectedItems;
}
}
Doesn't look like you can get the items directly, but GetSelectedIndices might help.
There is no such property but an easy linq query gets you the results fast and easy.
var selectedItems = from ListItem i in myListbox.Items where i.Selected select i;
With an extension method you can make it even simpler if you need to do this kind of thing allot.
Last time I checked, no but this may be helpful
http://tlaughlin.pandorasystems.com/blogs/tlaughlin/archive/2007/08/03/asp-net-listbox-missing-selecteditems-property.aspx

Resources