C sharp

Résolu
verbeyst Messages postés 77 Date d'inscription mardi 10 mars 2009 Statut Membre Dernière intervention 15 février 2011 - 18 août 2008 à 16:55
TeBeCo Messages postés 467 Date d'inscription lundi 24 juin 2002 Statut Membre Dernière intervention 9 mars 2011 - 19 août 2008 à 11:08
Le bout de code suivant m'affiche un message erreur que je ne comprends pas
le message est "value cannot be null    parameter name : bytes"
veuillez m'aider

private






void
button2_Click(

object
sender,

EventArgs
e){


string
fichARestaurer =

""
;


Byte
[] maVarBinaire;


// demande de confirmation






DialogResult
réponse =

MessageBox
.Show(

"Voulez-vous vraiment"
+


"restaurer le fichier \n "
+listView1.SelectedItems[0].Text,


"Restauration de fichier"
,

MessageBoxButtons
.OKCancel,


MessageBoxIcon
.Question);


if
(réponse ==

DialogResult
.OK){


//appel de la méthode restauration






// choix de l'emplacement






DialogResult
réponse2 =

MessageBox
.Show(

"Voulez-vous"
+


"restaurer le fichier \n "
+listView1.SelectedItems[0].Text +


"A l'emplacement d'origine?"
,


"Restauration de fichier"
,

MessageBoxButtons
.OKCancel,


MessageBoxIcon
.Question); 


if
(réponse2 ==

DialogResult
.OK){

fichARestaurer = listView1.SelectedItems[0].SubItems[1].Text;

}


else

{







if
(folderBrowserDialog1.ShowDialog() ==

DialogResult
.OK){

fichARestaurer = folderBrowserDialog1.SelectedPath.ToString() +


"\"
+ listView1.SelectedItems[0].Text;


MessageBox
.Show(

"fichier a restaurer "
+ fichARestaurer);}

}

maVarBinaire =


Restauration
.RenvoyerCeFichier(fichARestaurer);  

 


try

{







File
.WriteAllBytes(fichARestaurer, maVarBinaire);}


catch
(

ArgumentNullException
exception){


MessageBox
.Show(exception.Message);}


catch
(

FileNotFoundException
exception){


MessageBox
.Show(exception.Message);}


catch
(System.Security.

SecurityException
exception){


MessageBox
.Show(exception.Message);}


catch
(

NotSupportedException
exception){


MessageBox
.Show(exception.Message);}


catch
(System.IO.

PathTooLongException
exception){


MessageBox
.Show(exception.Message);}


catch
(System.IO.

IOException
exception){


MessageBox
.Show(exception.Message);}


catch
(

UnauthorizedAccessException
exception){


MessageBox
.Show(exception.Message);}

}

}



Voici le code de la classe Restauration
---------------------------------------

public
class
Restauration{

public
static
Byte[] RenvoyerCeFichier(
string nomCompletFichier){

Byte[] fichier =
new
Byte[100];

using (
SqlConnection maConnexion =
new
SqlConnection(
BDDetailEvenement.RenvoieConnectionString()))

{

SqlCommand maCommand =
new
SqlCommand();

try{

maConnexion.Open();

maCommand.CommandTimeout = 0;

//configuration commandemaCommand.Connection = maConnexion;
maCommand.CommandType CommandType.StoredProcedure;maCommand.CommandText

"[dbo].[RestaurerLeFichier]";maCommand.Parameters.Add(

"@NomduFichier",
SqlDbType.NVarChar);maCommand.Prepare();

//initialisation des paramètresmaCommand.Parameters[

"@NomduFichier"].Value = nomCompletFichier;

//exécution ou appel de la procédure paramétrée contenant des requêtes SQL

//maCommand.ExecuteNonQuery();

object tempObject = maCommand.ExecuteScalar();

if (tempObject !=
null){

fichier = (

Byte[])tempObject; }

elsefichier =

null;

}

catch (
InvalidOperationException exception){

MessageBox.Show(exception.Message);}

catch (
SqlException exception){

MessageBox.Show(exception.Message);}

catch (
Exception exception){

MessageBox.Show(exception.Message);}

return fichier;

}

}

}

verbeyst

3 réponses

TeBeCo Messages postés 467 Date d'inscription lundi 24 juin 2002 Statut Membre Dernière intervention 9 mars 2011
19 août 2008 à 11:08
le message est "value cannot be null    parameter name : bytes"
veuillez m'aider

que faut il comprendre ?
un bute vaut null ? et non rien a voir
c'est le nom d'un parametre de fonction qui s'apelle bytes a qui tu passe rien du tout genre une variable non instancier
exemple :

static void toto(montype mavar)
{
   if(mavar == null)
      throw new argumentexeption("Value cannot be null", "tralalatchoum");

   //Traitement normal
}

static main()
{
   montype a;

   toto(a); //argument exception : "value cannot be null"  parameter name : "tralalatchoum"
}

en general on fait corespondre "tralalatchoum" avec le nom de l'argument donc ici on aurai mit "mavar"
donc cherche dans tous tes appel de fonction si t'aurais pas un parametre "bytes" (qui apparteins surment au framework)
je commencerais par regarde du coté de writeallbytes au hasard (non j'ai pas la doc sous les yeux je sais pas si ca a le meme nom)

TeBeCo
3
maitredede Messages postés 153 Date d'inscription vendredi 9 août 2002 Statut Membre Dernière intervention 18 septembre 2009
18 août 2008 à 17:19
Bonjour,

Un peu de politesse ne fait pas de mal (bonjour, s'il vous plait...)

Ensuite, quand tu lances ton programme dans Visual Studio, au moment où Visual Studio attrape l'exception, regarde la valeur de la propriété "StackTrace" de ton exception, et tu verra où est ton erreur. Tu en déduira quel paramètre n'a pas la bonne valeur et tu pourra corriger ton erreur.

@+
0
ikaer Messages postés 42 Date d'inscription lundi 14 janvier 2008 Statut Membre Dernière intervention 23 septembre 2008
18 août 2008 à 20:37
Byte est un type valeur et non référence je crois

http://msdn.microsoft.com/fr-fr/library/5bdb6693(VS.80).aspx

ce qui fait qu'il ne peut pas prendre la valeur null comme un typé réference. Enfin je crois
0
Rejoignez-nous