Bonjour
Je voudrai trouver un "idclient" non présent dans ma table.
J'ai donc créer une fonction IdClient() retournant un string:
	
	| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 |  
protected string IdClient()
    {
        int idclient = 1;
        bool trouve = false;
 
        while( trouve == false){
            string sql = "Select idclient from clients where idclient="+idclient;
            using (SqlConnection conn = new SqlConnection(GetConnectionString()))
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                conn.Open();
 
 
               int id = (int)cmd.ExecuteScalar();
 
                conn.Close();
                if (id == idclient)
                {
                    idclient++;
                }
                else
                {
                    trouve = true;
                }
 
            }           
        }
        string idcli = idclient.ToString();
        return idcli;
    } | 
 cependant j'obtient l'erreur suivante:
	
		
			
			
				System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.
			
		
 
	 
 sur la ligne:
	
	int id = (int)cmd.ExecuteScalar();
 Je ne comprend pas pourquoi.
Merci de m'aider
						
					
Partager