January 2005 - Posts

Adding images to ListView control

this an example how to add images to listview control:

// create a new image list
ImageList ilImages = new ImageList();
// add some images
ilImages.images.add(“image.bmp“);
lvObjects.SmallImageList = ilImages;

foreach (object o in objects)
{
ListViewItem listItem =
new ListViewItem();
string sArg = "";
listItem.Text = o.Text;
// index of desired image

listItem.ImageIndex = 0;
lvObjects.Items.Add(listItem);

}

"String or binary data would be truncated" MS Sql error

This problem occurs when you trying to insert to field a string that exceeds fields length. The only solution I could find was to set a bigger field length.

T-SQL transaction vs ADO.NET transactions

What is the better to use between them?

Application "Not responding" solution needed...

I have a function that runs many loops, it could run for a long time. When the function runs the application becomes “not responding”. I added a new form contains one label “Please wait...” so users could be calm that application not freezed. I load this form each time function starts and unload when it finishs. My origin plan was to show function's statuses on this form. But the problem is when the function run this form is freezed and no massage I could update.
What is the best scenario to solve this problem?

Simple class for preparing strings for database

As always, during the presentation something must go wrong. At this time I presented my new program to R&D guys. For the test one guy entered an entry “Someone's test” and my program throwed the exception. What a shame!  So I wrote simple class for formatting strings before insert to database.
public class StringFormatter
{
      public static string FormatOutgoingString(string sOut)
     {
         Int32 FoundAt;
         char[] InvalidChars = {(char)39};
         for(int i=0;i<=InvalidChars.GetUpperBound(0);i++)
         {
              FoundAt = sOut.IndexOf(InvalidChars[i]);
              if(FoundAt!=-1)
         {
              sOut= sOut.Insert(FoundAt,InvalidChars[i].ToString());
          }
      }
     return sOut;
}

 

M-systems and SanDisk announce U3

M-systems and SanDisk established a U3 company that will produce next generation of DiskOnKey devices. U3 devices is the solution for storing personal data and luanching applications. You could store etire environment like Visual Studio with all your projects and luanch it from every computer without intalling it. It would be great!!! 

Howto read MS Word tables

In my recent project I need to import data from MS word to Xml so that it could be easily imported to database. Actually within MS word document only tables are needed to be read. After a few hours of searching, googling and reading articles I found basic clues of .NET MS Word programming. I assume that the pretty simple algorithm for reading tables from Word document, is to use document.tables collection for accessing information located in tables – that is what I needed. And reading tables row by row, excluding unnecessary data like headers.

Everything gone fine until my program started to give me exceptions that can't read from merged rows. Table that I needed to read was something like follow:

Name

Some description

1. Property

2. Property

3. Property

4. Property

5. Property

 

It is critical for me to read merged cells. Meanwhile the only solution for that problem I thought about is to run on table column by column, but I am forced to run on each table few times: First I read all single cells to DataTable, then I start to read merged cell. Each table cell object has rowindex property, this is actually the row index where cell starts. So I read all that indexes to an array so that I know where each cell starts and ends. Using this information I could generate following DataTable:

Name

Some description

1. Property

Name

Some description

2. Property

Name

Some description

3. Property

Name

Some description

4. Property

Name

Some description

5. Property

 

I use following code:

foreach (Table tbl in doc.Tables)

{

if(tbl.Columns.Count==3)

{

DataTable dt = new DataTable();

// procedure that add columns to datatable

InitTable(ref dt);

// Insert Steps to table

Console.WriteLine("Inserting steps to scenario "+nScenario);

for(Int32 row=2;row<=tbl.Rows.Count;row++)

{

DataRow dr = dt.NewRow();

dr["Step"]= ReplaceWordTags(tbl.Cell(row,3).Range.Text);

dt.Rows.Add(dr);

}

// get begin of cells

Console.WriteLine("reading indexes ");

Int32[] BeginsOfCells = new int[tbl.Columns[1].Cells.Count];

// insert number to datateable

Console.WriteLine("Inserting No. and Desc to scenario "+nScenario);

for (Int32 i=2;i<=tbl.Columns[1].Cells.Count;i++)

{

Cell TmpCell = tbl.Columns[1].Cells[i];

BeginsOfCells[i-2]=TmpCell.RowIndex;

}

BeginsOfCells[tbl.Columns[1].Cells.Count-1]=tbl.Rows.Count;

for (Int32 i=2;i<=tbl.Columns[1].Cells.Count;i++)

{

Cell TmpCell = tbl.Columns[1].Cells[i];

Cell TmpCellDesc = tbl.Columns[2].Cells[i];

TmpCellDesc.Select();

Selection newselect = ThisApplication.Selection;

string CellDesc= ReplaceWordTags(newselect.Text);

string CellValue=ReplaceWordTags(TmpCell.Range.Text);

for(Int32 f=BeginsOfCells[i-2];f<BeginsOfCells[i-1];f++)

{

dt.Rows[f-2]["Number"]=CellValue;

dt.Rows[f-2]["Descript"]=CellDesc;

}

}

nScenario= nScenario+1;

dt.TableName = "Scenario"+nScenario;

ds.Tables.Add(dt);

}

Relevant links:
Understanding the Word Object Model from a .NET Developer's Perspective

DataSet.ReadXml() problem

I've created a function that writes Xml to a file using DataSet.WriteXml() function. I want to read from created file but it just not reads. Anybody knows what could be the problem DataSet.ReadXml function doesn't work.

A "Must read" article for .NET Xml beginners

Detailed explanation of  .NET Xml API's and their usage pros and cons.

http://support.softartisans.com/kbview.aspx?ID=673

ListBox datasource refresh problem

I have a wierd problem with listbox refrsh event. I've created panel contains 2 listboxes: first contains all available items taken from database , second contains desired items user choose. Data taken from database stored in ArrayLists. The main idea that user chooses one item from available list clicks button and item moves from available ListBox to desired ListBox. It moves but then I remove this item from available items ArrayList with remove method and I try to refresh datasource of fisrt ListBox it fails with OutOfrange exception.

I could solve this problem by populating this Listbox:

ArrayList scenarios;

Object scenarion;

foreach (Scenario scenario in scenarios)

{

        ListItem item = new ListItem();

        item.Value = scenario.ScenarioId;

        item.Text = scenario.ScenarioName;

        ListBox.Items.Add(item);

}

 

Microsoft's anti-spyware beta

On my first look it looks good...I installed it yesterday. Few things I liked from the begining: It found more spyware than both Adware and Spybot - I consider to uninstall them when finall release will come out. It has “Track earase” feature - you can clear IE history, cookies, temporary internet files and so on. Also it cleans windows temporary files. This is a very useful feature for me at least because I always cleaned these things by my self - I don't trust any 3rd party software. I know MS software wouldn't earase some essential files or at least would ask “Are You Sure?”:). So I recommend to try this soft.

Link for download : http://www.microsoft.com/athome/security/spyware/software/default.mspx