samedi 29 novembre 2008

Fixing sys.InvalidOperationException: InitializeError #2104 in Silverlight 2 Beta 2

I've been working constantly on Silverlight (at a somewhat crazy pace :)) during the past few months. The site www.silverlight.net helped me a lot. I learned almost everything I know about Silverlight on this site.

I ran into this error quite frequently :
sys.InvalidOperationException: InitializeError #2104
The silverlight control won't load, and page shows blanc, even though the plugin is well installed. In fact, this error is due to the fact that the file with the extension  ".xap"  does not get saved as mime type in IIS.

To save this file correctly, Follow these steps before:
The screens are in french, but the placement of buttons and options is identical


 image

 image 

In the two textboxes, enter the following :
  - Extension : .xap
  - Mime Type : application/x-silverlight-2-b2
That should do it ! :)

Here is the french version of this post.

vendredi 28 novembre 2008

Remplir un DropDownList à partir d'un DataTable

Dans cet exemple, j'interroge la base de données (SQL Server 2005) et je recupere les données dans un DataTable comme suit:

public DataTable getAllClients()
{

DatabaseHelper db = new DatabaseHelper();
DataTable dt = new DataTable();
try
{
dt = db.ExecuteDataSet("spGetAllClients", CommandType.StoredProcedure).Tables[0];
}
catch (Exception ex)
{
}
return (dt);

}



La procédure stockée retourne l'id du client et la raison sociale.



Pour remplir un controle DropDownList, je met les données recupérées depuis la base dans un DataTable dt. Il est possible de relier dt avec le DropDownList, mais j'ai besoin de modifier la présentation des données. Pour celà, j'utilise un 2ème DataTable dtTmp:



DAClient da = new DAClient();

DataTable dt = da.getAllClients();

if (dt.Rows.Count != 0)
{

DataTable dtTmp = new DataTable(); //creation du dattatable dtTmp

dtTmp.Columns.Add("idclient", typeof(Int32)); //datacolumn 0 : "id"
dtTmp.Columns.Add("raisonsociale", typeof(string)); //datacolumn 1: contiendra le texte "id - raisonsociale"

DataRow tmpRow;

tmpRow = dtTmp.NewRow();
tmpRow["idclient"] = "0";
tmpRow["raisonsociale"] = "Sélectionnez un client";
dtTmp.Rows.Add(tmpRow); //ajoute la ligne "0", "Sélectionnez un client" au DataTable

for (int i = 0; i < dt.Rows.Count; i++)
{
tmpRow = dtTmp.NewRow();

tmpRow["idclient"] = dt.Rows[i]["idclient"];

tmpRow["raisonsociale"] = dt.Rows[i]["idclient"] + " - " + dt.Rows[i]["raisonsociale"];


dtTmp.Rows.Add(tmpRow);

}

ddlClients.DataTextField = "raisonsociale";
ddlClients.DataValueField = "idclient";


ddlClients.Items.Clear();

ddlClients.DataSource = dtTmp;

ddlClients.DataBind();
}



Le résultat:



image



En ce moment je travaille sur un projet qui, vu sa nature un peu particulière, les nouvelles techniques et méthodes de développement ne sont pas forcement la bonne solution. J'essaierai autant que je peux de poster du code "the old fashioned way" qui je pense peut toujours être utile pour certains....