Archive for Agosto, 2007

Programmatically add a bound field to a GridView

Agosto 31st, 2007

Description: This example takes a DataTable and adds bound fields for every column to a GridView in ASP.Net 2.0

Link: http://www.codekeep.net/snippets/c13a8b80-f483-4d18-859b-6453ee5b1769.aspx

foreach (DataColumn column in dataTable.Columns)
{
	BoundField field = new BoundField();
	field.HeaderText = column.Caption;
	field.DataField = column.ColumnName;
	grid.Columns.Add(field);
}

Update App.config file value

Agosto 30th, 2007

Description: Save value to the App.config file’s appSettings section

Link: http://www.codekeep.net/snippets/00e10a84-b264-437d-85bd-0ce6fe8fa620.aspx

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove("test");
            config.AppSettings.Settings.Add("test", "testValue");
            config.Save(ConfigurationSaveMode.Modified);

Select an XML node and update its inner text.

Agosto 30th, 2007

Description: Use the SelectSingleNode() method to select a node and then update its inner text.

Link: http://www.codekeep.net/snippets/933523ac-1fc6-43ca-9d48-b12ad588a2c7.aspx

string xml = "<Entity EntityType=\"Manufacturer\">";
xml += "<Name></Name><XPath /><URL></URL></Entity>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode nodeName = doc.SelectSingleNode("Entity/Name");
nodeName.InnerText = "ACME";

-----------------------------------
Now, the xml looks like this:
-----------------------------------
<Entity EntityType="Manufacturer"><Name>ACME</Name><XPath /><URL></URL></Entity>

Use Invoke to update user interface from a separate thread

Agosto 30th, 2007

Description: In Windows Forms if you launch a new thread and want to update controls on your form, you must use Invoke. This example updates the Text property of a Label.

Link: http://www.codekeep.net/snippets/3b1cd8c5-cf11-4eb2-b52f-78dccc2d6de9.aspx

------------------------
First create a method to make the UI updates, and define a delegate that
matches the signature of the method.
------------------------
private delegate void setStatusDelegate(string status);

private void setStatus(string status)
{
	lblStatus.Text = status;
}

------------------------
From another thread, you can use the Invoke method of the form to
execute the setStatus method.
------------------------
Invoke(new setStatusDelegate(setStatus), "This is the status text");

Validate required drop down lists.

Agosto 30th, 2007

Description: Use a CompareValidator to ensure that the user has made a valid selection from a drop down list.

Link: http://www.codekeep.net/snippets/56f51d03-b35d-4e87-8033-4741d4e4985b.aspx

-------------------------------------
Here are the drop down list and validator declarations.
The example here is a list of products.
-------------------------------------
<asp:DropDownList*Selection Required" ErrorMessage="You must select a Product"></asp:CompareValidator>

-------------------------------------
Here is the C# code to bind the list and add a default entry.
Note that the value of the default entry is -1.
-------------------------------------
lstProducts.DataSource = GetProducts();
lstProducts.DataBind();
insertDefaultItem(lstProducts);

private void insertDefaultItem(DropDownList ddl)
{
	ddl.Items.Insert(0, new ListItem("<Select One>", "-1"));
}

[XML] Recupération et Synthetisation de fichiers

Agosto 29th, 2007

Description: Recupère touts les fichiers d’alimentation MENESR d’un dossier et les synthetisent en un seul document

Link: http://www.codekeep.net/snippets/9d5071d0-2425-4123-bf12-d174c6c0c0ca.aspx

Console.WriteLine ("->Recuperation des Fichiers");
            DirectoryInfo myDir = new DirectoryInfo (path);
            FileInfo[] myFiles = myDir.GetFiles ("*.xml");

            Collection<XmlDocument> ficsXML = new Collection<XmlDocument> ();

            foreach (FileInfo file in myFiles) {
                if(file.Name.Contains ("_EtabEducNat_")){
                    ficEtab.Load (file.FullName);
                    ficsXML.Add (ficEtab);
                    if(ConfigurationManager.AppSettings["debug"]=="true") {
                        Console.WriteLine ("-> Lecture des fichiers d'etablissements.");
                    }
                }
                if (file.Name.Contains("_PersEducNat_"))
                {
                    ficPersEducNat.Load(file.FullName);
                    ficsXML.Add(ficPersEducNat);
                    if (ConfigurationManager.AppSettings["debug"] == "true")
                    {
                        Console.WriteLine("-> Lecture des fichiers des Personnels Enseignants & Non Enseignants.");
                    }
                }
                if (file.Name.Contains("_PersRelEleve_"))
                {
                    ficPersRelEleve.Load(file.FullName);
                    ficsXML.Add(ficPersRelEleve);
                    if (ConfigurationManager.AppSettings["debug"] == "true")
                    {
                        Console.WriteLine("-> Lecture des fichiers de parents d'élève.");
                    }
                }
                if (file.Name.Contains("_Eleve_"))
                {
                    ficEleve.Load(file.FullName);
                    ficsXML.Add (ficEleve);
                    if (ConfigurationManager.AppSettings["debug"] == "true")
                    {
                        Console.WriteLine("-> Lecture des fichiers d'élèves.");
                    }
                }

            }

            Console.WriteLine("");

            Console.WriteLine("->Synthetisation des Fichiers");

            XmlDocument XMLInfos = new XmlDocument ();
            int j = 0;
            foreach (XmlDocument document in ficsXML) {
                if (j == 0) {
                    XMLInfos = document;
                }
                else {
                    XmlNodeList oList = document.SelectNodes ("/ficAlimMENESR/addRequest");
                    XmlNode dest = XMLInfos.SelectSingleNode ("/ficAlimMENESR");
                    foreach (XmlNode node in oList) {

                        XmlNode maNode =XMLInfos.ImportNode (node, true);
                        dest.AppendChild (maNode);
                    }
                }
                j++;
            }

How to Create Form Posts with Ajax

Agosto 29th, 2007

Now that Ajax is becoming a standard for handling interactions on the Web it’s time to use it to update form submissions. In this article you’ll learn how to create a reusable Ajax process for all forms. The source code and a working demo is included. By Kris Hadlock. 0829

WinUI

Agosto 29th, 2007

Description: WinUI

Link: http://www.codekeep.net/snippets/8226c14f-a9f0-4237-9184-2e2e96d65ee2.aspx

using HIS.WinUI.Common;
HIS.WinUI.Common.Forms.FormBase

[NetUtilisateur] Set Multivalued Property

Agosto 28th, 2007

Description: Code permettant de modifier une Property Multivaluée sous InfiniTec.

Link: http://www.codekeep.net/snippets/3d1b4b97-9cf6-46f5-8f9b-5ae27acec3df.aspx

                Refresh ();
                if (existProperty("PropertyName", false))
                {
                    Properties.Remove(Properties["PropertyName"].Name);
                    Save (true);
                }
                Properties.AddMultiValued ("PropertyName", value);
                Save(true);

Resize an image to a max height or width

Agosto 27th, 2007

Description: Resize an image to be no larger than a specified width and height (for instance to create a thumbnail) while maintaining the aspect ratio.

Link: http://www.codekeep.net/snippets/1741ec47-b966-4892-9e60-5335edf3e9c4.aspx

const int maxsize = 100;

using(Image image = Bitmap.FromFile(@"C:\image.jpg"))
{
	double coeff;
	if (image.Width > image.Height)
	{
		coeff = maxsize/(double) image.Width;
	}
	else
	{
		coeff = maxsize/(double) image.Height;
	}

	int newHeight = Convert.ToInt32(coeff*image.Height);
	int newWidth = Convert.ToInt32(coeff*image.Width);

	using(Bitmap bitmap = new Bitmap(image, newWidth, newHeight))
	{
		bitmap.Save(@"C:\image_resized.jpg", image.RawFormat);
	}
}

Next »