Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
GeneralRe: Saving Data on a Form Pin
thomas_anderson20-Feb-10 11:29
thomas_anderson20-Feb-10 11:29 
QuestionReading from parallel port using c# Pin
A k ch17-Feb-10 17:41
A k ch17-Feb-10 17:41 
Questionplease forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux17-Feb-10 15:20
tonyonlinux17-Feb-10 15:20 
AnswerRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
Dan Mos17-Feb-10 15:30
Dan Mos17-Feb-10 15:30 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux17-Feb-10 15:35
tonyonlinux17-Feb-10 15:35 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
Dan Mos17-Feb-10 15:37
Dan Mos17-Feb-10 15:37 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux17-Feb-10 15:54
tonyonlinux17-Feb-10 15:54 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this [modified] Pin
Dan Mos17-Feb-10 16:21
Dan Mos17-Feb-10 16:21 
Hy,

It's all wrong.

1)Create a class like
public class Something{
     public string Name{get;set;}
     public string Surname{get;set;}
     .
     .
     .
}


And then create a List<Something> instead of list of string(s);

2) When loading data if you keep loading it using Interop create something like:
for(RCount...{
    curr = new Something();
    for(cCount...){
        if(c==1){
             curr.Name = (string)(range.Cells[rCount, cCount] as Excel.Range).Value2;
        }
        if(c==2){
        ...
        }
        .
        .
    }
    //now(outside the column for) add it to the list of something
    lst.Add(curr);
}


But I Really don't recomend this approach.
Use something like this. It's a lot faster and it does not require Excel to be installed

//the actual connection
                string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=" + yourFileNameHere+ ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\";";
                OleDbConnection con = null;
                lst = new List<Something>();
                Something curr = new Something();

                con = new OleDbConnection(connString);
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandText = "SELECT Name,SurName,etc.. " +
                    "FROM [Sheet1$]";

                OleDbDataReader dr = null;
                try
                {
                    con.Open();
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        curr = new Something();
                        curr.Name = dr.IsDBNull(0) ? "Empty" : dr.GetString(0);
                        curr.SurName = dr.IsDBNull(1) ? "Empty" : dr.GetString(1);
                        .
                        .
                        if (curr.IsOK())//this is really usefull
                        {
                           lst.Add(curr); 
                        }
                    }                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    //posible log and stuff
                }
                finally
                {                    
                    if (con.State != ConnectionState.Closed)
                    {
                        con.Close();
                    }
                    con.Dispose();
                    con = null;
                }


and a create a method inside your class(Something) in this silly example of mine that returns true if the current Something is OK, else false.

Usefull in order to avoid filing your list with "Empty"/Nok rows.

[EDIT] And now you could use LINQ to get the datas something like:
var needed = from rec in lst
     where rec.Name=="YourName"
     select whateverYouNeed;
modified on Thursday, February 18, 2010 1:02 AM

GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux17-Feb-10 19:33
tonyonlinux17-Feb-10 19:33 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
Dan Mos17-Feb-10 20:03
Dan Mos17-Feb-10 20:03 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux18-Feb-10 2:01
tonyonlinux18-Feb-10 2:01 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
Dan Mos18-Feb-10 2:50
Dan Mos18-Feb-10 2:50 
GeneralRe: please forgive me for i know this is basic but i simply don't know how to add stuff a list like this Pin
tonyonlinux18-Feb-10 9:19
tonyonlinux18-Feb-10 9:19 
NewsBookStore GPLv3 Project Pin
User 691845417-Feb-10 14:22
User 691845417-Feb-10 14:22 
GeneralRe: BookStore GPLv3 Project Pin
Not Active17-Feb-10 14:50
mentorNot Active17-Feb-10 14:50 
GeneralRe: BookStore GPLv3 Project Pin
Mycroft Holmes17-Feb-10 17:15
professionalMycroft Holmes17-Feb-10 17:15 
GeneralRe: BookStore GPLv3 Project Pin
User 691845417-Feb-10 17:35
User 691845417-Feb-10 17:35 
GeneralRe: BookStore GPLv3 Project Pin
Anubhava Dimri17-Feb-10 17:37
Anubhava Dimri17-Feb-10 17:37 
Questioncustom pipeline - biztalk 2009 - how to loop through Ibasemessage.bodypart Pin
challa_praveena17-Feb-10 13:02
challa_praveena17-Feb-10 13:02 
AnswerRe: custom pipeline - biztalk 2009 - how to loop through Ibasemessage.bodypart Pin
Mirko198017-Feb-10 22:48
Mirko198017-Feb-10 22:48 
QuestionMessage Removed Pin
17-Feb-10 11:38
JimBob SquarePants17-Feb-10 11:38 
AnswerRe: Trouble deleting items using generic xml provider. Pin
AspDotNetDev17-Feb-10 12:11
protectorAspDotNetDev17-Feb-10 12:11 
QuestionTreeView Pin
mbangh17-Feb-10 10:58
mbangh17-Feb-10 10:58 
AnswerRe: TreeView Pin
PIEBALDconsult17-Feb-10 12:09
mvePIEBALDconsult17-Feb-10 12:09 
GeneralRe: TreeView Pin
mbangh17-Feb-10 15:02
mbangh17-Feb-10 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.