26 Temmuz 2012 Perşembe

access to main form from child fom (WinForms)

Assume ChildForm is opened from MainForm:


ChildForm form= new  ChildForm  ();
form.ShowDalog(this);


Now, to access MainForm from ChildForm (to refresh something on MainForm etc.), call this from child form:


MainForm parent = ( MainForm  )this.Owner;
// do sonething in parent form
this.Close(); // close child form



http://stackoverflow.com/questions/5443932/accessing-main-form-from-child-form

update error in dbcontext (ef)

When i tried to update an entity by using DbContext (entity framework), i got this error:

{"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."}

The discussion in http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-objectstatemanager-the-object helped me to solve it.
I replaced the lines


//context.Sentence.Attach(temp);
// context .Entry(temp).State = System.Data.EntityState.Modified;


with the following:


Sentence old =  context.Sentence.Find(temp.ID);
context .Entry(old).CurrentValues.SetValues(temp);

12 Temmuz 2012 Perşembe

reading an excel file in C#

In order to read an excel file cell by cell, the first step is adding Microsoft Excel Object Library as a reference to the project (http://www.c-sharpcorner.com/Forums/Thread/80161/). 
Afterwards, the following code should work:

// ..
using Excel = Microsoft.Office.Interop.Excel;
// ...

 private static void ReadExcelFile()
 {
            Excel.Application exApp ;
            Excel.Workbook exWorkBook ;
            Excel.Worksheet exWorkSheet ;
            Excel.Range range ;


            string str;
            int row = 0;
            int column = 0;


            String pwd =  Directory.GetCurrentDirectory();


            exApp = new Excel.Application();
            exWorkBook = exApp.Workbooks.Open("C:/filename.xls" );
            exWorkSheet = (Excel.Worksheet)exWorkBook.Worksheets.get_Item(1);


            range = exWorkSheet.UsedRange;


            for ( row  = 1;  row  <= range.Rows.Count; row++)
            {
                for ( column = 1;  column <= range.Columns.Count;  column  ++)
                {
                    // to allow nullable cells
                    if (range.Cells[rCnt, cCnt].Value2 == null) continue;
                    str = range.Cells[row,  column].Value2.ToString();
                }
            }


            exWorkBook.Close(true, null, null);
            exApp.Quit();
}


see these for more:
http://csharp.net-informations.com/excel/csharp-read-excel.htm
http://dontbreakthebuild.com/2011/01/30/excel-and-c-interop-with-net-4-how-to-read-data-from-excel/

11 Temmuz 2012 Çarşamba

JPA tips 2


A previous post about this topic: http://hilaltarakci.blogspot.com/2011/03/jpa-tips.html

Actually, long version of this post includes notes taken while i was reading Chapter 9- Working with Objects of book Java Persistence with Hibernate (http://www.manning.com/bauer2/). However, i could not post it because of the copyright rules..

 But at least, i can recommend to buy the book and read the whole chapter..

free data models

I am trying to model a complex questionnaire and during my googling for best practices i discovered this website: http://www.databaseanswers.org/data_models/
An excellent site full of free and well prepared data models on several topics..